Detecting mesh orientation

Hey,

I’m creating a mesh using the process described here: http://unity3d.com/support/documentation/ScriptReference/Mesh.html

How can I detect if the mesh is facing the camera?

Thanks

well basically you go

Vector3.Cross( c-a, b-a )

those are very simply the POSITIONS of the three vertices

it’s that easy really.

NOTE you must look up on wikipedia the infamous “finger finger thumb” diagram which purports to show the chirality of this universe.

NOTE when you write this routine I urge you to add a line of code Debug.LineDraw which shows the triangle-normal you have calculated.

NOTE it is literally impossible to remember whether it is c-aXb-a or whether it is b-aXc-a. One will be pointing out the arse of the triangle, and one will be pointing out the face. You simply have to test it both ways in your code to see which is correct.

NOTE of course once you have this “which way is the face pointing” information, you just need to check whehter it is pointing at the camera or not. (Or in your game, it may have to be “within 30 degrees of the eyeline view”, or whatever your situation is.)

NOTE usefully, you can apply this process to whole conceptual objects, not just mesh tris. So, you have a battleship rocking in the ocean. Which the hell way is up pointing at the moment? For your “abc”, use something like the fore-gun on the deck, the flag on the deck, and the side-gun on the deck … ie, three of your “things” on the deck in a triangle shape. Figure it out as in Note3, test it as in Note2, and you have a often very useful “up” arrow. The same could apply to your conceptual “faces” you mention here.

So it’s one line of code thank goodness, hope it helps !!!

{All this makes me think though – isn’t there some smart-arse way to do this using the 3D pipeline? After all, the renderer (or … something) knows if the tri is facing the camera or not. An issue for another day.}


Drawing of the INCREDIBLE trick described by Wolfram:

YOUR FINGERS JUST FOLLOW THE TRIANGLE SPINWARDS OMG YOUR THUMB IS THE UP

First you would have to determine what is “front” for your mesh. A general mesh can be anything - planar, round, closed, concave, self-intersecting, …

You can determine if an individual triangle is facing the camera, using standard vector arithmetics, by computing the face normal of that triangle (the provided vertex normal will not help you, it has nothing to do with frontfacing/backfacing), then use the dot product with the direction to the camera (search the web for the detailed process).

So either you do this for all triangles in your mesh, or you determine a general “front” direction (e.g, the transform.forward of that object), and then test this using the dot product as above.