-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Allow using nearest-neighbor filtering when sampling DEPTH_TEXTURE
#6025
Comments
There is no antialiasing being performed on the depth texture if MSAA is disabled (or if using FXAA/TAA only). If MSAA is enabled, it's likely a different story though. That said, aren't you referring to bilinear filtering here instead? In Godot 4, sampling state (filter/repeat) is set on the sampler, rather than on image files. Uniforms can have their filter mode set via property hints, but there's way to define those for |
Yes that must be bilinear filtering, sorry. I have seen the term antialiasing used by people describing the same issue but this makes more sense. |
DEPTH_TEXTURE
This is already the case in Godot 4, by the way. You can chose the sampler type. |
Thanks, that's a great feature. Using the bias argument of the texture function : sample the base depth and the refracted depth with something like this: The result I get is near-perfect and way way better than before :) |
Describe the project you are working on
I've been looking at many ocean/water shaders made by the community and all of them run into the same problem, caused by the DEPTH_TEXTURE. An exemple of it can be seen here.

Describe the problem or limitation you are having in your project
I'll try to explain it here:
You're coding a water shader with refraction.
When you sample the SCREEN_TEXTURE at (SCREEN_UV + an offset), you get a distorted SCREEN_TEXTURE that you can use for your refraction.
But there is one issue : since SCREEN_TEXTURE is a render of the whole screen, you also sample objects that are above the water level. This makes your shader unreadable/unrealistic.
So you must avoid rendering the objects that are above water.
The classic way to do it, as seen here (https://alextardif.com/Water.html) and many other tutorials, is to sample the DEPTH_TEXTURE where the refracted pixel should be, at (SCREEN_UV + an offset). Then you convert the depth at this coordinate to a world position. If the position is above water, you turn off refraction to avoid rendering it. The link above explains it with code, not in Godot but the language is similar.
However in Godot since DEPTH_TEXTURE is antialiased, you get at the edges of objects a value that is part the depth of the object, and part the depth of the object behind it. It messes with the result you're trying to achieve.
There are solutions to mitigate this problem, like checking multiple pixels around the target refracted pixel, but none of them work perfectly.
I've downloaded and tested most of the water shaders available on Godot shaders, Github etc, and if some of them almost get it right, they all get broken in edge cases, or show some kind of flickering pixels.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
The solution to this is to either entirely disable the DEPTH_TEXTURE antialiasing, or to enable sampling it unaliased. This way it would work similarly to other engines.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Something like textureDepth(vec2 UV, bool antialiased), or disabling antialiasing.
If this enhancement will not be used often, can it be worked around with a few lines of script?
No, the only way to bypass this issue right now would require using a viewport used as a buffer with the whole scene copied, which would be very detrimental to performance.
Is there a reason why this should be core and not an add-on in the asset library?
I put this in proposals but it may be considered an issue and relevant to the engine's core.
The text was updated successfully, but these errors were encountered: