Determine whether or not model is "inside out"

Pretty simple question... sometimes (and seemingly at random), my procedural meshes will be inside out. I've tried to figure out under what cases this is true, but it seems to be at random, and I have no idea why it's happening.

What will happen is... the faces will be facing the correct direction, but the stitching on the sides that I do by hand will sometimes be inverted (with the normals facing inward, not outward as they should be).

So my question is... is there some script that can automatically make all normals face outward? Or what would cause this behavior to happen at random? Remember it's not the entire model, just the sides.

I know that this can often be caused by writing the triangles in reverse order to the triangle, but I can't figure out why/when this occurs, so I can't just reverse the triangles.

Any help on this would be appreciated, thanks.

EDIT: So the problem is that my mesh faces always point in one direction, so if the user creates an object "in reverse" (for example, drawing counter-clockwise instead of clockwise), the mesh faces will be facing the wrong way. How can I tell if my object is drawn in reverse, or if my mesh faces are facing the wrong direction?

I've done a fair amount of procedural mesh building in Unity, and I've never seen inconsistent results as to which side the triangles come out. It seems to me to be completely deterministic and predictable - so unfortunately I think the problem must come down to an error in your mesh building code!

As far as I'm aware, there are only two determining factors as to which side of a triangle is visible:

  • The vertex winding order. If the triangle is defined clockwise, one side is visible; anticlockwise, and the other side is visible.

  • The shader's "Cull setting". This can be set to "Back", "Front" or "Off. The default is "Back" (meaning any back-facing tris are not drawn). This would affect every tri drawn by the shader, not just a few.

It's a common misconception that the normals affect which side of a triangle is visible. This is not the case - the normals only affect how the material responds to light. If the normal is pointing in the opposite direction to the triangle's face direction, it will be lit as if it were facing away, but it will still be drawn front-facing. I think this misconception stems from 3D editing apps, where they often merge the concept of normals and face-winding order into a single feature.

This might be useful: Determining whether a polygon is clockwise.... It includes simple C source code. It also assumes the polygon doesn't have holes and isn't self-intersecting - not sure what your drawing algorithm allows.