Search Unity

Question AlphaMaps headache

Discussion in 'World Building' started by RZGames_Jethro, Jan 20, 2023.

  1. RZGames_Jethro

    RZGames_Jethro

    Joined:
    Jul 6, 2017
    Posts:
    88
    trying to do a simple:
    - add up generated noise layers
    - apply to heightmap
    - add a texture to the terrain per noise layer
    - (fail) use each noise layer as splat maps (alphaMaps),

    i even write to texture per 4 noise layers, thinking i could, like in the past, just assign a RGBA Texture2D to the terrain splatmap


    code:
    Code (CSharp):
    1. for (int i = 0; i < terrainCount; i++)
    2.         {
    3.             for (int j = 0; j < terrains[i].terrainData.alphamapTextureCount; j++)
    4.             {
    5.                 int splatindex = j;
    6.                 if(splatCount > terrainCount)
    7.                     splatindex = j + (i * terrainCount);
    8.                 var splatMap = splatMaps[splatindex];
    9.                 Color[] pix = splatMap.GetPixels();
    10.                 float[,,] AMaps = new float[splatMap.width, splatMap.height, terrains[i].terrainData.terrainLayers.Length];
    11.                 for (int k = 0; k < splatMap.width; k++)
    12.                 {
    13.                     for (int l = 0; l < splatMap.height; l++)
    14.                     {
    15.                         Vector4 pixColor = pix[l + (k * splatMap.width)];
    16.                         for (int n = 0; n < 4; n++)
    17.                         {
    18.                             AMaps[k, l, n] += pixColor[n]/4;
    19.                         }
    20.                     }
    21.                 }
    22.                 terrains[i].terrainData.SetAlphamaps(0, 0, AMaps);
    23.                 terrains[i].Flush();
    24.             }
    25.         }
    I dont understand how we are suppose to iterate through the Alphamaps if a terrain has more then one.
    if i use Terrain tools to extract the splatmaps after running this code, the first on looks good, second just black... (btw i set 8 textures before so have 2 splats / terrain)
    In the code i try to sample the color from the generated splatmaps and assign to the mysterious AlphaMaps.
    as the docs say the 3rd element:
    "corresponds to the number of splatmap textures."
    or
    "while the third denotes the splatmap texture to which the alphamap is applied."
    but as it turns out, it looks more like the 3rd param represents the splatmap channels thats being written too...
    anyway either i dont get how to write to channels or how to write to more then 1 splatmap on a terrain.
     
  2. RZGames_Jethro

    RZGames_Jethro

    Joined:
    Jul 6, 2017
    Posts:
    88
    for others getting confused by this.
    the 3rd element in AlphaMaps is indeed the index of the channels over all the maps, so 0-3 are for the 1st splatmap, 3-7 for the second...