Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Procedural terrain splatmap

Discussion in 'Scripting' started by rkaetano, Oct 26, 2014.

  1. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    Hey guys, I`m trying to use the cellular automata to create one procedural texture for my terrain.
    I already have the cellular automata thing working, but I`m facing some problems to apply the texture to the terrain.
    I know that could be a lack of math envolved, since the resolution of the terrain and size are different, but I`m really facing big problem to figure out. Can someone give one idea ?


    Code (CSharp):
    1. void ApplySplat()
    2.     {
    3.         int normX;
    4.         int normY;
    5.  
    6.         TerrainData terrainData = terrain.terrainData;
    7.         float [, ,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];
    8.  
    9.         for ( int x = 0; x < terrainData.alphamapWidth; x++ )
    10.         {
    11.             for ( int y = 0; y < terrainData.alphamapHeight; y++ )
    12.             {
    13.                 splatmapData[ y, x, 0 ] = 0f;
    14.                 splatmapData[ y, x, 1 ] = 1f;
    15.             }
    16.         }
    17.  
    18.         for ( int x = 0; x < mapWidth; x++ )
    19.         {
    20.             for ( int y = 0; y < mapHeight; y++ )
    21.             {
    22.                 normX = Mathf.RoundToInt( ( ( x - terrain.transform.position.x ) / terrain.terrainData.size.x ) * terrainData.heightmapWidth );
    23.                 normY = Mathf.RoundToInt( ( ( y - terrain.transform.position.z ) / terrain.terrainData.size.z ) * terrainData.heightmapHeight );
    24.  
    25.                 splatmapData[ normY, normX, 0 ] = _map[ x, y ] ? 1f : 0f;
    26.                 splatmapData[ normY, normX, 1 ] = _map[ x, y ] ? 0f : 1f;
    27.             }
    28.         }
    29.  
    30.         terrainData.SetAlphamaps( 0, 0, splatmapData );
    31.     }
    Result of the function above:
    Captura de Tela 2014-10-26 às 18.07.56.png
     
  2. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    Bump ?
     
  3. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    Found one solution
    Well just posting for if someone face the same problem.
    My problem was that the texture resolution should have the same size of the terrain, which should be multiples of 8.

    64 / 128 / 256 / etc...