While Unity furnishes a raw image map importer for heightmaps. Out of curiosity and desire for developing a batch loading script (where I could import at any given time as many terrain heightmaps as desired), I've investigated the potential for loading high resolution 16 bit gray scale heightmaps.
In this case, it actually appears to be very simple. I've used a reference script, in this case found at
The problem is I wanted a section of this script customized from Java Script over to C # so basically the key is porting this main excerpt of code. Where heightmapimage.size is assumed to have equal length x height dimensions
using System.IO;
...
var bytes = File.ReadAllBytes(pathtofile + filename);
m_terrain.terrainData.heightmapResolution = heightmapimage.size;
m_heightValues = new float[heightmapimage.size,heightmapimage.size];
int i = 0;
for (int x=0; x < heightmapimage.size; x++){
for (int y=0; y < heightmapimage.size; y++){
using System.IO;
...
var bytes = File.ReadAllBytes(pathtofile + filename);
m_terrain.terrainData.heightmapResolution = heightmapimage.size;
m_heightValues = new float[heightmapimage.size,heightmapimage.size];
int i = 0;
for (int x=0; x
}
}
A grey scale heightmap is represented in 16 bits actually as 2 bytes, or 1 byte=8bits + 1 byte=8bits.
And the apparent algorithm for representing 1 of 65,536 gray scale color values is given above with normalization provided (this is done for PC), you'd simply follow the same procedure reversing the byte order for Mac conversions.
While I couldn't find any information explicitly on the encoding process for RAW16 (at least with a google search on any number of variant search strings, other than generalized information). Mathematically speaking it appears the encoding process is pretty straightforward modulus remainder arithmetic. A reading of the byte array returns the multiplier of the modulus amount (how many times wrapping around the value 256) which is multiplied by the 256 value factor (or if you think of this in two dimensional matrices, a row has 256 columns, with 256 rows for each column), the column space represents the remainder or modulus of 256 value which is the number on a given row before a value increments to the next row space. Thus by analogy we read the row column positions which fill any one of the values ranging from 0 to 65,535 which in turn represents the gray scale pixel value on the heightmap image.
No comments:
Post a Comment