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*;*
}
else {
// big-endian (mac)
u = rawData << 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][1]) 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);*
}
_*[1]: http://docs.unity3d.com/ScriptReference/Application-dataPath.html*_
https://mcmap.net/q/52763/unity-how-to-use-a-script-to-import-terrain-raw
– FungibleThanks for the replies... I have seen that link but the problem is that I cant figure out how to use it... any help?
– Syllabism