# Inconsistent face visibility even though normals are facing the same direction

**URL:** <https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008>\
**Category:** Questions\
**Created:** [April 15, 2020, 7:51am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008 "2020-04-15T07:51:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 15, 2020, 7:51am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/1 "2020-04-15T07:51:44Z")

</div>

Hello Babylonians!

So i am working on this triangulated mesh, triangulated with an [delaunay algorithm implementation](https://www.npmjs.com/package/delaunay):

 ![wireframe](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/1468f3c361eddf7a09b02635bf057a131cd9f7f1.jpeg)

My issue is now that the faces ‘face’ in different directions.  
Here a view w/o wireframe from the top:

 ![top](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/5/55cde31312555c9a613d65fc7c17c2ab2ab957f5.jpeg)  
Here a view from the bottom:  
 ![bottom](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/33a6198b08c7c3b00faaba61ba3272f25af8dcd4.jpeg)

The thing is: I am not creating normals with [VertexData.ComputeNormals](https://doc.babylonjs.com/api/classes/babylon.vertexdata#computenormals) but by creating a buffer with 0.0, 1.0, 0.0 x the amount of vertices i have.  
So i guess my normals are fine. I am wondering if it might have to do with the cw or ccw definition of the triangles in the indices buffer (this is the next thing i am gonna check). Even though i am wondering if cw/ccw triangles and normals may interfere somehow?  
And i know i could use backfaceculling = true but that wouldn’t work when texturing i guess. And it is a hacky solution in my opinion.

Btw. Long time no see! I am very happy to work with Babylon.js again. Hope you all are doing well…  
Greetings from Thuringia, Germany.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [April 15, 2020, 9:01am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/2 "2020-04-15T09:01:43Z")

</div>

Welcome back. CW and CCW difference is the problem, in the following PG facet 0, 1, 2 is CCW and 3, 4, 5 CW, both have normals in same direction.

[https://www.babylonjs-playground.com/#VKBJN#508](https://www.babylonjs-playground.com/#VKBJN#508)

---

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 15, 2020, 9:10am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/3 "2020-04-15T09:10:55Z")

</div>

Before i (try to) implement a solution. Is there some function to recompute index order based on normals?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [April 15, 2020, 9:15am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/4 "2020-04-15T09:15:46Z")

</div>

Not that I know of, you would need to create your own.

Did you know about [Use PolygonMeshBuilder - Babylon.js Documentation](https://doc.babylonjs.com/how_to/polygonmeshbuilder) or even [Create Parametric Shapes - Babylon.js Documentation](https://doc.babylonjs.com/how_to/parametric_shapes#non-regular-polygon)

---

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 15, 2020, 10:26am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/5 "2020-04-15T10:26:26Z")

</div>

Yes i know about those. I used those in an earlier version of the project.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [April 15, 2020, 1:20pm UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/6 "2020-04-15T13:20:10Z")

</div>

Are you happy to create a function that would recompute index order based on normals?

---

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 15, 2020, 1:48pm UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/7 "2020-04-15T13:48:24Z")

</div>

I guess that might be a rabbit hole. Changing from ccw to cw is easy. You just need to swap a pair of indices. But how do i find out that an index order ist ccw?!

I will try out earcut instead of the other triangulation algorithm to see if its results have no need for recomputing index order.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [April 15, 2020, 3:23pm UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/8 "2020-04-15T15:23:30Z")

</div>

[https://www.babylonjs-playground.com/#VKBJN#509](https://www.babylonjs-playground.com/#VKBJN#509)

Above PG will recalculate index order if needed. It is based on all vertices of all triangles having the same normal (0, 0, -1), some tweaking needed if the normals are in a different direction

---

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 16, 2020, 6:38am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/9 "2020-04-16T06:38:24Z")

</div>

Thanks John! In my implementation i used normal.equalsWithEpsilon(Vector3.Down()) ([see API](https://doc.babylonjs.com/api/classes/babylon.vector3#equalswithepsilon)). To see which indices are the ones being ordered ccw. But i may could also use if(normal.equalsWithEpsilon(Vector3.Up()) == false).

One new thing ocurred that i don’t understand: some of my normals are (0,0,0). How could that happen?

I made a [playground](https://playground.babylonjs.com/#92YIVJ#14) Version #14 is the “default” scene without any index reordering.

Update on [playground version 32](https://playground.babylonjs.com/#92YIVJ#32)  
I added the code to compute the normals and there are no 0,0,0 normals (check the console, i console.log() the values). Weird.  
Update: This issue is resolved!! 🤪

Update on [playground 52](https://playground.babylonjs.com/#92YIVJ#52):  
Determine wether facet is cw or ccw based on the normal. I realize that the facets that i am looking at from above are the facets that are ccw. After the reordering you don’t see anything because all facets are facing to negative y.

Update on [playground 53](https://playground.babylonjs.com/#92YIVJ#53):  
So if i am reordering facets that are similar to Vector3.Up() then i see my mesh from above. That is a little confusing because in my mind it should be exactly the opposite. I must have some knot in my head.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [April 16, 2020, 8:46am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/10 "2020-04-16T08:46:25Z")

</div>

> [@mamu](#):
>
> That is a little confusing because in my mind it should be exactly the opposite.

Swap the cross product over [https://playground.babylonjs.com/#92YIVJ#56](https://playground.babylonjs.com/#92YIVJ#56). Since the cross product is non commutative order of vectors matter.

Note I changed to an ArcRotateCamera because I like to see all around the mesh easily during construction.

Also note from my PG [https://www.babylonjs-playground.com/#VKBJN#509](https://www.babylonjs-playground.com/#VKBJN#509) that I declared my vectors prior to using them in a loop and then calculated their values using `set`, `subtractToRef` and `CrossToRef`. By reusing the same variables I cut down on how often Garbage Collection is called.

Could not see where you got (0, 0, 0) for any normals so cannot comment on this.

---

<div class="post-metadata">

**Author:** ![mamu](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@mamu](https://forum.babylonjs.com/u/mamu)\
**Post date:** [April 16, 2020, 8:56am UTC](https://forum.babylonjs.com/t/inconsistent-face-visibility-even-though-normals-are-facing-the-same-direction/10008/11 "2020-04-16T08:56:32Z")

</div>

Ah okay. I used the equation from [https://gamedev.stackexchange.com/a/152993](https://gamedev.stackexchange.com/a/152993). Does he assume some other coordiante system?

Will keep in mind the ArcRotateCamera for future PGs of that manner 😉

Ah thanks for noting that. I was wondering why you wrote your code like this. Now it makes totally sense. I will apply that to my implementation. Surely there are some other places i can apply this to.

Anyways, i mark your last answer as a solution to my post, since all issues are resolved. Thanks a lot John!
