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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Simple terrain texture chaging - help

Discussion in 'Scripting' started by xred13, Jul 19, 2018.

  1. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    Ok, so i've done a very small and straightforward script that should go through all points in the alpha map of my terrain and change the texture according to the y coordinate, but it isn't applying the third texture and the axis are switched for some reason.
    It should be something like: Stone, grass, snow but instead its what is on the image. Help! :)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Biome : MonoBehaviour
    4. {
    5.  
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.     Terrain thisterrain = Terrain.activeTerrain;
    12.     TerrainData td = Terrain.activeTerrain.terrainData;
    13.     drawTextures(thisterrain, td);
    14.  
    15.     }
    16.  
    17.     void drawTextures(Terrain thisterrain, TerrainData td)
    18.     {
    19.         var alphaData = td.GetAlphamaps(0, 0, td.alphamapWidth, td.alphamapHeight);
    20.  
    21.         for (int x = 0; x < td.alphamapWidth; x++)
    22.         {
    23.             for (int y = 0; y < td.alphamapHeight; y++)
    24.             {
    25.                 if (y < 400)
    26.                 {
    27.                     for (int k = 0; k < td.splatPrototypes.Length; k++)
    28.                     {
    29.                         alphaData[x, y, k] = 0;
    30.                     }
    31.                     alphaData[x, y, 1] = 1;
    32.                 }
    33.  
    34.                 else if (y < 1400)
    35.                 {
    36.                     for (int k = 0; k < td.splatPrototypes.Length; k++)
    37.                     {
    38.                         alphaData[x, y, k] = 0;
    39.                     }
    40.                     alphaData[x, y, 0] = 1;
    41.                 }
    42.  
    43.                 else
    44.                 {
    45.                     for (int k = 0; k < td.splatPrototypes.Length; k++)
    46.                     {
    47.                         alphaData[x, y, k] = 0;
    48.                     }
    49.                     alphaData[x, y, 2] = 1;
    50.                 }
    51.  
    52.             }
    53.         }
    54.         td.SetAlphamaps(0, 0, alphaData);
    55.     }
    56. }
     

    Attached Files:

  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    My first guess would be your conditions (y < 1400) are not correct - in this case too big. Therefor texture 3 just never happens. You are cycling through the x and y coordinates based on the alphamapWidth and Height, but using fixed numbers on your conditions.
     
    xred13 likes this.
  3. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    Yep thanks on that, i was changing wrong settings to 2048... about the x and y of the alpha map, shouldnt they be equal to the x and z of the scene? respectively