How to render specific layers to the shadow map

For my application, I need some fine-grained control over render order for a number of reasons. Thankfully the layers system offers an easy and convenient way to render specific objects in a specific order.

renderer.render(scene, camera);

// Render some objects with depthRead/depthWrite set differently maybe
renderer.render(scene, camera);

// Apply some postprocessing effects to specific objects
renderer.render(scene, camera);

// Render world-space UI after postprocessing
renderer.render(scene, camera);

The above pattern works fine, is flexible, and (I think) is perfectly performant. However, I have found that I need more granular control over shadow map rendering. In the above example, I have an object on layer 1 that needs to show the shadow of an object on layer 3.

To fix the issue, I’d need to render only the shadow map with both layers 1-3 active before layer 1 is rendered ‘for real’. However, I don’t see any way to actually do this.

the function renderer.shadowMap.render() is exposed but calling it directly results in errors as the render state isn’t set up correctly.

Does anyone have any ideas as to how I can achieve what I need here?

Just for clarification: Although the method is exposed, it is for internal use only.

The layers configuration of the camera also determines which objects are rendered to the shadow map. It is not intended to achieve a more fine granular rendering of shadow maps.

In other words, you want a scene object to cast a shadow but be invisible. I suggest you read the following answer at stackoverflow for a possible solution: