Forward Renderer problem - flickering shadows depending on camera view

Hello,

I’m trying to write a shader based on the unity’s built-in TreeSoftOcclusionLeaves shader.

I have almost gotten it right except for implementing the directional shadows.
My project is specifically restricted to 1 pixel light per scene, with distance shadowmask lightmapping, so the sun is the only realtime non-baked shadow-casting light in the scene.

I flew by this problem a lot trying to implement lighting and shadowing in forward rendering shaders. I cannot seem to find any documentation on it.

I use SHADOW_COORDS macro in v2f struct,
TRANSFER_SHADOW at the end of vertex shader,
and LIGHT_ATTENUATION to get the shadow value in pixel shader.

To clarify the problem, I’ve returned only the calculated LIGHT_ATTENUATION out of the tree canopy shader.
In most cases, it looks fine:

But when I move the camera around (positions and view directions are always the same when this happens):

I have verified via Frame Debugger that shadows are indeed drawn correctly in the shadow buffer, always. But for some reason, LIGHT_ATTENUATION doesn’t return correct values when camera is facing the wrong way.

The entire shader is here:

You can easily try it by creating a material from it and assigning it to standard asset’s Alder tree model’s canopy (leaves part).

  • EDIT - You can also download the Unity package from the attachments. It contains a tree prefab with the shader already good to go.

What I’ve tried:

  • Disabling instancing
  • Changing given UV index to SHADOW_COORD
  • Removing all the vertex animation from it
    Nothing worked so far.

Any insight to this? I’d much appreciate it!

3485291–277360–tree_shader.unitypackage (1020 KB)

Of course, after 2 days of frustrating myself, I decide to ask the forums for help, and only then do I make a breakthrough.

The bug appears to be related to Render Queue… of all things. The temporary “fix” for the shader so far appears to be:

  • Completely remove LightMode tag (“ForwardBase” breaks the shader in INCREDIBLE way)
  • Set the render queue to AlphaTest-100, or Geometry even. This appears to fix the problem and shadow flickering does not happen anymore except when trees are at the very edge of the last shadow cascade.
1 Like

You saved my life. Thank you :slight_smile: