Dynamically render heightmap in front of camera (to offset grass to fit!)

Godot Version



I’ve made a grass shader… (but)


…now i want to place it dynamically in front of the camera on Terrain3D only at all times…

My thought was to go with a subviewport and a camera3D inside of it. Cam set to orthogonal and it actually seemed to work a bit. The camera renders stuff. I’ve figured out to use the cull mask to only render the terrain.
But two things stopped me here.

  1. Using subviewport made it so i can’t move the camera in the subviewport around. So i can’t make it follow the “real” camera. Is this a bug you think?
    The subviewport is not a “3d” thing so i cant move around just like a “Node” cant move around. Contrary to the “Node3D”.

  2. I can’t seem to figure out how to render a height/depth map from that camera. I feel like this is the completely wrong direction!
    I would expect to find some render-pass options in here, but i think there really isn’t any usable ones?

Anyone have some pointers?

Thing is I would love it to be so its not specifically tied to the Terrain3D, but could be used for anything, really, that you would populate geometry with stuff.
And by “not tied to Terrain3D” i mean exporting a height map from Terrain3D as part of the pipeline…

idiotic idea incoming!!!

using this shader →

shader_type spatial;
uniform sampler2D depth_tex:hint_depth_texture;

void fragment() {
	float depth = texture(depth_tex, SCREEN_UV).r;
	depth = depth*2.0 - 1.0;
	depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
	ALBEDO = vec3(depth * 0.01);

with a camera that looks at a quad with that shader on. Only rendering that quad. Pipe the output into my grass shader. somehow?

(like so!)

(look it works)
(but it “renders” everything… sad)

  • Hmm, wonder how i get the depth shader to only render a specific cull mask?
  • Hmm, wonder how to get the output of a camera into a shader?

Alright. The camera (of course) is responsible for what it renders so setting that to only render cull mask 20 (the terrain in my attempt here) works.

So never mind what the quad sees. :sweat_smile:

But I must say getting whatever the camera renders into a shader is more difficult than I would imagine.

Anyone tried this before?
(getting viewport texture into a shader)