I saw the following code for sampling heightmap data from another site
Loading terrain height map data in Unity
Loading terrain height map data in Unity
private void LoadHeightmap( string filename ) { // Load heightmap. Texture2D heightmap = ( Texture2D ) Resources.Load( "Heightmaps/" + filename ); // Acquire an array of colour values. Color[] values = heightmap.GetPixels(); // Run through array and read height values. int index = 0; for ( int z = 0; z < heightmap.height; z++ ) { for ( int x = 0; x < heightmap.width; x++ ) { m_heightValues[ z, x ] = values[ index ].r; index++; } } // Now set terrain heights. m_terrain.terrainData.SetHeights( 0, 0, m_heightValues ); }Generally while I'd say this is about right, since a height map is gray scaled (at least hopefully gray scaled. Otherwise, for alternate height map false color images, I imagine you'd need to find some adequate height sampling method, or use whatever algorithm that were used in producing the height map firstly in reverse. One technically only need sample one color of an RGB tuplet (since in gray scale any color has equal RGB color values). The only remaining problem is normalizing the sampled color value from 0 to 1 on its given scaling. This is accomplished by dividing the sampled color by 255 since all color variations range from 0 to 255.
Thus I believe the correct code in Unity should read
private void LoadHeightmap( string filename ) { // Load heightmap. Texture2D heightmap = ( Texture2D ) Resources.Load( "Heightmaps/" + filename ); // Acquire an array of colour values. Color[] values = heightmap.GetPixels(); // Run through array and read height values. int index = 0; for ( int z = 0; z < heightmap.height; z++ ) { for ( int x = 0; x < heightmap.width; x++ ) { m_heightValues[ z, x ] = values[ index ].r/255; index++; } } // Now set terrain heights. m_terrain.terrainData.SetHeights( 0, 0, m_heightValues ); }
Pretty easy really. This script would be limited for 8 bit images. Generally Unity image (texture) importers are restricted outside of 16 bit options (at least at the moment I hadn't seen import options). I've also tried using Windows System.Drawing libraries for C # but all I could find were bitmap conversions of something like png or 8 bit bitmaps. It works but there are definitely noticeable graphical step value artifacts visually speaking on a given mesh terrain. If you can produce high resolution maps this may not be a problem, but if you are restricted in terms of resolution of terrain, then you may want to consider using 16 bit importation scripts.
No comments:
Post a Comment