0

With a given mesh at a given position, is there an fast and easy way (with vertexShader, fragmentShader or something else) to identify/remove from the mesh all the faces/vertices that are not rendered on the camera screen (hidden behind other faces, outside the view fustrum, etc.)?

Minichua
  • 185
  • 14

1 Answers1

3

If you mean remove in terms of not rendering them this is accomplished by having backface culling enabled(as it is by default).

Detecting Backfaces on the CPU

If you want to remove them from the geometry itself you would need to iterate over the faces and calculate the dot product between the face normal and your lookat/camera direction vector, then compose new geometry buffers containing only the faces where the dot product was positive.

Detecting Occluded Faces on the CPU

Detecting hidden/occluded faces could be done by iterating over the faces and building three planes using the triangle edges extruded along the lookat vector by the far clipping distance, and use the flipped normal of the face itself the 'front plane'. By that you would get an open ended prism type of shape. Then use that volume(its technically not a volume as its not closed) to cull the vertices of the remaining faces, when all vertices of a face lie whithin all planes(assuming your plane normals point inside) the face in question is occluded and can be removed.

Even when using acceleration structures like a Bounding Volume Hierarchy this will not be realtime compatible.

Detecting Occluded Faces on the GPU/CPU

Another easier and probably faster approach would be to assign a unique color representing an ID to each face, render the geometry to a framebuffer, iterating over that pixel data, bitshifting the colors back into their respective ID and build a list of visible faces that way.

Further Information

While its usually not done on a geometry level read the wikipedia article on occlusion culling for further information.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35
  • I was more looking for the second option. My concern is about hidden faces. On the same mesh, if one face is place "behind" another one but looking toward the camera, I suppose the dot product will be positive but the face should not be rendered. Is that right? – Minichua Jan 09 '15 at 17:08
  • Yes you're right, the dot product would be positive even though the face would not be visible. What you want is called `occlusion culling`. I've edited my answer and added two approaches to detect occluded faces aswell as a link with further information on the topic. However without knowing your use case its hard to advise on a good approach. – LJᛃ Jan 10 '15 at 00:58
  • My case is a 3D paint editor. In this editor, I need to be able to project images on a mesh. There already are example of projection in Three.js but they u – Minichua Jan 10 '15 at 15:38
  • My case is a 3D paint editor. In this editor, I want to be able to project images on a mesh. I have seen examples (decals) of projection in Three.js but they are based on 3D rectangular prisms. And the projection is done on all vertex in the cubes, even those you can not 'see'. I was thinking of "cleaning" the mesh before doing the projection, so that only the significant faces are used. By the way, if you know examples of projective texturing (webgl/Threejs) where the original texture is modified and can be recovered for further use. I find it quite hard right now. – Minichua Jan 10 '15 at 15:46
  • I'm not a threejs guy so I cant help you with examples. Projecting the "paint" texture back to the original texture should just be a matter of calculating the perspective transform onto the individual face UVs, similar to how lightmapping is done. This should be in another question though as you asked for occlusion culling/hidden face removal, not UV texture projection. So accept my answer and when the need arises create a new question. – LJᛃ Jan 10 '15 at 23:47
  • @Yokotsumo Depth testing should take care of the occluded faces on the GPU anyway, and is likely the most efficient way to handle this. I would not worry about this until you are further along and trying to optimize. Then a quick optimization that might be all you need is to sort your meshes from closest to farthest before drawing them. – Tenfour04 Jan 12 '15 at 14:00