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

Support shadow mapping with reverse Z #29770

Open
prideout opened this issue Oct 30, 2024 · 4 comments
Open

Support shadow mapping with reverse Z #29770

prideout opened this issue Oct 30, 2024 · 4 comments

Comments

@prideout
Copy link
Contributor

Description

To repro, modify examples/webgl_shadowmap.html by passing reverseDepthBuffer: true into the WebGLRenderer constructor.

To get anything to render at all, you will also need to add renderer.getContext().clearDepth(0) (see my comment in #29579).

After these modifications, the demo looks okay except that there are no shadows.

Solution

Ideally shadow mapping is supported without requiring clients to write any extra code, since DepthBuffer in WebGLState already flips the depth function & depth clear value at a low level. I have not yet been able to find the missing piece.

Alternatives

Alternatively, maybe the onus is on the applications to handle this in the onBeforeShadow hook?

Additional context

No response

@eXponenta
Copy link
Contributor

eXponenta commented Oct 30, 2024

Another problem with shadow mapping - needs to flip compare function inside shader programs.
Because if you use strict bias checking by lower depth in fragment it will miss die to buffer inversion.

reverseZ pretty unstable because required to change everything that use depthTexture and compares depth manually.

@eXponenta
Copy link
Contributor

eXponenta commented Oct 30, 2024

@CodyJasonBennett
@Mugen87
Looks like we should flip this line when compare samples from depth buffer and current texel, didn't we?

return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );

@CodyJasonBennett
Copy link
Contributor

Yes, the depth range is inverted. Any NDC math will also need to be adjusted for the [0, 1] range like it would for WebGPU.

@WestLangley
Copy link
Collaborator

Also of interest is how ShadowMapViewer should render when reverse Z is true. Perhaps the viewer should be designed to render the same, regardless. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
@WestLangley @prideout @eXponenta @Mugen87 @CodyJasonBennett and others