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

Directional light shadows disappear when camera is rotated #8645

Closed
GitGhillie opened this issue May 21, 2023 Discussed in #8571 · 11 comments · Fixed by #8877
Closed

Directional light shadows disappear when camera is rotated #8645

GitGhillie opened this issue May 21, 2023 Discussed in #8571 · 11 comments · Fixed by #8877
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Milestone

Comments

@GitGhillie
Copy link
Contributor

Bevy version

0.10.1,
main (f76b3c4)

Relevant system information

  • SteamOS 3.4.6
    AdapterInfo { name: "AMD Custom GPU 0405 (RADV VANGOGH)", vendor: 4098, device: 5695, device_type: IntegratedGpu, driver: "radv", driver_info: "Mesa 22.2.5", backend: Vulkan }

What you did

  • Spawn two horizontal planes above each other
  • Spawn directional light and a camera between the planes
  • Rotate camera

See somewhat minimal example here.

What went wrong

Depending on the distance and angle of the camera some shadows partially disappear. As seen here it can quickly ruin a scene.

In the example code I gave only rectangular shadows should be possible but when you spawn in it looks like this:
bevy_shadow
Move around the camera with the mouse and you will see the shadow change shape.

Additional information

Same issue on my Windows 10 desktop (tested with Bevy 0.10.1) but I'm currently abroad so I cannot check the exact AdapterInfo. It has an AMD Vega 56 gpu and uses the Vulkan backend as well.

@nicopap nicopap added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen labels May 21, 2023
@DGriffin91
Copy link
Contributor

I've also noticed this issue.

@GitGhillie
Copy link
Contributor Author

@DGriffin91 good to know it's not just me. Are you also using Vulkan + AMD card?

@DGriffin91
Copy link
Contributor

DGriffin91 commented May 31, 2023

I've seen it on both DX12 and Vulkan on Nvidia. I think it's probably an issue with the directional light shadow cascade calculations. Maybe something in:

fn calculate_cascade(

@DGriffin91
Copy link
Contributor

Here's a video of the issue:

2023-05-30.21-02-49.mp4

This is with these settings:

CascadeShadowConfigBuilder {
    num_cascades: 4,
    minimum_distance: 0.01,
    maximum_distance: 50.0,
    first_cascade_far_bound: 3.0,
    overlap_proportion: 0.2,
}

This issue also happens in the sponza test scene.

@superdump
Copy link
Contributor

Hmm, I can't seem to reproduce with the example code on an M1 Max in macOS. Could someone make a video of the problem with the example code? Or else make a minimal example that reproduces the problem, plus a video showing the problem you see?

@superdump
Copy link
Contributor

Ok, I tried on AdapterInfo { name: "NVIDIA GeForce RTX 3080 Laptop GPU", vendor: 4318, device: 9372, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "528.02", backend: Vulkan } on Windows 11 and it looked fine. But with WGPU_BACKEND=dx12 I see the issue. The shadow on the plane beneath the camera should be rectangular, but when looking down a bit the corner kind of inverts such that light cuts into the shadow region. Hmm, @danchia and @DGriffin91 were discussing near plane problems on Discord and now I've seen it first hand I'm inclined to agree.

@DGriffin91
Copy link
Contributor

@superdump I made a little test, the included one was a bit disorienting for me: https://github.com/DGriffin91/bevy_csm_test I see the issue on both vulkan and dx12 on a RTX3060. I have the camera spawn near one issue. You can also see it it you go up to one of the upper corners.

@JMS55 JMS55 added this to the 0.11 milestone Jun 11, 2023
@GitGhillie
Copy link
Contributor Author

GitGhillie commented Jun 11, 2023

cascades.mp4

Looks like the issue occurs near the camera and near the cascade bounds.
Cascade settings for this video:

cascade_shadow_config: CascadeShadowConfigBuilder {
                num_cascades: 3,
                maximum_distance: 20.0,
                overlap_proportion: 0.0,
                ..default()
            }

@GitGhillie
Copy link
Contributor Author

github-merge-queue bot pushed a commit that referenced this issue Jun 21, 2023
# Objective

- Fixes #8645

## Solution

Cascaded shadow maps use a technique commonly called shadow pancaking to
enhance shadow map resolution by restricting the orthographic projection
used in creating the shadow maps to the frustum slice for the cascade.
The implication of this restriction is that shadow casters can be closer
than the near plane of the projection volume.

Prior to this PR, we address clamp the depth of the prepass vertex
output to ensure that these shadow casters do not get clipped, resulting
in shadow loss. However, a flaw / bug of the prior approach is that the
depth that gets written to the shadow map isn't quite correct - the
depth was previously derived by interpolated the clamped clip position,
resulting in depths that are further than they should be. This creates
artifacts that are particularly noticeable when a very 'long' object
intersects the near plane close to perpendicularly.

The fix in this PR is to propagate the unclamped depth to the prepass
fragment shader and use that depth value directly.

A complementary solution would be to use
[DEPTH_CLIP_CONTROL](https://docs.rs/wgpu/latest/wgpu/struct.Features.html#associatedconstant.DEPTH_CLIP_CONTROL)
to request `unclipped_depth`. However due to the relatively low support
of the feature on Vulkan (I believe it's ~38%), I went with this
solution for now to get the broadest fix out first.

---

## Changelog

- Fixed: Shadows from directional lights were sometimes incorrectly
omitted when the shadow caster was partially out of view.

---------

Co-authored-by: Carter Anderson <[email protected]>
@DGriffin91
Copy link
Contributor

DGriffin91 commented Jul 9, 2023

This appears to still be an issue in sponza with bevy 0.11
https://github.com/DGriffin91/bevy_sponza_scene
image
image
image

@DGriffin91
Copy link
Contributor

This appears to still be an issue in bistro with bevy 0.11
https://github.com/DGriffin91/bevy_bistro_scene
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants