Is it possible to rotate a mesh differently for shadow rendering and regular rendering?

I have meshes that are effectively 2D sprites, which I rotate to face the camera before each render (similar to billboarding). When shadows are rendered, I’d like these meshes to face directly towards the light so that they cast a consistent shadow.

Is there an event I could hook that fires between shadow rendering and regular rendering, such that I could give sprite meshes a different rotation for the two phases? Or is the effect I want impossible to achieve without creating “shadow impostors” (i.e. duplicate shadow-only meshes for every sprite that needs to cast a shadow)?

Not sure if this works but maybe a hint, use setmaterialforrendering for the depthmap, and deal with the rotation in the vertex shader of a shadermaterial.

1 Like

Sorry, I don’t follow. Do you mean that it’s possible to specify a separate material for the shadow render, but not anything else, and there aren’t any events that fire between the depth render and normal render?

You do have an event which is raised before a mesh is rendered into the shadow map: shadowGenerator.onBeforeShadowMapRenderMeshObservable. And you also have the counterpart onAfterShadowMapRenderMeshObservable.

However, the onBefore event comes too late in the process, the world matrix has already been retrieved/set.

I see - so is it correct to assume that the only options are to make duplicate meshes or write your own shader?

Yes, I think these are the only solutions at the moment.


I don’t know if I can suggest features here, but it seems like a better solution for this would serve lots of use cases. Even without the sprite/billboard angle, one might want a given mesh’s shadow to be a different scale than the base mesh, for example.

1 Like

I don’t think we can provide a generic solution, but you can do it with a custom shader and a shadow depth wrapper:

Note that I don’t know if it’s faster than simply having a copy of the plane for shadow only rendering, as we have to recompute the world matrix two times for each tree with the method above…


Thank you for the demo Evgeni! In my case I think it will be simpler to create duplicate meshes, but the example code was very helpful.