summaryrefslogtreecommitdiffstats
path: root/water/Wave.gdshader
diff options
context:
space:
mode:
authordusoleil <howcansocksbereal@gmail.com>2022-09-22 20:31:35 -0400
committerdusoleil <howcansocksbereal@gmail.com>2022-09-22 20:31:35 -0400
commitd089c01a602964d2e3b4892e6b5e40c7bbbe725d (patch)
tree895be39111df185ebcc06172a456f29fbc372b56 /water/Wave.gdshader
parent4ac770d52cfc93563b66f25c64f59ca61f5f3dfb (diff)
downloadgodot_wildjam_49-d089c01a602964d2e3b4892e6b5e40c7bbbe725d.tar.gz
godot_wildjam_49-d089c01a602964d2e3b4892e6b5e40c7bbbe725d.zip
Rework sea foam
Diffstat (limited to 'water/Wave.gdshader')
-rw-r--r--water/Wave.gdshader52
1 files changed, 38 insertions, 14 deletions
diff --git a/water/Wave.gdshader b/water/Wave.gdshader
index 95d064e..06c5327 100644
--- a/water/Wave.gdshader
+++ b/water/Wave.gdshader
@@ -13,15 +13,20 @@ uniform vec3 wave3 = vec3(0.5,0.5,0.5);
uniform vec2 wave3_dir = vec2(1.0,0.0);
uniform float wave3_t = 0.0;
-uniform vec4 water_colour: hint_color;
-uniform vec4 deep_water_colour : hint_color;
-uniform sampler2D noise;
+uniform vec4 water_color : hint_color;
+
+uniform vec4 foam_color: hint_color;
+uniform sampler2D foam_map;
+uniform float foam_amount_edge = 2.0;
+uniform float foam_amount_crest = 4.0;
-uniform float foam_amount = 0.6;
uniform float murkiness = 4.0;
uniform float refraction_amount = 0.01;
-varying float vertex_y;
+uniform sampler2D noise;
+
+varying vec3 global_vertex;
+varying vec3 displacement;
vec3 gerstner_wave(vec3 wave, vec2 wave_dir, float t, vec3 pos, inout vec3 tangent, inout vec3 binormal)
{
@@ -59,13 +64,14 @@ vec3 gerstner_wave(vec3 wave, vec2 wave_dir, float t, vec3 pos, inout vec3 tange
void vertex()
{
- vec3 global_vertex = (WORLD_MATRIX * vec4(VERTEX,1.0)).xyz;
+ global_vertex = (WORLD_MATRIX * vec4(VERTEX,1.0)).xyz;
TANGENT = vec3(1.0,0.0,0.0);
BINORMAL = vec3(0.0,0.0,1.0);
- VERTEX += gerstner_wave(wave1,wave1_dir,wave1_t,global_vertex,TANGENT,BINORMAL);
- VERTEX += gerstner_wave(wave2,wave2_dir,wave2_t,global_vertex,TANGENT,BINORMAL);
- VERTEX += gerstner_wave(wave3,wave3_dir,wave3_t,global_vertex,TANGENT,BINORMAL);
- vertex_y = VERTEX.y;
+ displacement = vec3(0.0,0.0,0.0);
+ displacement += gerstner_wave(wave1,wave1_dir,wave1_t,global_vertex,TANGENT,BINORMAL);
+ displacement += gerstner_wave(wave2,wave2_dir,wave2_t,global_vertex,TANGENT,BINORMAL);
+ displacement += gerstner_wave(wave3,wave3_dir,wave3_t,global_vertex,TANGENT,BINORMAL);
+ VERTEX += displacement;
NORMAL = normalize(cross(BINORMAL,TANGENT));
}
@@ -80,6 +86,18 @@ float get_depth(vec2 coords, sampler2D tex, mat4 proj)
return depth;
}
+const float pi = 3.1415;
+const float tau = pi * 2.0;
+
+float wave_position(float k,vec2 dir,float t)
+{
+ float pos = k * (dot(normalize(dir),global_vertex.xz)-t);
+ pos = mod(pos-pi,tau);
+ pos = clamp(pos,0.0,tau);
+ pos = 1.0 - (0.5*cos(0.159*pow(pos,2.0))+0.5);
+ return pos;
+}
+
void fragment()
{
ALPHA = 1.0;
@@ -92,9 +110,15 @@ void fragment()
float depth = get_depth(SCREEN_UV, DEPTH_TEXTURE, INV_PROJECTION_MATRIX);
float water_depth = -VERTEX.z;
float submersion = depth - water_depth;
- vec4 water_color = mix(deep_water_colour, water_colour, clamp(vertex_y,0.0,1.0));
- float foam = clamp((foam_amount - submersion) / foam_amount, 0.0, 1.0);
- ALBEDO = mix(water_color, vec4(1.0), foam).rgb;
+ float wave_pos = wave_position(wave1.y,wave1_dir,wave1_t);
+ wave_pos *= wave_position(wave2.y,wave2_dir,wave2_t);
+ wave_pos *= wave_position(wave3.y,wave3_dir,wave3_t);
+ wave_pos = 1.0 - (pow(1.0 - wave_pos, foam_amount_crest));
+ vec4 foam = texture(foam_map, UV + (-displacement.xz * 0.001));
+ vec4 mapped_foam_color = mix(water_color,foam_color,foam.b);
+ vec4 color = mix(water_color, mapped_foam_color, wave_pos);
+ float object_foam = clamp((foam_amount_edge - submersion) / foam_amount_edge, 0.0, 1.0);
+ ALBEDO = mix(color, vec4(1.0), object_foam).rgb;
vec3 oriented_normalmap = vec3(TANGENT*NORMALMAP.x + NORMAL*NORMALMAP.y + BINORMAL*NORMALMAP.z);
vec3 normal = normalize(mix(NORMAL,oriented_normalmap,NORMALMAP_DEPTH));
@@ -111,5 +135,5 @@ void fragment()
}
float depth_scatter = exp(murky);
vec4 bg_color = textureLod(SCREEN_TEXTURE, refraction_uv, depth_scatter);
- EMISSION = mix(water_color, bg_color, depth_visibility).rgb * (1.0 - water_color.a);
+ EMISSION = mix(color, bg_color, depth_visibility).rgb * (1.0 - color.a);
}