- Home /

# Determining where a mesh triangle faces

I'm making procedural meshes from random data, which works pretty well apart from some of the triangle faces being the wrong way around. I can obviously invert the order in which they reference their vertices, but so far I have found no working way of determining whether this is necessary for a given triangle.

As others have found out a while ago, the facing of a triangle depends on whether its vertices are named in a clockwise or anticlockwise order. But if I'm working with a bunch of points in 3D space that will, at some points, result in a continuous mesh, then how can I determine that?

How is their being or not being clockwise even decided? I can't even imagine what the mathematics behind it look like, or what kind of coordinate system is used.

Thanks for any help on the issue.

Depending on the nature of the meshes, this might be easy.

A terrain or similar surface requires obvious "up" facing tris.

Certain simple shapes can determine their "out" facing tris by comparing the position of their verts against some simple rule (e.g. a sphere knows all tris face away from the origin).

I have not personally encountered a need for solving face orientation, but certainly some established algorithm exists - take Blender for instance. It can determine the intended face orientation with a fantastic success ratio. It's also open-source if your needs are that dire...

I'd be curious to learn how such a system achieves success, but I don't envy your task of constructing or integrating a solution.

By definition, the order of the vertices in a triangle is clockwise; that's not something that needs to be decided. I think what you mean is that you want to know if the surface normal of the triangle is pointing the way you want.

I read the OP's issue as being unable to determine which of the two given normals should be selected on a face-by-face basis to achieve consistency throughout a complex mesh. The question is pretty nonspecific about the nature of the meshes they're creating.

@Lambastard, if it's just a hull you're after, the best normal of the two would be the one best described as pointing away from the hull's origin, which could serve as your test criteria. Any more complex shapes and I'm fairly clueless.

Well, even most non convex shapes usually have more out facing "surface" area than faces that are pointed against each other. At least if the triangle density is roughly equally distributed.

If you have no clue which side is which, you could raycast from the triangles center along both surface normals against all other triangles. The side which doesn't hit something is most likely "outside". Now for non convex shapes it's possible that a triangle is covered on both sides with other triangles. The direction of those could be determined by "walking" the surface starting with a triangle which is known to face outwards. Each triangle has 3 neighbor triangles so you can jump from triangle to triangle and carry the ouside information with you. The two edges of two triangles always go in the oposite direction if both triangles are connected and represent the same "side".

This of course only works reliably with closed shapes which form a volume without self intersecting triangles. That's also the reason why a lot physics systems prefer or require closed shapes or even convex shapes. Unity is actually quite tolerant when it comes to generating colliders from mesh data.

How perfectly sensible. Thanks for elaborating on this! I feel edified. :)

**Answer** by Eric5h5
·
May 10, 2015 at 01:55 AM

You can determine the surface normal of a triangle by using the three vertices in a cross product. In this case i is the index of the first vertex in the triangle:

```
var surfaceNormal = Vector3.Cross (vertices[i+1]-vertices[i], vertices[i+2]-vertices[i]);
```

So if the surface normal is pointing the wrong way, invert the triangle order.

Thanks for the tip, I'll try to solve the issue this way.

Edit: Yep, worked. Don't know whether it'll suffice for the more complex affairs, but for now it's a good solution. Thanks!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Triangulating a 2D polygon with only hull vertices. 1 Answer

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

Creating a mesh "outline" around gameobject messes up when the gameobject rotates... 3 Answers

Procedural cube code 0 Answers

Procedural Mesh Generation - Split Arrays into sections 1 Answer