Make 2D shadows respect z_index
#93881
Open
+188
−81
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the ability to sort 2D light occluders using their z_index. Fixes #64939. Now, light occluders only cast shadows on objects that have a lower or equal z_index. I had written another PR that did the same as this one a while ago (#86689), but I didn't really like the approach I took since it was on the GPU side. It also now works on directional lights and on both forward+ and mobile renderers.
Old shadows on the left, new ones on the right:
This approach doesn't modify any shaders or low-level graphics code, and instead uses only the CPU. This is achieved by calculating the shadow map inside
RendererCanvasRenderRD
instead ofRendererViewport
. The shadow map gets recalculated if the lowest z_index of all active occluders is smaller than the current object's z_index, since the occluder then has to be removed from the shadow map.Performance-wise, in the best-case scenario where only one z_layer is used for all objects, the performance is the same since the shadow map is only calculated once. In the worst-case scenario, the shadow map gets calculated as many times as there are z_layers occupied by occluders. This shouldn't be a problem because usually you only need a few z_layers for occluders.
I hope that this PR can get merged since I feel like 2D shadows are currently unusable. This is still my first time contributing, so please let me know if I need to change or add anything!