- Home /
Can you manually set the layer order for triangles inside a mesh?
I'm using Puppet2D to convert a Sprite to a mesh, then applying that to my skeleton to animate, but it seems the mesh's triangles are rendering in a random order.
I uploaded a quick 15 second YouTube video to demonstrate what I mean:
Is there a way I can edit the mesh to make the triangles on one leg always appear above or below the triangles of the other leg to avoid the above?
(It also generated a material file too, not sure if that might be the issue rather than the mesh itself)
Just not sure where to look!
Answer by Bunny83 · Jan 07, 2020 at 09:36 AM
Individual triangles of a mesh can not be treated individual. However a shader that essentially use the painters algorithm to draw (this is common for 2d where no depth buffer is used or could not be used) the order of the triangle in the mesh does matter. I don't have Puppet2D and have never used it. So I'm not sure if there might be already a setting in the mesh generation that can fix that. However if you have access to the actual Mesh you could probably change the order of the triangles. For this you just need to know that triangles are rendered in the order they appear in the triangles list. A single triangle consists of 3 indices. Finally things drawn last will be on top if they overlap with other triangles.
If the mesh is generated and serialized as asset, you could of course "edit" the mesh in the editor with a custom script. However such automatic generated meshes could potentially be regenerated by the Puppet2D framework so your custom changes are lost. A slightly better approach might be to create a mask image for your sprite where you use a certain color / alpha value to mark the part that should be on top. Now you can create a script that iterates through all triangles, find the center point of each triangle UVs and look it up in your mask image. Those triangle would need to be moved to the end or the triangle list.
Of course a slightly simpler solution might be to split your sprite into two sprites. However I don't know how well that would work with Puppet2D's mesh generation and rigging.
Answer by thetomlongo · Jan 24, 2020 at 05:27 AM
Thanks for the advice mate, I ended up going with your last solution which is to split it into 2 sprites – one for left leg, one for right. While not ideal as it increases the amount of individual assets I have to keep track of, it gives me the best control.
There was a way to edit the order of the bones in P2D in order to make the auto-generated mesh behave better, but this was still pretty problematic so simplest solution is best!