How to make fog color transparent

I’m trying to make a guitar hero style game, where the track and notes fades from/to transparency at a distance from the camera, like the image below:

However it seems fog does not support Color4 for alpha control, if I make the scene’s clearColor to transparent and set the fogColor to the same value then everything would just be black within the fog, as shown here

I came across this post which have similar question as mine, however the PG provided by someone at the end isn’t really using fog and requires changing individual mesh, and seems to have some issue with objects at a distance:

Are there any methods to make distant objects fade to transparency using fog color that includes alpha support, or is there a better approach to achieving this effect?

Many thanks!

Welcome to the club! :slight_smile:

Did you give a shader a try?
Alpha Fog | Babylon.js Playground (

1 Like

Focus Distance is nice effect, too to get the meshes blury/foggy:
Babylon.js Playground (

1 Like

Thanks for the fast response!

I have tweaked the shader and added the start and end variable, it works like a charm!

1 Like

However it seems with this postprocess added, every material with alpha value below 1 or with hasAlpha property become completely transparent :cry: in this example the ground plane:

I’m not familiar with glsl, is there a way to incroproate texture’s alpha into the shader code? Thanks!

the “a” in “.rgba” stands for the alpha. You fetch with “rgba” a vector4. it’s the same operation if you like the x and z you would write “xz” and get a Vector2 with (x,z). GLSL is not my level, too. I hope some one else can help you.

1 Like

After digging through the transparent rendering doc and this pg I realized that it is because the depth map from the enableDepthRenderer does not contain meshes with alpha enabled, unless I set the the transparencyMode of the ground material to Material.MATERIAL_ALPHATEST, it works for plain color objects with alpha enabled:

However if the texture is an image with alpha, even with the alphaCutoff set to 0, the texture still won’t display properly with the alpha channel (because of alphatest):

Without the postprocess and with alpha blend this texture should look like this:

PG link:

I have tried other transparency modes but none were working as expected. Is someone who might be familiar with alpha blending that could advise whether there is a way to make the depth map work with the alpha texture? Thanks again for the help :slightly_smiling_face::pray: