Move shadow without moving mesh

Hi, I have a ground mesh on which some plane meshes are moving around with a (sprite) texture on them. Because the textures have some depth drawn on them (e.g. one foot of the drawn character is closer to the camera than the other) I’m using the renderingGroupId to draw the planes at the correct height relative to their position.

I’d like to have the planes cast a shadow, but since they are actually a bit below the ground they are casting a shadow on, the shadow is cut off.

See for example: [https://playground.babylonjs.com/#4G38H4#1946]

Is it possible to draw the shadow as if the plane was at the actual y-pos of the ground and slightly closer to the camera than it actually is (so it pretty much connects with the texture)? Or is there some other way to achieve this effect?

You can use the onBeforeRenderObservable / onAfterRenderObservable mesh observables to set the plane position. Use the former to set the position when rendering the plane to the screen, and the latter to set the position when rendering to the shadow map:

Note that these observables are also called when the mesh is rendered in the shadow map, but updating the mesh position there has no effect on the shadow map generation because the world matrix is already set on the shadow map shader.

3 Likes

That works great, thanks!