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

Atmosphere LUT parameterization improvements #17555

Merged
merged 10 commits into from
Feb 3, 2025

Conversation

mate-h
Copy link
Contributor

@mate-h mate-h commented Jan 27, 2025

Objective

  • Fix the atmosphere LUT parameterization in the aerial -view and sky-view LUTs
  • Correct the light accumulation according to a ray-marched reference
  • Avoid negative values of the sun disk illuminance when the sun disk is below the horizon

Solution

  • Adding a Newton's method iteration to fast_sqrt function
  • Switched to using fast_acos_4 for better precision of the sun angle towards the horizon (view mu angle = 0)
  • Simplified the function for mapping to and from the Sky View UV coordinates by removing an if statement and correctly apply the method proposed by the Hillarie paper detailed in section 5.3 and 5.4.
  • Replaced the ray_dir_ws.y term with a shadow factor in the sample_sun_illuminance function that correctly approximates the sun disk occluded by the earth from any view point

Testing

  • Ran the atmosphere and SSAO examples to make sure the shaders still compile and run as expected.

Showcase

showcase-img

Unverified

This user has not yet uploaded their public signing key.
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 27, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 27, 2025
@jnhyatt jnhyatt self-requested a review January 29, 2025 18:33
Copy link
Contributor

@jnhyatt jnhyatt left a comment

Choose a reason for hiding this comment

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

Just a bit of clarification on top of alice's, otherwise lgtm. Did some additional testing, I'll post a video showing the from-space view of the sun disc correctly dropping below the planet edge rather than the 0 degree zenith.

@jnhyatt
Copy link
Contributor

jnhyatt commented Jan 29, 2025

orbital.sunset.mp4

Copy link
Contributor

@ecoskey ecoskey left a comment

Choose a reason for hiding this comment

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

few small nits that could use clarification, otherwise lgtm :)

@ecoskey ecoskey added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 31, 2025
@mate-h
Copy link
Contributor Author

mate-h commented Jan 31, 2025

image

@mate-h
Copy link
Contributor Author

mate-h commented Jan 31, 2025

After some more discussions, this discovery points to an issue in how the sample points are calculated. Therefore in order to remove the 0.5 texel offset in the sampling function, we need to fix the math that compute the sample positions. I will work on that tomorrow.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 31, 2025
@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Jan 31, 2025
@ecoskey ecoskey added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jan 31, 2025
@mate-h
Copy link
Contributor Author

mate-h commented Jan 31, 2025

I've corrected the quadratic sampling in the sky view lut, reverted that to linear to recover the numerical accuracy. I've added a constant variable for the MIDPOINT_RATIO (open to renaming it) but it's now being used consistently in all raymarch loops.
Additionally, I am using the log(value) and exp(sample) method to linearly sample the inscattered light in log space, so that gets rid of some of the banding at high altitudes for a lut that extends far.

I posted the test scene and the raymarched reference code in the atmosphere-test branch

ecoskey and others added 3 commits February 1, 2025 17:31
// where in the segment that sample is taken (0.0 = start, 0.5 = middle, 1.0 = end).
// We use 0.3 to sample closer to the start of each segment, which better approximates
// the exponential falloff of atmospheric density.
const MIDPOINT_RATIO: f32 = 0.3;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could be exposed as a uniform variable, since it can differ depending on the scale height of the scatterers in the atmosphere.

Copy link
Contributor

@ecoskey ecoskey Feb 3, 2025

Choose a reason for hiding this comment

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

possibly, if it doesn't add more complexity to the user-facing api. Alternatively we could also just do the integration differently, for example piecewise linearly or quadratically (trapezoid rule vs simpson's rule)

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Feb 2, 2025
@mate-h
Copy link
Contributor Author

mate-h commented Feb 3, 2025

Okay great! I think I've addressed everything and this PR is ready for the merge queue @alice-i-cecile

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 3, 2025
@alice-i-cecile
Copy link
Member

Great cleanup and bug fixes; thanks a ton <3

Merged via the queue into bevyengine:main with commit f22ea72 Feb 3, 2025
28 checks passed
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
# Objective

- Fix the atmosphere LUT parameterization in the aerial -view and
sky-view LUTs
- Correct the light accumulation according to a ray-marched reference
- Avoid negative values of the sun disk illuminance when the sun disk is
below the horizon

## Solution

- Adding a Newton's method iteration to `fast_sqrt` function
- Switched to using `fast_acos_4` for better precision of the sun angle
towards the horizon (view mu angle = 0)
- Simplified the function for mapping to and from the Sky View UV
coordinates by removing an if statement and correctly apply the method
proposed by the [Hillarie
paper](https://sebh.github.io/publications/egsr2020.pdf) detailed in
section 5.3 and 5.4.
- Replaced the `ray_dir_ws.y` term with a shadow factor in the
`sample_sun_illuminance` function that correctly approximates the sun
disk occluded by the earth from any view point

## Testing

- Ran the atmosphere and SSAO examples to make sure the shaders still
compile and run as expected.

---

## Showcase

<img width="1151" alt="showcase-img"
src="https://github.com/user-attachments/assets/de875533-42bd-41f9-9fd0-d7cc57d6e51c"
/>

---------

Co-authored-by: Emerson Coskey <[email protected]>
joshua-holmes pushed a commit to joshua-holmes/bevy that referenced this pull request Feb 5, 2025
# Objective

- Fix the atmosphere LUT parameterization in the aerial -view and
sky-view LUTs
- Correct the light accumulation according to a ray-marched reference
- Avoid negative values of the sun disk illuminance when the sun disk is
below the horizon

## Solution

- Adding a Newton's method iteration to `fast_sqrt` function
- Switched to using `fast_acos_4` for better precision of the sun angle
towards the horizon (view mu angle = 0)
- Simplified the function for mapping to and from the Sky View UV
coordinates by removing an if statement and correctly apply the method
proposed by the [Hillarie
paper](https://sebh.github.io/publications/egsr2020.pdf) detailed in
section 5.3 and 5.4.
- Replaced the `ray_dir_ws.y` term with a shadow factor in the
`sample_sun_illuminance` function that correctly approximates the sun
disk occluded by the earth from any view point

## Testing

- Ran the atmosphere and SSAO examples to make sure the shaders still
compile and run as expected.

---

## Showcase

<img width="1151" alt="showcase-img"
src="https://github.com/user-attachments/assets/de875533-42bd-41f9-9fd0-d7cc57d6e51c"
/>

---------

Co-authored-by: Emerson Coskey <[email protected]>
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-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants