[C#] Seeing abnormal surfaces/shadowing when procedurally generating mesh

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PeteF52

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.

:bust_in_silhouette: Reply From: PeteF52

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.