Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question My procedural terrain script isn't affecting the y-axis of the vertices.

Discussion in 'Editor & General Support' started by JonteErik, Jun 5, 2023.

  1. JonteErik

    JonteErik

    Joined:
    Jan 29, 2022
    Posts:
    5
    So iv'e made a noisemap using simplex noise and trying to create a 3d terrain with it. Here's the code:
    Code (CSharp):
    1. public int TextureSize;
    2.     public int xSize = 20;
    3.     public int zSize =20;
    4.     public float NoiseScale, IslandSize;
    5.     [Range(1, 20)] public int NoiseOctaves;
    6.  
    7.    
    8.     // Privates
    9.  
    10.     Color[] col;
    11.     Texture2D tex;
    12.     int Seed;
    13.     Mesh mesh;
    14.     Vector3[] vertices;
    15.     Vector2[] uvs;
    16.     int[] triangles;
    17.     float noiseValue;
    18.     public Gradient ColorGradient;
    19.  
    20.     private void Start()
    21.     {
    22.         mesh = new Mesh();
    23.         GetComponent<MeshFilter>().mesh = mesh;
    24.         Seed = UnityEngine.Random.Range(0, 99999);
    25.         tex = new Texture2D(TextureSize, TextureSize);
    26.         col = new Color[tex.height * tex.width];
    27.  
    28.         Renderer rend = GetComponent<MeshRenderer>();
    29.         rend.sharedMaterial.mainTexture = tex;
    30.  
    31.         Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
    32.  
    33.         for (int x = 0, i = 0; x < TextureSize; x++)
    34.         {
    35.             for (int y = 0; y < TextureSize; y++, i++)
    36.             {
    37.                 col[i] = ColorGradient.Evaluate(Noisefunction((float)x, (float)y, Org));
    38.             }
    39.         }
    40.         tex.SetPixels(col);
    41.         tex.Apply();
    42.         tex.wrapMode = TextureWrapMode.Clamp;
    43.     }
    44.  
    45.     private void Update()
    46.     {
    47.         CreateShape();
    48.         UpdateMesh();
    49.     }
    50.  
    51.     private void UpdateMesh()
    52.     {
    53.         mesh.Clear();
    54.         mesh.vertices = vertices;
    55.         mesh.triangles = triangles;
    56.         mesh.uv = uvs;
    57.         mesh.RecalculateNormals();
    58.     }
    59.  
    60.     //creates the mesh
    61.     private void CreateShape()
    62.     {      
    63.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
    64.         for (int z = 0, i = 0; z <= zSize; z++)
    65.         {
    66.             for (int x = 0; x <= xSize; x++)
    67.             {            
    68.                 vertices[i] = new Vector3(x, noiseValue, z);
    69.                 i++;
    70.             }
    71.         }
    72.         triangles = new int[xSize * zSize * 6];
    73.  
    74.         int vert = 0;
    75.         int tris = 0;
    76.         for (int z = 0; z < zSize; z++)
    77.         {
    78.             for (int x = 0; x < xSize; x++)
    79.             {
    80.                 triangles[tris + 0] = vert + 0;
    81.                 triangles[tris + 1] = vert + xSize + 1;
    82.                 triangles[tris + 2] = vert + 1;
    83.                 triangles[tris + 3] = vert + 1;
    84.                 triangles[tris + 4] = vert + xSize + 1;
    85.                 triangles[tris + 5] = vert + xSize + 2;
    86.                 vert++;
    87.                 tris += 6;
    88.             }
    89.             vert++;
    90.         }
    91.  
    92.         uvs = new Vector2[vertices.Length];
    93.         for (int z = 0, i = 0; z <= zSize; z++)
    94.         {
    95.             for (int x = 0; x <= xSize; x++)
    96.             {
    97.                 uvs[i] = new Vector2((float)x / xSize, (float)z / zSize);
    98.                 i++;
    99.             }
    100.         }
    101.     }
    102.  
    103.  
    104.     //creates noise
    105.     private float Noisefunction(float x, float y, Vector2 Origin)
    106.     {
    107.         float a = 0, noisesize = NoiseScale, opacity = 1;
    108.  
    109.         for (int octaves = 0; octaves < NoiseOctaves; octaves++)
    110.         {
    111.            
    112.             float xVal = (x / (noisesize * TextureSize)) + Origin.x;
    113.             float yVal = (y / (noisesize * TextureSize)) - Origin.y;
    114.             noiseValue = noise.snoise(new float2(xVal, yVal)); // thought this was the line to get the y value of the mesh
    115.             a += Mathf.InverseLerp(0, 1, noiseValue) / opacity;
    116.  
    117.             noisesize /= 2f;
    118.             opacity *= 2f;
    119.         }
    120.  
    121.         return a -= FallOffMap(x, y, TextureSize, IslandSize);
    122.     }
    123.  
    124.     //makes it an island
    125.     private float FallOffMap(float x, float y, int size, float islandSize)
    126.     {
    127.         float gradient = 1;
    128.  
    129.         gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size));
    130.         gradient -= 16;
    131.         gradient /= islandSize;
    132.  
    133.  
    134.         return gradient;
    135.     }
    136. }

    I tried setting the y-value of the vertices to the noisemap, but that didn't do anything and neither did multiplying the noisevalue by a large number. All vertices are still at the same height. Any help on how I can make it into a 3d terrain would be appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    For every execution through line 68 where you set the height, you use
    noiseValue
    and it never changes for the entire loop.

    That variable is also a member variable of your class, which is an unsual architecture choice. Typically something like that would be computed every vert.

    If there's not all of your issues, well, it is ...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.