How to fade particles on distance from the camera?

Hello, I should fade (opacity decay) particles on distance from the camera.
I’ve seen this demo to display the depthRenderPass, but it’s actually a bit too advanced for me, expecially to understand how to use it in the particles shader (regular particles, not SP)
https://playground.babylonjs.com/#005S3E#5
Is there a more simple way to get this effect?
P.S. Fog does not seem to work with particles, and it would not be the right solution because I should keep the background visible
Many thanks!

If you are using a node material for your particle shader, you can use the FragCoord and Camera parameters block to compute a linear z value in the 0…1 range that you can use to modulate the alpha property:

https://playground.babylonjs.com/#J9J6CG#18

Note however that there is currently a bug with the Camera parameters block that will be fixed by (the PG above will only work when the PR is merged):

1 Like

Thanks a lot for your reply Evgeni_Popov,
I’m new to BJS and I’ve never used node materials, so please forgive my ignorance.
I’ve updated the “inputBlock.js” like suggested in my NPM , and I’ve copy pasted your code,
but I can’t see this fade effect…
Should your code work out of the box, or should I manipulate somehow the node material?

It will work only when the PR I linked to is merged (which it is) and when the playground is updated (not done yet - should be done in the coming days) / when the NPM packages are updated (if you are using NPM as it seems). NPM packages are recreated infrequently, so you may need to wait 7/8 days before they are updated.

3 Likes