Add a depthFunction property to materials

How about adding the possibility to set the depth function per material?

Currently, if you want to change it, you need to add a callback on mesh.onBeforeRenderObservable to set the value and on mesh.onAfterRenderObservable to reset the value to what it was before. Also, it allows to change the value on a per mesh basis and not on a per material basis.

As we already have a disableDepthWrite property, I think it would make sense to also have a depthFunction property.

Regarding implementation, I think it would not be complicated and would be much like disableDepthWrite: update Material._afterBind to set the new value (if it is not some default value saying to keep the current in-effect depth function, to not break existing code) and Material.unbind to reset the value to what it was before binding the material.

I can do a PR if it’s ok.


Hi @Evgeni_Popov

I guess @Deltakosh would be very happy to review your PR :slight_smile:

PR created:



Congrats on the core contribution, @Evgeni_Popov! (hi-5). Thanks!


PR to update the Inspector:

Can you show me a use case @Evgeni_Popov sounds like something I want to understand.

Easy: the last 2 playgrounds I did needed to change the depth function to “Always” to render some meshes!


And: Babylon.js Playground

In the first one, instead of doing:

        meshl.onBeforeRenderObservable.add(() => {
        meshl.onAfterRenderObservable.add(() => {

With the change, I will just need to do:

lineMaterial1_hidden.dephFunction = BABYLON.Engine.ALWAYS;
1 Like