Godot 3D Shader Tutorial (2) : Using the depth buffer @BastiaanOlij
Godot 3D Shader Tutorial (2) : Using the depth buffer  @BastiaanOlij
Uploaded September 2018 | Updated September 2026, 1 week ago
In this video we'll have a look at how we can use the depth buffer to find out how much distance there is between the surface of the water and the walls of the pool behind it. Then we use that to darken the water the higher that distance is.

Info about reconstructing positions from depth buffer:
derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer

Beers law:
en.wikipedia.org/wiki/Beer%E2%80%93Lambert_law

Just for completeness (note I'm leaving z as negative):
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
depth = depth * 2.0 - 1.0;
float z = -PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
float x = (SCREEN_UV.x * 2.0 - 1.0) * z / PROJECTION_MATRIX[0][0];
float y = (SCREEN_UV.y * 2.0 - 1.0) * z / PROJECTION_MATRIX[1][1];

or alternatively:
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
vec4 unproject = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
vec3 view_vertex = unproject.xyz / unproject.w;

Source code on github:
github.com/BastiaanOlij/shader_tutorial

Playlist:
youtube.com/watch?v=xoyk_A0RSpI&list=PLhqJJNjsQ7KHqNMYmTwtsYTeTrqrRP_fP

Follow me on twitter:
twitter.com/mux213
Godot 3D Shader Tutorial (2) : Using the depth bufferDev stream 50 - Godot Rendering Effects part 2Godot VR Weapons tutorial part 1 - Setting up the environment using GIProbeDev stream 56 - GameJam stuffGetting started with Godot and OpenXRGodot XR Community Game Jam July 2024 Theme announcementDev stream 16 - Working on my Godot VR Space gameDev stream 76 - Getting back into XR Tools v2Dev stream 53 - More Drums (Godot VR Dev)Dev stream 67 - Trying out RigidBody VR Character controllerDev stream 68 - Improvements to hand interactionsDev stream 74 - Trying out drawable textures
Bastiaan Olij |

Godot 3D Shader Tutorial (2) : Using the depth buffer

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER