Multiple Stencil Operations Based on Facing (Shadow Volume Problems)

I’m trying to implement volumetric shadows so as to avoid some of the shadow artefacts I’ve been getting.

As you’re possibly aware I need to update the stencil buffer based on depth pass or fail, incrementing or decrementing based on the facing and what happened.

I have a few questions:

  1. Can I combined results in the same shader, i.e. like below:
{
  Ref 1
  CompFront Always
  CompBack Always
  PassFront IncrSat
  PassBack DecrSat
}
  1. Given the facing is in play with PassFront, PassBack, are these inferred with the winding order of the triangle or is this taken from any supplied vertex normal? Currently I am not passing any normals because I presently don’t need them… or do I?
  2. Finally, with Renderer Features, are they executed in order? i.e. if I have 2 features do the execute top down always?

I am asking this because I don’t have success atm with this in a single pass. I am using URP and Renderer Features to Render my shadow volumes, but despite the volume appearing to look correct AND appearing to respect Cull Front or Cull Back, the shadow volume sometimes occludes things behind it that do not lie within the volume.

Here a debug volume is rendered and looks correct.

Here I am rendering the shadow volume, but the RHS of the volume is shadowing too much of the ground plane and the top left edge is generating an extrusion that is occluding areas of the scene it should not.

But from this side it looks pretty good:

It feels like the triangle winding order may be incorrect in some way but all of my attempts to determine this seem to prove that they are fine.

Any answers and any suggestions would be most welcome - I feel like I’m 90% of the way there but I’ve been faffing about trying and debugging for hours now.

Cheers
Ian

i was involved in some threads about this before as i have some knowledge, they would be a good place to start
https://discussions.unity.com/t/silhouette-edge-detection-for-shadow-volumes/942752

https://discussions.unity.com/t/issue-with-geometry-shader-clipping/943922

https://discussions.unity.com/t/how-to-visualize-stencil-buffer-in-texture/946398

Thank you. I actually think I just asked a question on one of your threads, ironically!

I think some of the above is my volume had duplicate triangles but I have fixed this. Still cannot get URP to play nice with PassFront and PassBack set in the same shader pass.

I will read over the threads your bring up and see if I can learn/understand them! TY

1 Like