Thursday, April 9, 2026

Unity mesh solely rendering one set of triangles


I have been utilizing Unity3D to procedurally generate terrain with Perlin Noise and I’ve come throughout an issue the place the mesh that I’ve constructed solely renders one set of triangles.

Unity mesh solely rendering one set of triangles

The next is my MeshGeneration code:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing System.Runtime.CompilerServices;
utilizing NUnit.Framework.Inner.Execution;
utilizing UnityEngine;

public static class MeshGenerator
{
    public static MeshData GenerateMesh(float[,] heightMap)
    {
        int peak = heightMap.GetLength(0);
        int width = heightMap.GetLength(1);
        int vertexIndex = 0;
        
        MeshData meshData = new MeshData(width, peak);

        for (int y = 0; y < peak; y++)
        {
            for (int x = 0; x < width; x++)
            {
                meshData.vertices[vertexIndex] = new Vector3(x, heightMap[y, x], y);
                meshData.uvs[vertexIndex] = new Vector2( x/(float)width, y/(float)peak);
                 
                // If we're not on the sting, then add two triangles to the mesh
                if ((x != width - 1) && (y != peak - 1))
                {
                    meshData.AddTriangle(
                        vertexIndex,
                        vertexIndex + width,
                        vertexIndex + width + 1
                    );
                    meshData.AddTriangle(
                        vertexIndex,
                        vertexIndex + 1,
                        vertexIndex + width + 1
                    );
                }
                
                vertexIndex++;
            }
        }

        return meshData;
    }
}

public class MeshData
{
    public Vector3[] vertices;
    public Vector2[] uvs;
    public int[] triangles;

    public int triangleIndex;
    public MeshData(int meshWidth, int meshHeight)
    {
        vertices = new Vector3[meshWidth * meshHeight];
        uvs = new Vector2[meshWidth * meshHeight];
        triangles = new int[(meshWidth - 1) * (meshHeight - 1) * 6];
    }

    public void AddTriangle(int a, int b, int c)
    {
        triangles[triangleIndex] = a;
        triangles[triangleIndex + 1] = b;
        triangles[triangleIndex + 2] = c;
        triangleIndex += 3;
    }

    public Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.vertices = this.vertices;
        mesh.uv = this.uvs;
        mesh.triangles = this.triangles;
        
        mesh.RecalculateNormals();
        return mesh;
    }
}

I am then passing within the mesh that I get from MeshData.CreateMesh() into the next perform.

public void BuildMesh(MeshData meshData, Texture2D texture)
    {
        meshFilter.sharedMesh = meshData.CreateMesh();
        meshRenderer.sharedMaterial.mainTexture = texture;
    }

I am following this tutorial: https://www.youtube.com/watch?v=4RpVBYW1r5M&checklist=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3&index=5

The Mesh technology code works by creating arrays of vertices, uvs, and triangles, and the populating them by iterating over a Vector3[] heightMap that I created with perlin noise.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles