Question by
Raffael_W · Sep 12, 2017 at 11:35 AM ·
c#meshrenderingprocedural meshprocedural generation
Mesh wont render although there are no errors!
Hello! Im trying to generate a simple plane in form of a mesh with this code, but it wont render/show up although there are NO errors. I also have already tried to hardcode some vertices and triangles to see if they get rendered and the do somehow! I dont know what the problem is, i would be very thankfull for any advise or tips!
With hardcoded testmode ON:
With hardcoded testmode OFF:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer),typeof(MeshCollider))]
public class TriangleMeshGenerator : MonoBehaviour {
//mapsize in Tiles
public int mapSizeX;
public int mapSizeZ;
//Offset on the y axis
public float yOffset;
//activate testmode or not
public bool testMeshMode;
//Number of Vertices on that axis
int xVertices;
int zVertices;
Mesh mesh;
private Vector3[] vertices;
private int[] triangles;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
//Number of Vertices on that axis
xVertices = mapSizeX + 1;
zVertices = mapSizeZ + 1;
if (testMeshMode == true)
{
//runs hardcoded methode
testMesh();
}
else
{
//runs procedural methode
GenerateVertices();
GenerateTriangles();
}
GenerateMesh();
}
public void GenerateVertices()
{
int vertIndex = 0;
int vertIndexSize = xVertices * zVertices;
for (int z = 0; z < zVertices; z++)
{
for (int x = 0; x < xVertices; x++)
{
//set array size of triangles
vertices = new Vector3[vertIndexSize];
//set vertices
vertices[vertIndex] = new Vector3(x, yOffset, z);
Debug.Log("vertex " + vertIndex + ": x" + vertices[vertIndex].x + ", z" + vertices[vertIndex].z);
vertIndex += 1;
}
}
}
void GenerateTriangles()
{
int triIndex = 0;
int triIndexSize = mapSizeX * mapSizeZ * 2 * 3;//2 bc: 2 triangles per square, 3 bc: 3 indices per triangle
for (int z = 0; z < mapSizeZ; z++)
{
for (int x = 0; x < mapSizeX; x++)
{
//set array size of triangles
triangles = new int[triIndexSize];
//set first triangle from square
triangles[triIndex + 0] = x + (xVertices * z);
triangles[triIndex + 1] = x + (xVertices * (z + 1));
triangles[triIndex + 2] = x + 1 + (xVertices * z);
//set second triangle from square
triangles[triIndex + 3] = x + 1 + (xVertices * z);
triangles[triIndex + 4] = x + (xVertices * (z + 1));
triangles[triIndex + 5] = x + 1 + (xVertices * (z + 1));
Debug.Log("1 Tri: " + triangles[triIndex+0] + ", " + triangles[triIndex+1] + ", " + triangles[triIndex+2]);
Debug.Log("2 Tri: " + triangles[triIndex + 3] + ", " + triangles[triIndex + 4] + ", " + triangles[triIndex + 5]);
triIndex += 6;
}
}
}
void GenerateMesh()
{
if (mesh != null)
{
mesh.Clear();
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
//just for testing if it outputs Hardcoded code (it does)
void testMesh()
{
//set vertices
vertices = new Vector3[6];
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(1, 0, 0);
vertices[2] = new Vector3(2, 0, 0);
vertices[3] = new Vector3(0, 0, 1);
vertices[4] = new Vector3(1, 0, 1);
vertices[5] = new Vector3(2, 0, 1);
//set triangles
triangles = new int[12];
triangles[0] = 0;
triangles[1] = 3;
triangles[2] = 1;
triangles[3] = 1;
triangles[4] = 3;
triangles[5] = 4;
triangles[6] = 1;
triangles[7] = 4;
triangles[8] = 2;
triangles[9] = 2;
triangles[10] = 4;
triangles[11] = 5;
}
}
programm-pic-with-testmode.png
(303.5 kB)
programm-pic-without-testmode.png
(288.3 kB)
Comment
Answer by Raffael_W · Feb 13, 2018 at 06:59 PM
The problem was i had to use lists instead of arrays. Lists are dynamic arrays and can be resized...