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?

asked Dec 3, 2019 in Engine by hexaquo (33 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

answered Dec 3, 2019 by hexaquo (33 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 How to use this Q&A? before posting your first questions.