- Home /
Raw File / Heightmap Switch at Runtime
Hello, I have a game where I want the heightmap of the terrrain to be changed at runtime by the player, but I cannot get that to work. I have multiple .raw files in the project already that are waiting to to be used, but I cant figure out how to extract the data from them, and apply it to the terrain at runtime. (btw I use c# mostly, but i'm desperate enough to accept javascript files)
Thank you for your help it is very much appreciated.
Thanks for the replies... I have seen that link but the problem is that I cant figure out how to use it... any help?
Answer by villevli · Sep 09, 2016 at 09:10 PM
I came up with a better method. The raw files can be imported and serialized by Unity when their extension is changed to .bytes and you can use the UnityEngine.TextAsset type to reference them. I made extension methods for the TerrainData class which can load the raw data from UnityEngine.TextAsset.bytes array.
You can make a TextAsset array in your script and drag the .byte heightmaps to it in the inspector:
public TextAsset[] heightmaps;
When you want to load a heightmap from the array to a terrain, call:
terrain.terrainData.LoadHeightmap(heightmaps[index].bytes);
Here's the extension class script for TerrainData. You can choose the endianness (Little (windows) or Big (macintosh)). If adjustResolution parameter is true, the heightmap resolution of the terrain is matched to the file.
using UnityEngine;
using System.Collections;
public enum Endianness
{
Little,
Big
}
public static class TerrainExtensions {
public static void LoadHeightmap(this TerrainData tData, byte[] rawData, Endianness dataEndianness = Endianness.Little, bool adjustResolution = false)
{
int h = (int)Mathf.Sqrt((float)rawData.Length / 2);
if (adjustResolution) {
var size = tData.size;
tData.heightmapResolution = h;
tData.size = size;
}
else if (h > tData.heightmapHeight) {
h = tData.heightmapHeight;
}
int w = h;
float[,] data = new float[h, w];
int i = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int u;
if (dataEndianness == Endianness.Little) {
// little-endian (windows)
u = rawData[i + 1] << 8 | rawData[i];
}
else {
// big-endian (mac)
u = rawData[i] << 8 | rawData[i + 1];
}
float v = (float)u / 0xFFFF;
data[y, x] = v;
i += 2;
}
}
tData.SetHeights(0, 0, data);
}
}
Other way (load directly from .raw files):
When you build your project you have to copy the heightmap file to your data folder (indicated by Application.dataPath) after the build has finished . With the default path in this script you have to create a Heightmaps folder inside the data folder and put the heightmap.raw file there. In editor the data folder is ProjectFolder/Assets so you can have it in Assets/Heightmaps/
public string heightmapPath = "/Heightmaps/heightmap.raw";
public Terrain terrain;
void SomeMethod() {
LoadTerrain(heightmapPath, terrain.terrainData);
}
void LoadTerrain(string aFileName, TerrainData aTerrain)
{
aFileName = Application.dataPath + aFileName
int h = aTerrain.heightmapHeight;
int w = aTerrain.heightmapWidth;
float[,] data = new float[h, w];
using (var file = System.IO.File.OpenRead(aFileName))
using (var reader = new System.IO.BinaryReader(file))
{
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
float v = (float)reader.ReadUInt16() / 0xFFFF;
data[y, x] = v;
}
}
}
aTerrain.SetHeights(0, 0, data);
}
I got it to work! Thank you so much for your help it was very much appreciated!
Actually I have run into a little problem now.... $$anonymous$$y terrain seams to be created all zigzagy and I cant figure out how to fix it
Is the raw file in the correct format? The script reads it as 16bit "windows" (little endian).
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Set Hightmap resolution while importing custom heightmap texture? 1 Answer
Terrainmap is lined 1 Answer
why heightmap flips in terrain import raw(certain case)? 0 Answers
C#: Generate Raw File from byte[]? 1 Answer