- Home /
C#: Generate Raw File from byte[]?
I'm generating splat maps from code, and I'm at the point where I'm trying to import the generated images into unity.
Anyone done this, or know exactly what the format is?
Is it just a series of bytes in sets of 3 for rgb, in height/width ?
Answer by Eric5h5 · Mar 15, 2014 at 11:24 PM
Splat maps are RGBA, not RGB. The format is whatever you want, but to use SetAlphamaps it needs to be converted to a float[,,] array. See the ApplySplatmap script included with Fractscape, which exports splat maps as .tga.
Sorry if the question was too vague (and incorrect! I wasn't dealing with splatmaps at this point - am dealing with relief maps). Sorry for the confusion. What I'd like to do is be able to save each of the heightmaps as .raw then use the terrain inspector to import them. It just makes it a little easier if you're generating a ton of them to have it saved as an image.
What I'm currently doing is just saving a set of bytes and applying them with:
byte[] bytes = File.ReadAllBytes( @"..." );
Terrain t = FindObjectOfType< Terrain >();
int size = ( int ) $$anonymous$$athf.Sqrt( bytes.Length );
float[,] heights = new float[size,size];
Debug.Log( bytes.Length );
for (int i = 0; i < bytes.Length; i++ ) {
int x = i%size;
int y = i/size;
float p = ( bytes[ i ]) / 256f ;
heights[ x, y ] = p;
}
t.terrainData.SetHeights(0, 0, heights );
It'd be nicer though to drop this code entirely and just use the built in inspector.
Oh, raw image files are just that, a series of bytes. Not rgb, just the grayscale data. So two bytes per pixel, either little-endian or big-endian. No formatting data.
Thanks for your help Eric!
Sorry if this is a really dumb question but, what exactly are the two bytes per pixel?
The heights range from 0 to 65535, so each pixel is a unsigned short (ushort), which is two bytes, the low byte and the high byte. Or vice versa.