Need help converting Unity shader to Godot 4
Asked Answered
S

3

0

I've spent the past couple days trying to recreate a water shader I found online, but I'm struggling to get it work correctly.
https://alexanderameye.github.io/notes/stylized-water-shader/

More specifically, I've been trying to implement the depth fade effect that is shown since I have not found any similar ones in Godot. The one in the article is done using the Unity shader graph, but I've tried my best to recreate it.

My code so far:

shader_type spatial;

uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, repeat_disable, filter_nearest; 

uniform float depth_distance = 2.0;

uniform vec3 shallow_color : source_color = vec3(1);
uniform vec3 deep_color : source_color = vec3(0);

void fragment()
{
	// Linearized scene depth
	float depth = textureLod(DEPTH_TEXTURE, SCREEN_UV, 0.0).r;
  	vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
	vec4 upos = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
	upos.xyz /= upos.w;
	float linear_depth = -upos.z;
	
	// "World space water position"
	vec3 frag_position_world = (INV_VIEW_MATRIX * vec4(VERTEX, 0.0)).xyz;
//	float view_length = distance(frag_position_world, CAMERA_POSITION_WORLD);
	
	// "World space scene position"
	vec3 view_vector = -(INV_VIEW_MATRIX * vec4(VIEW, 1.0)).xyz; // i think this is the problem, but not sure
	view_vector = view_vector / FRAGCOORD.z;
	view_vector = view_vector * linear_depth;
	vec3 scene_position_world = view_vector + CAMERA_POSITION_WORLD;
	
	// "Depth fade"
	vec3 water_view_depth = frag_position_world - scene_position_world;
	float water_depth = -water_view_depth.y;
	water_depth = exp(water_depth / depth_distance);
	water_depth = clamp(water_depth, 0.0, 1.0);
	vec3 color_out = mix(deep_color, shallow_color, water_depth);
	
	ALBEDO = color_out;
}

How it looks in game:

godot-v403-stable-win64-msosxwmawk.mp4
1MB

It looks like to me the issue might be that the VIEW vector is normalized by default, and I can't figure out a way to get the full vector? I've probably made some other mistakes as well, but I'm very new to shaders so I don't really know what I'm doing. If anyone could provide any guidance or help me figure out what the problem with it is then I would appreciate it.

Scissor answered 28/5, 2023 at 20:11 Comment(0)
S
0

Vitrify This fixed it right away! Thank you so much, I was really struggling with this.

EDIT: After some more time I realized this was actually having some issue with the depth being distorted in the corners of the screen depending on the viewing angle and it was driving me nuts. Found this thread which actually has a proper solution and is simpler than the stuff I was trying here.
https://mcmap.net/q/463/godot-how-can-i-fix-the-proximity-fade-or-linear-depth-distortion

Scissor answered 29/5, 2023 at 0:0 Comment(0)
P
0

I realise this will mean more work but a better option may be to start over with a new shader instead of fiddling with Unity code forever, been down that road as a unity refugee myself, wouldn't recommend it.

This video has everything you need and I've used this code myself, in fact I ended up commenting some fixes on the code myself awhile ago LOL.

Papillote answered 28/5, 2023 at 21:35 Comment(0)
V
0

Scissor
Assuming linear_depth is calculated as in your posted code:

// water frag position in world space
vec3 frag_position_world = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
// view direction in world space
vec3 view_direction_world = mat3(INV_VIEW_MATRIX) * -VIEW;
// scene geometry frag position in world space
vec3 scene_position_world = CAMERA_POSITION_WORLD + view_direction_world * linear_depth;
// linear water depth in world space
float water_depth = (frag_position_world - scene_position_world).y;
Vitrify answered 28/5, 2023 at 21:50 Comment(0)
S
0

Vitrify This fixed it right away! Thank you so much, I was really struggling with this.

EDIT: After some more time I realized this was actually having some issue with the depth being distorted in the corners of the screen depending on the viewing angle and it was driving me nuts. Found this thread which actually has a proper solution and is simpler than the stuff I was trying here.
https://mcmap.net/q/463/godot-how-can-i-fix-the-proximity-fade-or-linear-depth-distortion

Scissor answered 29/5, 2023 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.