Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 2D shadows respect z_index #93881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DanielKauss
Copy link

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:
LightComp

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 of RendererViewport. 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!

@DanielKauss DanielKauss requested review from a team as code owners July 3, 2024 04:04
@Chaosus Chaosus added this to the 4.x milestone Jul 3, 2024
@AThousandShips AThousandShips changed the title 2D shadows respect z_index Make 2D shadows respect z_index Jul 3, 2024
@AThousandShips AThousandShips added bug regression cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release and removed enhancement labels Jul 3, 2024
@AThousandShips AThousandShips modified the milestones: 4.x, 4.4 Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release regression topic:rendering topic:2d
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CanvasItem light_mask is ignored by LightOccluder2D occluder_light_mask
3 participants