0 votes

I'm working on a raymarching shader, which is a canvasitem shader on a full-screen ColorRect - it reads from the SCREENTEXTURE and adds the raymarched objects to that color.

Currently, raymarched objects are always on top of other objects drawn by the engine. I'd like to access the depth buffer in my shader so that I can stop the march when I'm exceeding the depth at that pixel. However, I can't find a way to access the depth buffer in a canvasitem shader in the documentation - there's only SCREENTEXTURE with no depth component.

I've also tried turning it into a spatial shader, but it seems like 2D objects like ColorRects don't do anything with those.

Is there a way to do this?

in Engine by (37 points)

1 Answer

0 votes
Best answer

I figured out an alternative: Instead of a canvasitem shader, I use a spatial shader on a quad which is always positioned exactly in the viewport. When setup correctly, this looks and works exactly the same as a canvasitem shader on a full-screen texture, but it gives access to all the nice spatial built-ins! It even works in the editor since you can directly access the CAMERA_MATRIX etc. instead of having to pass them as uniform variables.

This tutorial has been very helpful: https://docs.godotengine.org/fi/latest/tutorials/shading/advanced_postprocessing.html

by (37 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmast[email protected] with your username.