Hey Hey!
Just your friendly local code no0b here
Hoping someone can help me fix this last bit I’ve been stuck on for a little now.
Using this script, How are the bytes in a 16bit heightmap organized? - Questions & Answers - Unity Discussions
I’ve been able to import a 16bit heightmap, but as described by Eric I believe my byte order is mismatched…unfortunately this is my first time working with ANY of this information and I’m at a total loss on how to correct this. I have tried reversing the byte order, but everything goes out of whack and the terrain is completely unrecognizable…
Here is the current code (basically the same as what’s posted in the link above):
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
public TextAsset binaryData;
private Terrain terrain; // terrain to modify
protected int heightmapWidth; // heightmap width
protected int heightmapHeight; // heightmap height
void Start (){
Terrain.activeTerrain.terrainData.heightmapResolution = 256;
terrain = Terrain.activeTerrain;
heightmapWidth = terrain.terrainData.heightmapWidth;
heightmapHeight = terrain.terrainData.heightmapHeight;
byte[] bytes = binaryData.bytes; // Loads the whole heightmap file into this array
//System.Array.Reverse (bytes);
float[,] heightmap = new float[heightmapWidth,heightmapHeight];
int byteNumber = 0; // Will be used to iterate through the 'bytes' array
for(int width = 0; width < bytes.Length/(heightmapWidth*2); width++){
for(int height = 0; height < bytes.Length/(heightmapHeight*2); height++){
heightmap[width, height] = ((bytes[byteNumber] << 0x08) | bytes[byteNumber+1])/65536f; // Sets value in 'bytes' array to matrix
byteNumber += 2;
}
}
terrain.terrainData.SetHeights(0, 0, heightmap);
}
}
I’ve commented out the reverse line because I don’t think it’s what I’m looking for, I could be wrong though, I think what I’m probably going for is something more along these lines:
http://www.csharp-examples.net/reverse-bytes/
Either way…I can’t get any of it to work, so yeah. Here I am…again.
Current project build: