- Home /
how to use a script to import terrain .raw?
I want players to be able to add the game the maps . I want that added .raw files . (via script)
Answer by Bunny83 · Oct 20, 2015 at 12:37 AM
So you want to load a terrain heightmap at runtime from a file? ".raw" is not a specific format. It simply contains raw data. A quite common format for a height map is 16 grayscale, however in theory it could contains anything.
There is no built-in method to load raw terrain data. You have to load the file yourself and convert it into a float array that can be used with TerrainData.SetHeights.
If you want to support the same options Unity has for the terrain heightmap importer, you have to provide a similar GUI with options for bit depth and resolution. Without those you can't load the data. raw files don't have any header. It literally only contains the raw data. So when reading the file you either only support one specific format (for example 513 x 513 16bit) or you have to provide options for the user to choose from.
Here's an example how to read a 16 bit raw image with "windows" format (little endian):
void LoadTerrain(string aFileName, TerrainData aTerrain)
{
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);
}
There are no error checks. So if the file doesn't contain enough data it will fail with an exception. If the resolution doesn't match the terrain size the image won't appear right.
What does the " / 0xFFFF " part mean in line 13 in your code?
In that line I divide by the max possible value that can be represented by 16 bit.
0xFFFF = 65535
So an unsigned 16 bit value is in range 0 - 65535. By dividing that value by 0xFFFF the resulting values are in range 0.0 - 1.0
The float cast is important. ReadUInt16 returns an unsigned integer while 0xFFFF also represents an integer. If you would divide an integer by an integer the result would be again an integer. One of the operands need to be a float to get a float result.
Just in case you don't know:
FFFF is hexadecimal so each digit doesn't have 10 possible values but 16. So in addition to the numbers 0 to 9 there is A=10, B=11, C=12, D=13, E=14 and F=15. Unlike the decimal system the value of each digit isn't a multiple of 10 (1, 10, 100, 1000), but a multiple of 16 (1, 16, 256, 4096)
Ah, thanks. Great explanation. So if we were reading an 8-bit heightmap, it would be " / 0xFF " or " / 255 ", since 8-bit is in the range 0 - 255, right?
One more question--would you still use "ReadUInt16()" if the heightmap were 8-bit?
No, :) of course not. ReadUInt16() reads an unsigned 16 bit integer from the binary stream. If your height map samples have 8 bit you have to read a single unsigned byte using ReadByte. See the documentation of the BinaryReader to find an appropriate method.
Note: As soon as a single sample has more than 8 bit you can get in trouble with the endianness of the system you work on. x86 / x64 PCs use the "little endian" format. That means types that require two or more bytes memory are stored in reverse order. So for example the integer value "5000" which is represented as 16 bit hex as "0x1388" is stored like this in memory:
0 1
-------
88 13
As you can see the low order byte comes first and the high order byte comes last. There are systems (like $$anonymous$$ac afaik) which uses the big endian format so the same data would be stored like this:
0 1
-------
13 88
I only work on x86 / x64 PCs so i never really had to take care about that. I just want to re$$anonymous$$d the reader to keep this in $$anonymous$$d when dealing with raw data. The Unity importer also provides a dropdown to allow the user to specify the endianness of the file.
Hi, This works great but I need it in mac byte order and also flipped vertical. How can I do that?
Your answer
Follow this Question
Related Questions
Terrain Tree Problem 0 Answers
Climbing script - impossible get under the map and turning around 0 Answers
How to use Height Maps 2 Answers
Stopping the 270 degree FBX default rotation on import 6 Answers