Disabling a layer, but still cast shadows

I need to render a display layer to a texture, but I need objects from other layers to still cast shadows. Setting the camera’s culling mask completely disables the other objects, so I lose shadows on the objects I’m rendering.

The only way I’ve found to do this is to iterate through every object in the scene that isn’t in my layer, set shadowCastingMode to ShadowsOnly before I call Render, then set it back, but that’s slow even in simple scenes. It can take longer than the actual render.

Shader replacement with a shader that only casts shadows almost does it, but that would require modifying every other shader in the game to add a subshader, which would be way too invasive. (Shader replacement is very limited…)

Is there a good way to do this?

I’d go with replacement shaders. Or the lazy way, make a copy of the scene. (With only 1 material, all collapsed together into 1 model.)

+1 for a replacement shader for me, but I guess the optimised level would be faster if draw calls are an issue :slight_smile:

This is for an effect asset, so expecting people to add a replacement subshader to every shader in their game for one isolated effect won’t really work. If there was a way to say “replace all shaders with tag X, and use shader Y if it doesn’t have the tag” it would work, but replacement shaders only let you say “replace all shaders with tag X, and don’t render at all if it doesn’t have the tag”.

Unless there’s something in replacement shaders I don’t know about, or some way to add global “fallback replacement shaders” automatically…

Object > Mesh Renderer > Lighthing > Cast Shadows > Choose Shadow Only
Been searching for 2 hours and i have found solution cliclking every mesh renderer setting randomly lul

3 Likes

OMG, thanks for that!
Definitely, the easiest way I found so far!