I want to sample the Terrain heights to a Texture for a custom Editor tool and having some problems.
What ever I do the Texture is always black.
here is a screenshot and the relevant code:
private TerrainData Data
{
get
{
return _data;
}
set
{
// if the value (TerrainData) has changed re-/ sample the Texture
if( value != _data )
SampleTexture( value.GetHeights( 0, 0, value.heightmapWidth, value.heightmapHeight ), value.heightmapWidth, value.heightmapHeight );
_data = value;
}
}
private void SampleTexture( float[,] heightMap, int width, int height )
{
_texture = new Texture2D( width, height, TextureFormat.RGB24, false );
_texture.wrapMode = TextureWrapMode.Clamp;
var colors = new Color[ heightMap.Length ];
for( int x = 0; x < width; x++ )
for( int y = 0; y < height; y++ )
colors[x + y] = Color.Lerp( Color.black, Color.white, heightMap[x, y] );
_texture.SetPixels( colors );
_texture.Apply();
}
// GUI function
protected override void DrawNodeContent()
{
GUILayout.Label("TerrainData");
Data = (TerrainData)EditorGUILayout.ObjectField( Data, typeof( TerrainData ), false );
_scroll = GUILayout.BeginScrollView( _scroll );
if( _texture != null )
{
GUILayout.Box( _texture );
//GUI.DrawTexture( new Rect( rect.x, rect.y, 100, 100 ), _texture, ScaleMode.ScaleAndCrop, false );
}
GUILayout.EndScrollView();
}
Anyone having an idea whats going wrong?
EDIT: solved. I did the loop wrong. for every one who is curious
for( int x = 0; x < width; x++ )
for( int y = 0; y < height; y++ )
colors[x * height + y] = Color.Lerp( Color.black, Color.white, heightMap[x, y] );