Reputation: 41
I am trying to procedurally generate a mesh made out of quads, but at the positive y and negative z edges half of the quad is not rendered at all. I can sort of see why it happens, but I don't know how to fix it.
public void create(byte[,,] blocks) {
for(int z = 0; z < 16; z++) {
for(int y = 0; y < 16; y++) {
for(int x = 0; x < 16; x++) {
if(x == 0) {
int CurrentCount = vertices.Count;
vertices.Add(new Vector3(x, y + 1f, z + 1f));
vertices.Add(new Vector3(x, y - 1f, z + 1f));
vertices.Add(new Vector3(x, y - 1f, z - 1f));
vertices.Add(new Vector3(x, y + 1f, z - 1f));
indices.Add(CurrentCount);
indices.Add(CurrentCount + 1);
indices.Add(CurrentCount + 2);
indices.Add(CurrentCount + 2);
indices.Add(CurrentCount);
indices.Add(CurrentCount + 3);
}
}
}
}
}
Upvotes: 0
Views: 396
Reputation: 90629
Your problem is with the normals.
Triangle goes clockwise -> normal shows in your direction.
Triangle goes counter-clockwise -> normal shows away from you => Due to backface culling it is not rendered
What you are doing is defining all your vertices in clockwise order like
(CurrentCount + 3)--------(CurrentCount + 0)
| |
| |
| |
(CurrentCount + 2)--------(CurrentCount + 1)
and then create one triangle with indices in clockwise order
indices.Add(CurrentCount);
indices.Add(CurrentCount + 1);
indices.Add(CurrentCount + 2);
But the other one with counter-clockwise index order
indices.Add(CurrentCount + 2);
indices.Add(CurrentCount);
indices.Add(CurrentCount + 3);
You can see this quite good when you a) enter the wire-frame mode and be move to the other side of the plane
It should rather be
indices.Add(CurrentCount);
indices.Add(CurrentCount + 1);
indices.Add(CurrentCount + 2);
indices.Add(CurrentCount);
indices.Add(CurrentCount + 2);
indices.Add(CurrentCount + 3);
which now constructs one correct plane facing the "right" side of the Unity world
Upvotes: 2