needAlphaBlending with ShaderMaterial disabling depth test


I am trying to create a vertex fog with transparent color and wrote a simple custom shader with it. everything seems to work fine accept when it comes to transparency. when I set needAlphaBlending to true, some of the meshes of my opaque models are not depth tested. that’s my observation so far. Also when I set needDepthPrePass to true. It works fine. But my particle systems are not rendering in front in that case :frowning: any other solution for this problem? thanks.

I think we will need a repro in the PG to better understand.

However, when alpha blending is enabled for a material, the corresponding meshes does not write to the depth buffer anymore but they are still depth tested.

1 Like

hey, I will create repro later for sure. is there any performance penalty when I set needDepthPrePass or forceDepthWrite to true? Thanks.

You have a small performance penalty with needDepthPrePass = true because your mesh is drawn two times, a first time only to the depth buffer (with color writing disabled) and a second time it is drawn normally.

There’s no penality with using forceDepthWrite = true.

1 Like

I do not have a repro for now but I will try my best to be clear, here’s the scenario, I have a particle system emitting particles in FRONT of an opaque mesh. I want to have a transparent fog effect on opaque mesh vertices based on camera distance, so I enable alpha blending. It disables depth writing and to encounter this I enable needDepthPrePass/forceDepthWrite to true. Now, when I do that, I see particle effect being obscured by my opaque mesh. WHY? both have same renderingGroupID. :'D Help.

You don’t need to set alpha blending on the opaque mesh but only on the mesh (plane?) that has the fog effect.

By doing so, your opaque meshes won’t be flagged as transparent meshes and everything should work.

Now, if your opaque meshes are displayed on top of the particle systems, that’s because they are flagged as transparent (alpha blended) and so are displayed last, even after particle systems.

See this doc for the rendering order of the different elements in a scene:

1 Like

the game levels are generated at a distance and I need transparent fog for level pieces instead of far plane culling(it doesn’t look professional). is there a hack around this? I understand transparent has to be rendered last and in an sorted order but I don’t have such requirement. and I am not worried about transparency issues. I just want the transparent fog working.

Why not activate the scene fog?

1 Like

I tried that but it just exposes rgb for fogColor. It doesn’t have alpha channel support for the same reason I guess. :’) It’s okay I guess. I will give up on this for now.

You really need to provide a repro because I don’t understand your need…

The fog is blended with the meshes, so it gradually fades to the fog color:

I don’t understand what you want to achieve with the transparency.

so usually In fog basically you can decide the fog color. this is the color we blend from using lerp. But in my case I also want to have the alpha channel. So what It will do is basically transition transparent_fog_color---->original_mesh_color smoothly. The idea is to make mesh appear smoothly.

since its an exceptional case I will post the problem in stack overflow and here as well and try to be more clear. I will see if someone is trying to achieve the same and have the solution for this. Thanks for your responding so quickly. Appreciated.

The idea is to make mesh appear smoothly.

It seems to me that the meshes appear smoothly:

Maybe you will have more luck at the stack overflow site.

hi Evgeni,

sorry for asking this again, so as you remember my problem was that particle system which supposed to render in front was rendering behind my alpha blended mesh. If I enable forceDepthWrite of my alpha blended mesh, it should fix the problem since depth testing will work correctly right?

No, they will still be rendered after the particle system. The rendering order is fixed, as described in Transparent Rendering | Babylon.js Documentation

Use the renderingGroupId property of the particle system to make it render after the alpha blended mesh:

1 Like


so thanks a lot for the link Evgeni. The guy who posted had the same exact problem and he’s using reneringGroupId for particles plus this…

setRenderingAutoClearDepthStencil(particlesRenderingGroupID, false);

and now particles are rendering with depth testing. so just to be clear, This function call with false will reserve the depth buffer for that rendering group id I suppose? Is there any heavy performance penalty involved if I am using this? thanks.

No, the false parameter means the depth and stencil buffers won’t be cleared when starting to render the objects with renderingGroupId = particlesRenderingGroupID. That means those objects will be correctly depth tested with the objects rendered with a lower renderingGroupId.

No, there’s no penalty that I know of.

1 Like