- Home /
Procedurally Generated mesh not rendering all triangles,Procedurally generated mesh isn't rendering all triangles?
I'm working on making a planet generator, and to start it off I'm trying to see if I can make a plane of procedurally generates squares. I have it print out each time a vertex is made, and every time a square is made out of 2 triangles, as well as the coordinates, and even though the console prints out everything properly, only the top row of squares appear.
If it helps, this is my spaghetti-looking code: (resolution is the amount of squares each side, so a res of 2 would make a 2 by 2 plane, 3 would make a 3 by 3 plane, etc)
int resolution=10;
int numVert = (resolution+1)*(resolution+1)+1;
int numTri = resolution*resolution*6;
Debug.Log(numVert);
Debug.Log(numTri);
Mesh mesh = new Mesh();
Debug.Log(resolution);
Vector3[] vertices = new Vector3[numVert];
int[] triangles = new int[numTri];
int i = 0;
int i2 = 0;
int x = 0;
int y = 0;
for (y = 0; y <= resolution; y++)
{
for (x = 0; x <= resolution; x++)
{
vertices[i+1] = new Vector3(x*2,-y*2,0);
i=i+1;
Debug.Log("Vertex made at "+x+","+y);
}
}
for (y = 1; y <= resolution; y++)
{
for (x = 1; x <= resolution; x++)
{
triangles[i2] = x+resolution+1;
triangles[i2+1] = x;
triangles[i2+2] = x+1;
triangles[i2+3] = x+resolution+1;
triangles[i2+4] = x+1;
triangles[i2+5] = x+resolution+2;
i2=i2+6;
Debug.Log("Square made at "+x+","+y);
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
GetComponent<MeshFilter>().mesh = mesh;
MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = mesh;
}
Answer by amabone · Feb 04 at 10:04 AM
Ok, Mr. you start from a 0 indexing vertex list, u grow up of maxX (resolution) for each y, that for the entire first row is 0.
for (y = 1; y <= resolution; y++)
{
for (x = 1; x <= resolution; x++)
{
....
you start here for 1 while doing calculation. resulting in index 0 never being mentioned, and thats what i would do (i did). you take count of the whole square while triangulating. so u need to check only for the square in a
resolution-1 * resolution-1 grid
those are the number of square in a grid of resolution*resolution vertices so u do
for (y = 0; y <= resolution-1; y++)
{
for (x = 0; x <= resolution-1; x++)
{
and then when selecting the corresponding vertex index u start from the point 0, 0 of the grid and count from there
x---------x+1
| |
x+X------x+X+1
entering the indexing like this
triangles[i2] = x;
triangles[i2+1] = x+1;
triangles[i2+2] = x+resolution;
triangles[i2+3] = x+resolution;
triangles[i2+4] = x+1;
triangles[i2+5] = x+resolution+1;
i2=i2+6;
im actually not sure by the orther, but u can figure it out i hope. Good Luck!
edit: i re-read the answer that i posted and pointed out that x, must be accessed linearly so, u can add another counter, like int index, and then when the last x occur u can call index++
triangles[i2] = index;
triangles[i2+1] = index+1;
triangles[i2+2] = index+resolution;
triangles[i2+3] = index+resolution;
triangles[i2+4] = index+1;
triangles[i2+5] = (index++)+resolution+1
i2=i2+6;
u can rewrite the whole thing as a single for loop int i = 0; i<((resolution-1)*(resolution-1));i++
the y would be i divided / by resolution
removing i2 at all stepping i for 6 like this
for(int i = 0 ;i<((resolution-1)*(resolution-1));i++){
int triangle_index = i*6;
int y = i / resolution;
//triangle[triangle_index+0] = i;//i+1,i+resolution
//blabla...
//i2 no more
}
Your answer
Follow this Question
Related Questions
Procedural mesh with texture atlas, how to get rid of material artifacts? 0 Answers
Re-use lightmap for different mesh with same lightmap uvs 0 Answers
Shadow artifects on mesh with Standard shader 1 Answer
Splitting vertices in a mesh specifically a plane 0 Answers
C# Proceducal Mesh terrain 2 Answers