Get shadows working with custom shader

I have a custom shader that highlights some parts of a ground (a plane that uses a heigh map), but the problem I have is that I don’t have shadows on it. My current guesses are:

  • I don’t have shadows because those were drawn by a shader and my current shader is replacing them.
  • To fix this I need to add the shadow adding process to my custom shader.

I’ve got some code from some ansers in this forum that I think it could help me, although I’m a bit lost in how to implement it:

customMaterial.setTexture("shadowSampler0", light.getShadowGenerator().getShadowMapForRendering());
customMaterial.setMatrix("lightMatrix0", light.getShadowGenerator().getTransformMatrix());
customMaterial.setVector3("shadowsInfo0", light.getShadowGenerator().getDarkness(), light.getShadowGenerator().getShadowMap().getSize().width, light.getShadowGenerator().bias);

I guess what this does is to set the shadow and light information in the shader. But I’m not sure how to use that information inside the shader (assuming that code above is correct).

Another option, I guess, is just to pick the default shader that I’m guessing babylon uses and customize its behavior so it also adds the highlighting I need. But I’m not sure how to do that or where to find that code, or if the approach makes sense.


What you need is the ShadowDepthWrapper class:


Hi Evgeni. Thanks for your answer. Is this feature currently available or is a version that is not yet released? When I go to the BabylonJS site it says “4.1”. Is 4.2 still in development?

Yes, 4.2 is still in development, the release is scheduled for later this year. However, the alpha in the preview CDN are normally quite stable.

1 Like