Trouble rendering quads correctly
Hi all,
This is the first question I've posted, so hopefully I've done everything right!
I've been putting together a sine function to generate a mesh. So far, I've managed to get all of the vertices lined up, and I'm able to generate a mesh, but only from every 4th vertices.
The code sample that works is below:
mesh.vertices = vertices;
//triangles
int[] tri = new int[mesh.vertices.Length * 6];
for (int x = 0, t = 0; x < vertices.Length; x++, t+=6) {
if (x % 4 == 0) {
tri [t] = x;
tri [t+1] = x+3;
tri [t+2] = x+1;
tri [t+3] = x;
tri [t+4] = x+2;
tri [t+5] = x+3;
}
}
mesh.triangles = tri;
}
Here's a screenshot of what that produces:
Problem is, when I try and bring the code down so that it starts with every vertices divisble by two, to fill the gaps, I get an error:
mesh.vertices = vertices;
//triangles
int[] tri = new int[mesh.vertices.Length * 6];
for (int x = 0, t = 0; x < vertices.Length; x++, t+=6) {
if (x % 2 == 0) {
tri [t] = x;
tri [t+1] = x+3;
tri [t+2] = x+1;
tri [t+3] = x;
tri [t+4] = x+2;
tri [t+5] = x+3;
}
}
mesh.triangles = tri;
}
Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 192, VertexCount: 32 UnityEngine.Mesh:set_triangles(Int32[]) Generator:GenerateMesh() (at Assets/Scripts/Generator.cs:111) Generator:GenerateBorder() (at Assets/Scripts/Generator.cs:72) Generator:drawCircle() (at Assets/Scripts/Generator.cs:50) Generator:Start() (at Assets/Scripts/Generator.cs:19)
Any idea what this could be, or how I could fix it?
Your answer
Follow this Question
Related Questions
What does Failed setting triangles. Some indices are referencing out of bounds vertices mean? 1 Answer
Triangles don't follow the vertices changed on mesh 1 Answer
How to get separated mesh vertices to interact? 0 Answers
Some triangles are black on a mesh generated by script 1 Answer
Vertex Coloured Shader for trianges. 1 Answer