diff options
author | dusoleil <howcansocksbereal@gmail.com> | 2022-09-22 20:31:35 -0400 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2022-09-22 20:31:35 -0400 |
commit | d089c01a602964d2e3b4892e6b5e40c7bbbe725d (patch) | |
tree | 895be39111df185ebcc06172a456f29fbc372b56 | |
parent | 4ac770d52cfc93563b66f25c64f59ca61f5f3dfb (diff) | |
download | godot_wildjam_49-d089c01a602964d2e3b4892e6b5e40c7bbbe725d.tar.gz godot_wildjam_49-d089c01a602964d2e3b4892e6b5e40c7bbbe725d.zip |
Rework sea foam
-rw-r--r-- | water/Water.tscn | 11 | ||||
-rw-r--r-- | water/Wave.gdshader | 52 | ||||
-rw-r--r-- | water/foam_map.png | bin | 0 -> 2122701 bytes | |||
-rw-r--r-- | water/foam_map.png.import | 37 |
4 files changed, 82 insertions, 18 deletions
diff --git a/water/Water.tscn b/water/Water.tscn index 6b4c72b..b37340b 100644 --- a/water/Water.tscn +++ b/water/Water.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://water/Wave.gdshader" type="Shader" id=1] [ext_resource path="res://water/Water.gd" type="Script" id=2] +[ext_resource path="res://water/foam_map.png" type="Texture" id=3] [sub_resource type="PlaneMesh" id=9] size = Vector2( 2000, 2000 ) @@ -26,11 +27,13 @@ shader_param/wave2_t = 0.0 shader_param/wave3 = Vector3( 0.25, 0.349066, 0.716197 ) shader_param/wave3_dir = Vector2( 1, 1.3 ) shader_param/wave3_t = 0.0 -shader_param/water_colour = Color( 0.694118, 0.945098, 0.941176, 0.368627 ) -shader_param/deep_water_colour = Color( 0.25098, 0.623529, 0.788235, 0.54902 ) -shader_param/foam_amount = 0.6 +shader_param/water_color = Color( 0.25098, 0.623529, 0.788235, 0.54902 ) +shader_param/foam_color = Color( 0.694118, 0.945098, 0.941176, 0.368627 ) +shader_param/foam_amount_edge = 2.0 +shader_param/foam_amount_crest = 4.0 shader_param/murkiness = 4.0 shader_param/refraction_amount = 0.01 +shader_param/foam_map = ExtResource( 3 ) shader_param/noise = SubResource( 21 ) [node name="Water" type="Spatial"] 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); } diff --git a/water/foam_map.png b/water/foam_map.png Binary files differnew file mode 100644 index 0000000..6560cdf --- /dev/null +++ b/water/foam_map.png diff --git a/water/foam_map.png.import b/water/foam_map.png.import new file mode 100644 index 0000000..d79f7e9 --- /dev/null +++ b/water/foam_map.png.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="StreamTexture" +path.s3tc="res://.import/foam_map.png-8391a4e69d9c8dcac9b9d144ef3261b8.s3tc.stex" +path.etc2="res://.import/foam_map.png-8391a4e69d9c8dcac9b9d144ef3261b8.etc2.stex" +metadata={ +"imported_formats": [ "s3tc", "etc2" ], +"vram_texture": true +} + +[deps] + +source_file="res://water/foam_map.png" +dest_files=[ "res://.import/foam_map.png-8391a4e69d9c8dcac9b9d144ef3261b8.s3tc.stex", "res://.import/foam_map.png-8391a4e69d9c8dcac9b9d144ef3261b8.etc2.stex" ] + +[params] + +compress/mode=2 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 |