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

[Merged by Bors] - bevy_pbr2: Fix clustering for orthographic projections #3316

Closed

Conversation

superdump
Copy link
Contributor

Objective

PBR lighting was broken in the new renderer when using orthographic projections due to the way the depth slicing works for the clusters. Fix it.

Solution

  • The default orthographic projection near plane is 0.0. The perspective projection depth slicing does a division by the near plane which gives a floating point NaN and the clustering all breaks down.
  • Orthographic projections have a linear depth mapping, so it made intuitive sense to me to do depth slicing with a linear mapping too. The alternative I saw was to try to handle the near plane being at 0.0 and using the exponential depth slicing, but that felt like a hack that didn't make sense.
  • As such, I have added code that detects whether the projection is orthographic based on projection[3][3] == 1.0 and then implemented the orthographic mapping case throughout (when computing cluster AABBs, and when mapping a view space position (or light) to a cluster id in both the rust and shader code).

Screenshots

Before:
before
After:
Screenshot 2021-12-13 at 16 36 53
Old renderer (slightly lighter due to slight difference in configured intensity):
Screenshot 2021-12-13 at 16 42 23

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Dec 13, 2021
@mockersf mockersf added A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior and removed S-Needs-Triage This issue needs to be labelled labels Dec 13, 2021
@superdump
Copy link
Contributor Author

Works in Sponza and looks appropriately funky 🙂 :
https://user-images.githubusercontent.com/302146/145849528-b14b86fb-217a-4cae-b7a4-1bac22a059a9.mp4

@superdump
Copy link
Contributor Author

I forgot to pick in the debug visualisation improvements and comment fixes from one of my branches with bloom on top of clustered forward before making the PR for it. I used the third visualisation (cluster coherency) to check the clustering, and the z-slicing one as well to find and fix a bug. So I added those in here too.

@superdump
Copy link
Contributor Author

Updated on top of main.

@cart cart added this to the Bevy 0.6 milestone Dec 14, 2021
@cart
Copy link
Member

cart commented Dec 14, 2021

Looks good to me. Thanks for the quick fix!

@cart
Copy link
Member

cart commented Dec 14, 2021

bors r+

bors bot pushed a commit that referenced this pull request Dec 14, 2021
# Objective

PBR lighting was broken in the new renderer when using orthographic projections due to the way the depth slicing works for the clusters. Fix it.

## Solution

- The default orthographic projection near plane is 0.0. The perspective projection depth slicing does a division by the near plane which gives a floating point NaN and the clustering all breaks down.
- Orthographic projections have a linear depth mapping, so it made intuitive sense to me to do depth slicing with a linear mapping too. The alternative I saw was to try to handle the near plane being at 0.0 and using the exponential depth slicing, but that felt like a hack that didn't make sense.
- As such, I have added code that detects whether the projection is orthographic based on `projection[3][3] == 1.0` and then implemented the orthographic mapping case throughout (when computing cluster AABBs, and when mapping a view space position (or light) to a cluster id in both the rust and shader code).

## Screenshots
Before:
![before](https://user-images.githubusercontent.com/302146/145847278-5b1bca74-fbad-4cc5-8b49-384f6a377fdc.png)
After:
<img width="1392" alt="Screenshot 2021-12-13 at 16 36 53" src="https://user-images.githubusercontent.com/302146/145847314-6f3a2035-5d87-4896-8032-0c3e35e15b7d.png">
Old renderer (slightly lighter due to slight difference in configured intensity):
<img width="1392" alt="Screenshot 2021-12-13 at 16 42 23" src="https://user-images.githubusercontent.com/302146/145847391-6a5e6fe0-22da-4fc1-a6c7-440543689a63.png">
@bors bors bot changed the title bevy_pbr2: Fix clustering for orthographic projections [Merged by Bors] - bevy_pbr2: Fix clustering for orthographic projections Dec 15, 2021
@bors bors bot closed this Dec 15, 2021
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 this pull request may close these issues.

3 participants