generated from CSharpGodotTools/Template
-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
good first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is needed
Description
Video showing water
2023-04-30.15-27-40.mp4
Things that need to be done
- The vertex displacement was designed to work for linear planes but not spherical meshes like this. I need someone to redesign the shader vertex code to work with spherical geometry instead.
- I am not an expert with shaders but seeing other peoples water shaders, I know that this can look a lot better.
- Currently using the default Godot sphere for the water geometry. But it will help if we generated the water sphere through script just like I did with the planet but just remove the noise code. This way the water has more vertices to work with for larger planets and most of the indices do not meet at the top and bottom of the sphere creating a weird look.
Shader Code
The current shader code is a modified version of https://www.youtube.com/watch?v=7L6ZUYj1hs8
shader_type spatial;
uniform vec3 albedo : source_color;
uniform vec3 albedo_fresnel : source_color;
uniform float metallic : hint_range(0.0, 1.0);
uniform float roughness : hint_range(0.0, 1.0);
uniform sampler2D wave;
uniform sampler2D texture_normal_1;
uniform sampler2D texture_normal_2;
uniform vec2 wave_direction_1 = vec2(2.0, 0.0);
uniform vec2 wave_direction_2 = vec2(0.0, 1.0);
uniform float time_scale : hint_range(0.0, 0.03, 0.005) = 0.025;
uniform float noise_scale = 10.0;
uniform float height_scale = 0.15;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear_mipmap;
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
uniform vec4 color_deep : source_color;
uniform vec4 color_shallow : source_color;
uniform float beers_law = 2.0;
uniform float depth_offset = -0.75;
uniform float edge_scale = 0.1;
uniform float near = 0.5;
uniform float far = 100.0;
uniform vec3 edge_color : source_color;
varying float height;
varying vec3 world_pos;
float fresnel(float amount, vec3 normal, vec3 view)
{
return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0)), amount);
}
float edge(float depth) {
depth = 2.0 * depth - 1.0;
return near * far / (far + depth * (near - far));
}
void vertex() {
world_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
height = texture(wave, world_pos.xz / noise_scale * TIME * time_scale).r;
VERTEX.y += height * height_scale;
}
void fragment() {
float depth_texture = texture(DEPTH_TEXTURE, SCREEN_UV).r * 2.0 - 1.0;
float depth = PROJECTION_MATRIX[3][2] / (depth_texture + PROJECTION_MATRIX[2][2]);
float depth_blend = exp((depth+VERTEX.z + depth_offset) * -beers_law);
depth_blend = clamp(1.0 - depth_blend, 0.0, 1.0);
float depth_blend_power = clamp(pow(depth_blend, 2.5), 0.0, 1.0);
vec3 screen_color = textureLod(SCREEN_TEXTURE, SCREEN_UV, depth_blend_power * 2.5).rgb;
vec3 depth_color = mix(color_shallow.rgb, color_deep.rgb, depth_blend_power);
vec3 color = mix(screen_color * depth_color, depth_color * 0.25, depth_blend_power * 0.5);
// Getting edge depth calc
float z_depth = edge(texture(DEPTH_TEXTURE, SCREEN_UV).x);
float z_pos = edge(FRAGCOORD.z);
float z_dif = z_depth - z_pos;
vec2 time_1 = (TIME * wave_direction_1) * time_scale;
vec2 time_2 = (TIME * wave_direction_2) * time_scale;
vec3 normal_blend = mix(texture(texture_normal_1, UV + time_1).rgb, texture(texture_normal_2, UV + time_2).rgb, 0.5);
float fresnel = fresnel(5.0, NORMAL, VIEW);
vec3 surface_color = mix(albedo, albedo_fresnel, fresnel);
vec3 depth_color_adj = mix(edge_color, color, step(edge_scale, z_dif));
ALBEDO = clamp(surface_color + depth_color_adj,vec3(0.0),vec3(1.0));
METALLIC = metallic;
ROUGHNESS = roughness;
NORMAL_MAP = normal_blend;
}Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is needed