0 votes

I am trying to procedurally generate a terrain mesh so that it is different each time the project is loaded and everything is working as intended except that most meshes get this stringing/shadowing of surfaces along the z-axis.
The problem is more prominent when the amplitude of the terrain is increased.
Can someone please a solution?

Example 1:
shadow terrain view 1

Example 2:
Shadow terrain view 2

Example 3:
Shadow terrain view 3

This is where I assume the problem is:

    public override void _Ready()
   {
        int vertexIndex = 0;
        var vertices = new Vector3[mapWidth*mapHeight];

        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);
        //surfaceTool.AddSmoothGroup(true);

        NoiseTexture noiseTexture = terrainNoise();

        for (int z = 0; z < mapHeight; z++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                float vertHeight = noiseTexture.Noise.GetNoise2d(x,z) * 25;
                vertices[vertexIndex] = new Vector3(x, vertHeight, z);
                vertexIndex++;

            }
        }

        vertexIndex = 0;

        for (int z = 0; z < mapHeight; z++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                if (vertexIndex < (mapHeight * mapWidth) - (mapWidth + 1))
                {
                    //First Triangle
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex]));
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex+1]));
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex+mapWidth]));

                    //Second Triangle
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex+mapWidth]));
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex+1]));
                    surfaceTool.AddVertex(new Vector3(vertices[vertexIndex+mapWidth+1]));

                }
                vertexIndex++;
            }
        }          
        surfaceTool.Index();
        surfaceTool.GenerateNormals();

        var meshInstance = new MeshInstance();
        meshInstance.Mesh = surfaceTool.Commit();
        GetChild<MeshInstance>(0).AddChild(meshInstance);
    }

There is also a noise generator before this block of coding.
I am still very new to both Godot and C# and have had to mix several tutorials to get to this point so I'm sorry if the code is messy.

in Engine by (19 points)

1 Answer

0 votes
Best answer

I figured it out in the end.
It turned out that, because i had it drawing triangles based on a vertex index i'd made, it was trying to create triangles between the vertices of the last line and the beginning of the next which stretched the whole map.

I have added an "If()" statement so that the vertices along the edge are skipped when drawing the triangles.

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.