Change material if in shadow, like in the gif animation

Shadow pass switch is irrelevant for your use case. It changes the shadow, not things that are in shadow.

The only way I can think of to generate a shadow map before the base pass without changing engine code would be to use an orthographic scene capture component to capture a depth buffer of the scene from the angle of the sun to a render target and make the shadow map yourself from scratch using it. You would need to understand how shadow mapping works fully. But yes it would then be possible to sample it as a texture.

If it’s totally static, you only need to test the ray when they object is added to the scene, or when the light moves. If neither ever move, there is no runtime cost to just check it with a ray at build time. If the sun moves, every single one would need to re-test and it could be very expensive but the only way to say how expensive is to test it. No one is going to be able to give you a number.