[SOLVED] need help with a Directional Light Shadow rendering error

[EDIT:
SOLUTION: add Fallback “Diffuse” at the end of the shader
Resolved by bgolus
/EDIT]

Hey everyone, I’ve looked all over the forum for an answer to this issue and couldn’t find anything, so I’m posting a new thread.

This is the issue I have, for some reason the shadow from the only Directional in the scene is rendering over the spaceship, even though the spaceship is nowhere near the object that’s casting the shadow. Here it is in action (gifv).

I have absolutely no idea what can be causing this, I’ve tried all sorts of changes in the Directional Light, in the Camera Clipping planes, Quality settings, the Cull in the shaders… no idea what else to try.

Some details:

I’m using Forward Rendering mode (I need it on Forward, switching to Deferred at this point wrecks my project).

The problem seems to go away if i put Legacy Deferred Rendering Path in the Player settings, but I will probably need Reflection probes later on, so I’d prefer to stay on Forward if possible.

I’ve been reading the Unity docs on Forward and Legacy Deferred but it’s a bit over my head, I don’t really understand what would cause the shadow to render over an object regardless of it’s position. It has to be something between the camera and the object, right?

Any ideas? Thanks in advance!

Is your ship’s material using a custom shader with no ShadowCaster pass? Even if an object doesn’t cast shadows, it needs a ShadowCaster pass to receive shadows. In Unity 4 shaders had separate shadow caster and shadow receiver passes, and in Unity 5+ they’ve been merged into a single ShadowCaster pass in the shader file.

The easiest solution is to have a Fallback "Diffuse" near the end of the shader file or alternatively for surace shaders on the #pragma surface line include addshadow.

Thank you!! Man, I would have never figured that one out!

It’s indeed a custom shader (a very customized offshoot of the legacy Self-Illuminated Bumped Specular), and that “addshadow” in the #pragma surface instantly fixed the problem! Now the ship is affected by shadows and casts shadows correctly, here’s a shot now:

3037805--227387--Surf.jpg

Thanks again bgolus, will update all my surface shaders!

BTW, it’s more efficient to use Fallback "Diffuse" instead of addshadow even for surface shaders. As long as your surface shader doesn’t use alpha test or modify the vertex positions, the Fallback method will work fine as no custom shadowcaster pass is needed, where as addshadow will respect modifications you’re doing.

1 Like

Oh, now that you mention it, I see that I’ve created the problem myself!

A while ago I went through all the shaders and copied the Legacy Unity Builtin shaders in a folder in the project (because I wanted to have access to the code and try to put GPU instancing in all of them) and set all the fallbacks to those shaders in that folder. For some reason, even though the fallback was set to a copy of the internal Unity Diffuse, it doesn’t work (at least the shadow issue is still there).

Now I’ve set the Fallback "Diffuse" and it works properly, thanks!