Phoenix2233
Phoenix2233

Reputation: 13

Having trouble generating a mesh in unity

I have been trying to generate a mesh using the code below, and have been successful generating a mesh with width and length of one, but any more and I get an error in the console that says

"Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 24, VertexCount: 9 UnityEngine.Mesh:set_triangles (int[])"

I have done all the math of calculating the triangles on paper, and all of the values are within the length of the vertices array, going from 0 to 8. I don't have any clue what I am doing wrong, or why this error message is getting thrown, so any help would be greatly appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshGenerator : MonoBehaviour
{
    Mesh mesh;
    Vector3[] vertices;
    int[] triangles;

    public Mesh GenerateMesh(int mapWidth, int mapHeight)
    {
        mesh = new Mesh();
        vertices = new Vector3[(mapHeight + 1) * (mapWidth + 1)];
        triangles = new int[6 * (mapHeight * mapWidth)];
        int index = 0;
        for (int x = 0; x <= mapHeight; x++)
        {
            for (int y = 0; y <= mapWidth; y++)
            {
                vertices[index] = new Vector3(x, 0, y);
                index += 1;
            }
        }
        int z = 0;
        for (int i = 0; i <= triangles.Length - 1; i += 6)
        {
            Debug.Log(i);
            if (z == mapWidth)
            {
                triangles[i] = i + 1;
                triangles[i + 1] = i + 2;
                triangles[i + 2] = i + mapWidth + 3;
                triangles[i + 3] = i + 1;
                triangles[i + 4] = i + mapWidth + 3;
                triangles[i + 5] = i + mapWidth + 2;
                z = 1;
            }
            else
            {
                triangles[i] = i;
                triangles[i + 1] = i + 1;
                triangles[i + 2] = i + mapWidth + 2;
                triangles[i + 3] = i;
                triangles[i + 4] = i + mapWidth + 2;
                triangles[i + 5] = i + mapWidth + 1;
                z++;
            }
        }
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        return mesh;
    }
}

Upvotes: 0

Views: 1109

Answers (1)

Phoenix2233
Phoenix2233

Reputation: 13

I figured it out. The problem was I was using the index of the triangle array to calculate the index of the vertex array. Instead, I needed to use a separate variable (a) to calculate the vertex index. The new code is below.

        int a = 0;
        for (int i = 0; i < triangles.Length; i += 6)
        {
            Debug.Log(i);
            if (z == mapWidth)
            {
                triangles[i] = a + 1;
                triangles[i + 1] = a + 2;
                triangles[i + 2] = a + mapWidth + 3;
                triangles[i + 3] = a + 1;
                triangles[i + 4] = a + mapWidth + 3;
                triangles[i + 5] = a + mapWidth + 2;
                z = 1;
                a += 2;
            }
            else
            {
                triangles[i] = a;
                triangles[i + 1] = a + 1;
                triangles[i + 2] = a + mapWidth + 2;
                triangles[i + 3] = a;
                triangles[i + 4] = a + mapWidth + 2;
                triangles[i + 5] = a + mapWidth + 1;
                z++;
                a++;
            }
        }

Upvotes: 0

Related Questions