bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
32.96k stars 3.2k forks source link

Implement opt-in sharp screen-space reflections for the deferred renderer, with improved raymarching code. #13418

Closed pcwalton closed 6 days ago

pcwalton commented 2 weeks ago

This commit, a revamp of #12959, implements screen-space reflections (SSR), which approximate real-time reflections based on raymarching through the depth buffer and copying samples from the final rendered frame. This patch is a relatively minimal implementation of SSR, so as to provide a flexible base on which to customize and build in the future. However, it's based on the production-quality raymarching code by Tomasz Stachowiak.

For a general basic overview of screen-space reflections, see 1. The raymarching shader uses the basic algorithm of tracing forward in large steps, refining that trace in smaller increments via binary search, and then using the secant method. No temporal filtering or roughness blurring, is performed at all; for this reason, SSR currently only operates on very shiny surfaces. No acceleration via the hierarchical Z-buffer is implemented (though note that https://github.com/bevyengine/bevy/pull/12899 will add the infrastructure for this). Reflections are traced at full resolution, which is often considered slow. All of these improvements and more can be follow-ups.

SSR is built on top of the deferred renderer and is currently only supported in that mode. Forward screen-space reflections are possible albeit uncommon (though e.g. Doom Eternal uses them); however, they require tracing from the previous frame, which would add complexity. This patch leaves the door open to implementing SSR in the forward rendering path but doesn't itself have such an implementation. Screen-space reflections aren't supported in WebGL 2, because they require sampling from the depth buffer, which Naga can't do because of a bug (sampler2DShadow is incorrectly generated instead of sampler2D; this is the same reason why depth of field is disabled on that platform).

To add screen-space reflections to a camera, use the ScreenSpaceReflectionsBundle bundle or the ScreenSpaceReflectionsSettings component. In addition to ScreenSpaceReflectionsSettings, DepthPrepass and DeferredPrepass must also be present for the reflections to show up. The ScreenSpaceReflectionsSettings component contains several settings that artists can tweak, and also comes with sensible defaults.

A new example, ssr, has been added. It's loosely based on the three.js ocean sample, but all the assets are original. Note that the three.js demo has no screen-space reflections and instead renders a mirror world. In contrast to #12959, this demo tests not only a cube but also a more complex model (the flight helmet).

Changelog

Added

Screenshot 2024-05-18 143555

Screenshot 2024-05-18 143606

pcwalton commented 2 weeks ago

I made an attempt to get things working on WebGL 2 but hit a wall due to our old friend, the Naga bug whereby sampler2D is generated instead of sampler2DShadow. So I'm disabling on WebGL 2 and marking as ready for review.

pcwalton commented 1 week ago

Should be fixed now.

pcwalton commented 6 days ago

Addressed review comments.