screen space refraction shader

For screen space refraction shader Tutorial. i made it for ocean shader fragment part & later reflection & depth fade is planned too.

Shader code
shader_type spatial;

uniform vec3 albedo : source_color;
uniform sampler2D normalmap;

uniform float alpha : hint_range(0.0, 1.0, 0.01) = 0.8;
uniform float refraction_strength : hint_range(0.0, 8.0, 0.001) = 0.5;
uniform sampler2D screen_texture : hint_screen_texture;

uniform sampler2D depth_texture : hint_depth_texture;

uniform bool is_depth_based = false;

vec2 refract_uv(vec2 uv, float strength, vec3 normal){
	float strength1 = strength * (is_depth_based ? pow(uv.y, 4.0) * 5.0 : 1.0);
	uv += strength1 * length(normal) - strength1 * 1.2;
	return uv;

void fragment() {
	vec3 nmap = texture(normalmap, UV).rgb;
	ALBEDO = mix(albedo, texture(screen_texture, refract_uv(SCREEN_UV, refraction_strength, nmap)).rgb, 1.0 - alpha);
ocean, refraction, screen-space, water
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from mujtaba-io

Ray marching ocean waves + atmosphere

3D fire shader

Gerstner Wave Ocean Shader

Related shaders

Thick 3D Screen Space – Depth – & Normal – Based Outline Shader.

Screen-Space Edge Detection Outline Shader

Screen Space Frost, with volumetric Snow

Notify of

Newest Most Voted
Inline Feedbacks
View all comments
3 months ago

there are errors in this in godot 4.x around the normal map in the void fragment.