# Sort alphaIndex of each face individually

**URL:** <https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675>\
**Category:** Questions\
**Created:** [September 8, 2021, 3:53pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675 "2021-09-08T15:53:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhn11](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jhn11](https://forum.babylonjs.com/u/jhn11)\
**Post date:** [September 8, 2021, 3:53pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/1 "2021-09-08T15:53:22Z")

</div>

Hello again amazing community,

I have this issue that I have managed to reproduce in the PG:  
[https://playground.babylonjs.com/#YIU90M#479](https://playground.babylonjs.com/#YIU90M#479)

If I rotate the model around, the alphaIndex (not sure how to call it) on the meshes change as I move around.  
What I’m trying to achieve is, as I look from one side to the other, to see everything in the correct order, without having these artifacts.

What I have here is 3 meshes, cylindrical in shape.

My configuration looks like this:

 ![top-down](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/9/e9114cbb7e7cc04eb835a9c7a8e5e35717634bab.jpeg)  
outside is red, cup is black and underprint is inside.

Looking from the side for example, I want to sort the meshes this way:

1. red
2. black
3. green
4. green
5. black
6. red

But because 1 = 6, and 2 = 3, I can’t manually assign alphaIndex values to them, as 6 would be rendered in front or 5, 5 in front of 4 and so on.

My questions are:

1. Am I doing something wrong here? Is there something obvious that I’m not seeing?
2. Is it possible to sort depth for each individual face? I know of [Facet Depth Sorting](https://doc.babylonjs.com/divingDeeper/mesh/facetData#facet-depth-sort), I have tried to apply it, but the meshes must be updatable (as far as I know), and I’m not sure that’s possible with imported meshes (or is it?).

Thanks,  
jhn11

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [September 8, 2021, 4:45pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/2 "2021-09-08T16:45:43Z")

</div>

Hi there !  
I haven’t worked with this specific case, but it seems right up the alley of PBR materials. What I would try to do is use **one single mesh** for you glass instead of three, and create a **glass material with apparent backfaces** (it would probably look more realistic as well !). I can’t fix it in your playground right now, but @PatrickRyan has made an awesome tutorial on babylon’s youtube channel, explaining how to achieve this using the [node material editor](https://nodematerial-editor.babylonjs.com/) :

[![](https://img.youtube.com/vi/q-fCBSfRqPI/maxresdefault.jpg "PBR Nodes in Node Materials Part 4") ](https://www.youtube.com/watch?v=q-fCBSfRqPI)

If you’re having trouble with it, I’ll be glad to look further into and try to reproduce the effect on your glass over the weekend. 🙂

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 8, 2021, 4:53pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/3 "2021-09-08T16:53:45Z")

</div>

I would advice in your case to split backface and front face meshes so you ll have 6 meshes you can order 🙂

---

<div class="post-metadata">

**Author:** ![PatrickRyan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/patrickryan/32/3417_2.png) [@PatrickRyan](https://forum.babylonjs.com/u/PatrickRyan)\
**Post date:** [September 8, 2021, 5:33pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/4 "2021-09-08T17:33:13Z")

</div>

@jhn11, there are a few things you can do here to render correctly. There are two threads that can help you with this. The first is [one that dealt directly with a drinking glass](https://forum.babylonjs.com/t/the-problems-with-tangent-space/21886/6) where one of the best solutions was to split the inner faces from the exterior faces which allows you to make use of backface culling to manage some of the sorting issues.

Another thing that can help you is to handle the decal on the glass in the shader instead of with another mesh. There was [a thread about this question in applying a decal to a skateboard deck](https://forum.babylonjs.com/t/how-to-position-and-size-a-decal-to-1-side-of-a-mesh/23141) so that you can see the wood grain beneath a decal that can be swapped on the fly.

Another example of something like this is [the soda bottle demo](https://playground.babylonjs.com/#P1RZV0) we created for the last release which also uses a label on glass. In this example, the label uses alpha and transmission values from texture to create the opacity for the label separate from the refraction of the glass.

Let me know if you have any questions about any of these methods, but I think a combination of these examples should get you rolling in the right direction.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [September 8, 2021, 8:24pm UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/5 "2021-09-08T20:24:15Z")

</div>

Once [this PR](https://github.com/BabylonJS/Babylon.js/pull/10659) is merged, you will simply have to enable OIT to have the objects rendered correctly 🙂

---

<div class="post-metadata">

**Author:** ![jhn11](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@jhn11](https://forum.babylonjs.com/u/jhn11)\
**Post date:** [September 9, 2021, 6:57am UTC](https://forum.babylonjs.com/t/sort-alphaindex-of-each-face-individually/23675/6 "2021-09-09T06:57:10Z")

</div>

I love all of the solutions presented.

I will go through each one individually and see what works best for me and I will eventually post results once it’s finished…or further questions if I have any 🙂

Thank you all, I really appreciate it!
