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

Full screen quad depth buffer issues depending on camera settings #48202

Open
thesquaregroot opened this issue Apr 26, 2021 · 4 comments
Open

Comments

@thesquaregroot
Copy link

thesquaregroot commented Apr 26, 2021

Likely related to #43575.

Godot version:
3.3.stable, Linux

OS/device including version:
Pop!_OS Linux 20.10, X11, Nvidia GTX 760, GLES3

Issue description:
Following the steps in the advanced post-processing tutorial for using a full-screen quad with the depth buffer, appears to work fine until camera settings are changed. With a perspective projection, increasing the near clipping distance creates a foreground area unaffected by the shader. With an orthogonal projection, only objects closer to the far clipping plane seem to be affected. The actual depth values also appear to be very different at similar distances

Steps to reproduce:
Create a simple scene with a full-screen quad as described in the tutorial linked above. Change the camera settings to see results like the following:

setting-1
setting-2
setting-3
setting-4
setting-5
setting-6
setting-7
setting-8

Minimal reproduction project:
OrthogonalDepthTest.zip

@clayjohn
Copy link
Member

The tutorial should be updated to explain that the sample code is only for cameras using perspective projection.

And it should explain that the quad needs to be beyond the near plane so that it doesn't not get clipped.

@thesquaregroot
Copy link
Author

@clayjohn Thanks, I had guessed the sample might not work for orthogonal projections, but admittedly assumed INV_PROJECTION_MATRIX would change to account for the current camera's projection in a way that would make it all work out.

That said, I'm still skeptical of the perspective case. I was (perhaps wrongfully) under the impression that this part of the shader would make sure the quad was in the right place:

void vertex() {
  POSITION = vec4(VERTEX, 1.0);
}

It at least seems to work fine when the near clipping distance is small (0.05). Plus, it seems more like the quad is just moving further into the scene than I want it to. To prove this to myself, I put a couple objects underneath the plane I had in the original project and set the near value to 2. This is what I got:

boxes_under_plane

I tried adjusting the quad translation but that didn't do anything. However, adjusting the z-value assigned to POSITION worked:

  POSITION = vec4(VERTEX - vec3(0.0, 0.0, 1.0), 1.0);

position_offset

Interestingly, this seems to fix the issue regardless of the value I set for the near clipping distance (I assumed it would vary with the distance), provided extra_cull_margin on the quad is at least that value (e.g. if near is 7, extra_cull_margin needs to be 7.01+). The tutorial suggests you set this as high as it will go though, so that shouldn't be a problem.

I'm admittedly not entirely sure that I understand what is happening, as I'm somewhat new to shaders, but I'm questioning if it might be an off-by-one error, either in the engine or just as an oversight in the tutorial. Either way, hopefully this is useful information. Thanks again!

@SephReed
Copy link

SephReed commented Nov 20, 2024

POSITION = vec4(VERTEX - vec3(0.0, 0.0, 1.0), 1.0); Did not fix the issue for me.

Perspective:
Image

Orthogonal:
Image

Scene Preview:
Image

I have a stack overflow question here:
https://gamedev.stackexchange.com/questions/212413/how-can-i-make-a-screen-shader-for-orthogonal-camera-in-godot-4

@SephReed
Copy link

https://godotengine.org/article/introducing-reverse-z/

POSITION = vec4(VERTEX.xy, 1.0, 1.0);

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

3 participants