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

Fix directional light shadows: take two #181

Merged
merged 1 commit into from
Mar 20, 2025

Conversation

PJB3005
Copy link
Contributor

@PJB3005 PJB3005 commented Mar 17, 2025

This PR aims to do the same as #180, but hopefully without making future WebXR compat more annoying.

get_frustum_corners() is now implemented properly, directly from the projection matrix. I believe it should work with the existing OpenXR code, but I cannot test this with any WebXR implementations with "sheared projection matrices", because I am continuing to doubt whether that even exists.

I also added unit tests, which involved a decent amount of code shuffling regardless. The original implementation from #180 is left in as a control in the test code.

Fixes #179

This PR aims to do the same as awtterpip#180, but hopefully without making future WebXR compat more annoying.

get_frustum_corners() is now implemented properly, directly from the projection matrix. I believe it should work with the existing OpenXR code, but I cannot test this with any WebXR implementations with "sheared projection matrices", because I am continuing to doubt whether that even exists.

I also added unit tests, which involved a decent amount of code shuffling regardless. The original implementation from awtterpip#180 is left in as a control in the test code.
@PJB3005 PJB3005 marked this pull request as draft March 17, 2025 23:45
Comment on lines +85 to +88
// I don't know why multiplying the Z axis by -1 is necessary.
// As far as I can tell from (likely my incorrect understanding of the code),
// PerspectiveProjection::get_frustum_corners() has the Z axis inverted??
Vec3A::from_vec4(inverse_matrix.mul_vec4(clip_pos)) / near * Vec3A::new(1., 1., -1.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to infinite far plane (far_z <= near_z)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the reverse Z only flips the resulting depth in the range 0-1. It should still have a right-handed coordinate system with Z- forward, but the existing bevy math writes the resulting frustum as if it's in the Z+ space.

@PJB3005
Copy link
Contributor Author

PJB3005 commented Mar 18, 2025

202395~1

Well, it works.

@PJB3005 PJB3005 marked this pull request as ready for review March 18, 2025 14:09
@Schmarni-Dev
Copy link
Collaborator

LGTM let me know if you are okay with me merging thus in its current state

@PJB3005
Copy link
Contributor Author

PJB3005 commented Mar 20, 2025

LGTM let me know if you are okay with me merging thus in its current state

Yep, I think it's done. Except maybe figuring out what's up with the coordinate space flip thing but idk.

@Schmarni-Dev Schmarni-Dev merged commit 8747b13 into awtterpip:main Mar 20, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Directional light shadows are cut off
3 participants