Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | MrSmithyyy |
I have a script that generates a mesh using an ArrayMesh consisting of 4 triangles that form into 2 quads.
When creating the indices, I noticed that I can have the two quads share the middle indices like so:
var vertices = PoolVector3Array()
var indices = PoolIntArray()
vertices.push_back(Vector3(0, 0, 0))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(0, 0, 1))
vertices.push_back(Vector3(1, 0, 1))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(2, 0, 0))
vertices.push_back(Vector3(1, 0, 1))
vertices.push_back(Vector3(2, 0, 1))
indices.push_back(0)
indices.push_back(1)
indices.push_back(3)
indices.push_back(0)
indices.push_back(3)
indices.push_back(2)
# Index 1 shared
indices.push_back(1)
indices.push_back(5)
indices.push_back(7)
# Index 3 shared
indices.push_back(1)
indices.push_back(7)
indices.push_back(3)
or they can have their own indices:
var vertices = PoolVector3Array()
var indices = PoolIntArray()
vertices.push_back(Vector3(0, 0, 0))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(0, 0, 1))
vertices.push_back(Vector3(1, 0, 1))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(2, 0, 0))
vertices.push_back(Vector3(1, 0, 1))
vertices.push_back(Vector3(2, 0, 1))
indices.push_back(0)
indices.push_back(1)
indices.push_back(3)
indices.push_back(0)
indices.push_back(3)
indices.push_back(2)
indices.push_back(4)
indices.push_back(5)
indices.push_back(7)
indices.push_back(4)
indices.push_back(7)
indices.push_back(6)
and it produces the same result (as far as I can tell by the wireframe). However, if I try to skip defining common vertices and use the common indices, it skews and you can tell a couple of lines are missing from the wireframe:
var vertices = PoolVector3Array()
var indices = PoolIntArray()
vertices.push_back(Vector3(0, 0, 0))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(0, 0, 1))
vertices.push_back(Vector3(1, 0, 1))
# Can't remove this vertex even though it's defined above.
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(2, 0, 0))
# Also can't remove this vertex.
vertices.push_back(Vector3(1, 0, 1))
vertices.push_back(Vector3(2, 0, 1))
# Index 1 shared
indices.push_back(1)
indices.push_back(5)
indices.push_back(7)
# Index 3 shared
indices.push_back(1)
indices.push_back(7)
indices.push_back(3)
I couldn’t find much on this topic but is there a reason that indices can be shared (and is there a performance cost to sharing them?) but vertices have to be defined even if they were already defined earlier?