Get Depth & Normal Texture From Camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By palhimanshu

Hi all,

I’m trying to get the depth & normal texture of a 3d scene from camera in Godot.
I’m trying to port some of my code from Unity to Godot.
In Unity3d, i just applied a very basic shader to camera via script and retrieved its render texture. (I followed this tutorial

How to do this correctly & efficiently in Godot?

I’ve looked at the documentation and the mentioned water shader tutorial, but they talk about applying shader to objects and not to a camera.

Thank you for any suggestions you can provide.

:bust_in_silhouette: Reply From: PoisonIvy

This Godot Doc helped me out a ton:

I had to create a MeshInstance with a Quad primitive (width and height set to 2), parent it to my camera, and set Geometry->Extra Cull Margin to its maximum. Then I applied the following shader to the quad:

shader_type spatial;

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

void fragment() {
    float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
    vec3 ndc = vec3(SCREEN_UV, depth) * 2.0 - 1.0;
    vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0); /= view.w;
    float linear_depth = -view.z;

    ALBEDO = vec3(linear_depth / 100.0);

That division by 100.0 at the end is because I needed to scale linear_depth to the camera’s view range. If your camera’s Far is set to some other value, you’ll need to divide by that instead.

At the end of it, this is what I see rendered:
Depth Texture Results
It also works in the editor just fine.

In godot 4, it is now fully possible to get the normals now with the additional screen textures presented here