Sorting of mesh with vertex colors and custom shader

Hello all,

I’m having a mesh with custom vertex colors which I use for another purpose than my question/problem.

It seems that the sorting of the mesh is incorrect. I think it is due to the use of vertex colors, which come with the exported model file.

This is the material setup to illustrate the issue:

[Edit]: I just notice that the mesh doesn’t stay in the material editor once the link has been made, I’ll upload the mesh separately
[Edit #2]: I can’t seem to upload a file as attachement yet, since I’m a new user :confused:

Is there a way I can force the material to not use any alpha blend mode, so it renders opaque always?


Welcome aboard!

It will be easier to understand the problem and help you if you can setup a repro in the playground. Here’s a page that will help you using an external asset in the Playground: Using External Assets In the Playground | Babylon.js Documentation

You can try something like yourMaterial.needAlphaBlending = () => false;.

1 Like

Thank you for your reply!

I did some further digging. It seems that it really has to do with using vertex colors or not.


Both the meshes are the same other than the exported vertex colors. You can see that the sorting goes bad on one of them, not on the other. Maybe it is a Babylon bug?

I’m using this as a test material to show the problem:

material = new BABYLON.StandardMaterial("", this.scene);
material.disableLighting = true;

var matCapTexture = new BABYLON.Texture("", this.scene);
matCapTexture.coordinatesMode = BABYLON.Texture.SPHERICAL_MODE;

material.reflectionTexture = matCapTexture;
material.needAlphaBlending = () => false;

This shows the vertex colors I’m using:


I use the vertex colors as a mask to resize the mesh on specific width/height etc

I also tried disabling the use of vertex colors (to test/debug) using:

mesh.useVertexColors = false;

This didn’t seem to affect anything.

An other option I have is to create an external data file to describe which vertices are having which ‘color’ or ‘data’ so to speak. So the meshes won’t have any vertex colors. But that is a bit of a hassle of course.

Thank you again!

The bad mesh is considered as transparent, so it won’t write to the depth buffer and the faces will be displayed in a “random order” (actually, in the order they are defined in the mesh geometry).

Make sure that mesh.hasVertexAlpha == false and mesh.visibility == 1, else the mesh will be considered to have transparency.

1 Like

This fixes it indeed!

mesh.hasVertexAlpha = false;
mesh.visibility = 1;

Thank you so much, this had me debugging for ages!