Hey guys,
I am trying to understand what controls which side of a BufferedGeometry polygon is visible and which isn’t (i.e. which side is considered ‘front’ and which one is ‘back’). Please consider the following simple indexed BufferGeometry that creates a simple square made of 2 triangles.
const geometry = new THREE.BufferGeometry();
const vertices = [
{ pos: [-1, -1, 1], norm: [0, 0, 1], uv: [0, 0], },
{ pos: [1, -1, 1], norm: [0, 0, 1], uv: [1, 0], },
{ pos: [-1, 1, 1], norm: [0, 0, 1], uv: [0, 1], },
{ pos: [1, 1, 1], norm: [0, 0, 1], uv: [1, 1], },
];
const positionNumComponents = 3;
const normalNumComponents = 3;
const uvNumComponents = 2;
const positions = new Float32Array(vertices.length * positionNumComponents);
const normals = new Float32Array(vertices.length * normalNumComponents);
const uvs = new Float32Array(vertices.length * uvNumComponents);
let posIdx = 0;
let nrmIdx = 0;
let uvIdx = 0;
for (const vertex of vertices) {
positions.set(vertex.pos, posIdx);
normals.set(vertex.norm, nrmIdx);
uvs.set(vertex.uv, uvIdx);
posIdx += positionNumComponents;
nrmIdx += normalNumComponents;
uvIdx += uvNumComponents;
}
geometry.setAttribute('position', new THREE.BufferAttribute(positions, positionNumComponents));
geometry.setAttribute('normal', new THREE.BufferAttribute(normals, normalNumComponents));
geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, uvNumComponents));
geometry.setIndex([
0, 1, 2, // first triangle
2, 1, 3 // second triangle
]);
Now, it was my understanding that the vertex normals are used to interpolate the face normals and those face normals are then used when drawing the polygons on screen to determine which side should be drawn. So I expected that if I flip the normals from (0,0,1) to (0.0,-1), the polygon would face the opposite direction and I will not see it (since I have the side set to THREE.FrontSide) . Turns out that is not the case and I can remove the normals attribute altogether without any visible effect or error.
Then I tried flipping the vertex indices and I realized that if I created the second triangle counter-clockwise like so
geometry.setIndex([
0, 1, 2, // first triangle
2, 3, 1 // instead of 2,1,3
]);
this will actually flip the face so that the camera would now be facing the back side of the second triangle.
Could someone please tell me if changing the vertex index order is what should be used to control which polygon side is “front” and which side is “back” or is there a different/recommended way to achieve this?
Thanks in advance for the help!