tutorials/shaders/advanced_postprocessing #42
Replies: 3 comments 4 replies
-
Here's an example you can use as the base for your effects. It reads from all three screen textures ( // Godot 4.3, Forward+ or Mobile
shader_type spatial;
render_mode unshaded, fog_disabled;
uniform sampler2D screen_texture : source_color, hint_screen_texture;
uniform sampler2D depth_texture : hint_depth_texture;
uniform sampler2D normal_rough_texture : hint_normal_roughness_texture;
void vertex() {
POSITION = vec4(VERTEX.xy, 1.0, 1.0);
}
void fragment() {
vec4 screen = texture(screen_texture, SCREEN_UV);
float depth_raw = texture(depth_texture, SCREEN_UV).x;
vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth_raw);
vec4 position_view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
position_view.xyz /= position_view.w;
float linear_depth = -position_view.z;
vec4 world = INV_VIEW_MATRIX * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
vec3 position_world = world.xyz / world.w;
vec4 normal_rough = texture(normal_rough_texture, SCREEN_UV);
vec3 normals_view_raw = normal_rough.xyz; // Normals in view space, in [0.0, 1.0] range
vec3 normals_view_remapped = normals_view_raw.xyz * 2.0 - 1.0; // Normals in view space, in [-1.0, 1.0] range
vec3 normals_world = (INV_VIEW_MATRIX * vec4(normals_view_remapped, 0.0)).xyz;
float roughness = normal_rough.w;
// Visualize the outputs
// Screen texture
ALBEDO.rgb = screen.rgb;
// Raw depth
//ALBEDO.rgb = vec3(depth_raw);
// Linear depth
//ALBEDO.rgb = vec3(fract(linear_depth));
// World position
//ALBEDO.rgb = fract(position_world);
// Normals from the normal buffer, in view space
//ALBEDO.rgb = normals_view_raw;
// Normals in world space, [-1.0,1.0] range
//ALBEDO.rgb = normals_world;
// Roughness
//ALBEDO.rgb = vec3(roughness);
} |
Beta Was this translation helpful? Give feedback.
-
I also posted this comment in the Screen-Reading Shaders doc, but I think it is worth noting here as well. That doc mentions a significant limitation with post-processing and transparent objects in 3D:
This affects not just the screen texture but also the depth and normal-roughness textures. There are a couple ways to get around this so that you can do post-processing effects with transparent objects:
|
Beta Was this translation helpful? Give feedback.
-
Not sure if I am just slow but I was getting an odd oscillating / banding effect (similar to this) even if I just copy-pasted the tutorial shader into my file. Through trial and error I found I had to divide the linear depth by some value around 100 or above to get the oscillating effect to push out far enough away for it not to be present anymore. float linear_depth = -(view.xyz / view.w).z / 100.0; If any shader experts out there understand why this would happen I would love to hear it! Thanks and hope this helps somebody! ✌️ |
Beta Was this translation helpful? Give feedback.
-
tutorials/shaders/advanced_postprocessing
Introduction: This tutorial describes an advanced method for post-processing in Godot. In particular, it will explain how to write a post-processing shader that uses the depth buffer. You should al...
https://docs.godotengine.org/en/stable/tutorials/shaders/advanced_postprocessing.html
Beta Was this translation helpful? Give feedback.
All reactions