fancier water shader

This commit is contained in:
Lilith Schier 2026-09-01 16:01:14 +02:00
parent c97d645a8c
commit a72bf3107a
3 changed files with 40 additions and 23 deletions

View file

@ -144,9 +144,15 @@ sources/2 = SubResource("TileSetAtlasSource_wqvko")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_y2hwx"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_y2hwx"]
resource_local_to_scene = true resource_local_to_scene = true
shader = ExtResource("9_y2hwx") shader = ExtResource("9_y2hwx")
shader_parameter/water = Color(0.31764707, 0.5921569, 0.6431373, 0.64705884) shader_parameter/flow = Vector2(0, 0)
shader_parameter/highlights = Color(0.51546186, 0.75253254, 0.7743172, 1) shader_parameter/diffuse_color = Color(1, 1, 1, 0.9)
shader_parameter/highlights = Color(1, 1, 1, 0.2)
shader_parameter/top_light = Vector3(1, 1, 1)
shader_parameter/light_attenuation = 1.5
shader_parameter/absorption_color = Color(0.9, 0.016, 0.004, 1)
shader_parameter/top_border = 0.0 shader_parameter/top_border = 0.0
shader_parameter/top_ribbon_depth = 1.0
shader_parameter/top_ribbon_color = Color(1, 1, 1, 0.7)
[node name="Game" type="Node2D" unique_id=1186062666] [node name="Game" type="Node2D" unique_id=1186062666]
script = ExtResource("1_kc8ui") script = ExtResource("1_kc8ui")
@ -155,9 +161,9 @@ script = ExtResource("1_kc8ui")
layer = 0 layer = 0
follow_viewport_enabled = true follow_viewport_enabled = true
[node name="GlobalLight" type="DirectionalLight2D" parent="World" unique_id=207170317] [node name="SunLight" type="DirectionalLight2D" parent="World" unique_id=207170317]
position = Vector2(0, 1) shadow_enabled = true
energy = 0.9 height = 1.0
[node name="Background" type="Node2D" parent="World" unique_id=2097726518] [node name="Background" type="Node2D" parent="World" unique_id=2097726518]
@ -200,7 +206,7 @@ collision_visibility_mode = 2
[node name="Entities" type="Node2D" parent="World" unique_id=1592804921] [node name="Entities" type="Node2D" parent="World" unique_id=1592804921]
[node name="Player" parent="World/Entities" unique_id=500011307 instance=ExtResource("2_feb5d")] [node name="Player" parent="World/Entities" unique_id=500011307 instance=ExtResource("2_feb5d")]
position = Vector2(-367, -88) position = Vector2(-401, -31)
[node name="Birb" parent="World/Entities" unique_id=428810294 instance=ExtResource("2_fc0e3")] [node name="Birb" parent="World/Entities" unique_id=428810294 instance=ExtResource("2_fc0e3")]
position = Vector2(16, 32) position = Vector2(16, 32)
@ -214,6 +220,7 @@ polygon = PackedVector2Array(0, 0, 256, 0, 256, 32, 0, 32)
[node name="Rain4" type="Sprite2D" parent="World/Overlay" unique_id=940148613] [node name="Rain4" type="Sprite2D" parent="World/Overlay" unique_id=940148613]
physics_interpolation_mode = 2 physics_interpolation_mode = 2
visible = false
modulate = Color(1, 1, 1, 0.26666668) modulate = Color(1, 1, 1, 0.26666668)
texture_repeat = 2 texture_repeat = 2
position = Vector2(37, 13) position = Vector2(37, 13)

View file

@ -1,23 +1,31 @@
shader_type canvas_item; shader_type canvas_item;
render_mode blend_mix;
uniform vec4 water : source_color; uniform vec2 flow;
uniform vec3 highlights : source_color; uniform vec4 diffuse_color : source_color = vec4(1.0, 1.0, 1.0, 0.9);
uniform vec4 highlights : source_color = vec4(vec3(1.0), 0.2);
uniform vec3 top_light = vec3(1.0);
uniform float light_attenuation = 1.5;
uniform vec3 absorption_color : source_color = vec3(0.9, 0.016, 0.004);
uniform float top_border; uniform float top_border;
uniform float top_ribbon_depth = 1.0;
uniform vec4 top_ribbon_color : source_color = vec4(vec3(1.0), 0.7);
const float depth_band_size = 0.6;
varying vec2 world_position; varying vec2 world_position;
varying float depth;
#include "res://src/shaders/noise3D.gdshaderinc" #include "res://src/shaders/noise3D.gdshaderinc"
void vertex() { void vertex() {
world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy; world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
// Called for every vertex the material is visible on.
} }
void fragment() { void fragment() {
vec2 coords = world_position * vec2(0.01, 1.0) + TIME * vec2(0.0, 0.0); vec2 coords = (world_position - flow * TIME) * vec2(0.01, 1.0);
float t = TIME * 0.02; float t = TIME * 0.02;
float x = 0.0; float x = 0.0;
int octaves = 2; const int octaves = 2;
{ {
float mult = 1.0; float mult = 1.0;
for (int i = 0; i < octaves; i++) { for (int i = 0; i < octaves; i++) {
@ -28,20 +36,20 @@ void fragment() {
x = (x+1.0)/2.0; x = (x+1.0)/2.0;
if (x > 0.8) x = 1.0; else x = 0.0; if (x > 0.8) x = 1.0; else x = 0.0;
if (int(world_position.y) % 2 == 1) x = 0.0; if (int(world_position.y) % 2 == 1) x = 0.0;
vec3 raw_color = mix(water.rgb, highlights, x);
float depth = world_position.y - top_border; depth = world_position.y - top_border;
if (depth < 0.0) { if (depth < 0.0) {
COLOR = vec4(0.0); COLOR = vec4(0.0);
} else { } else {
float top_ribbon_depth = 6.0; float ribboned_depth = exp(round(log(depth) / depth_band_size) * depth_band_size);
float depth_grad = (top_ribbon_depth - depth) / top_ribbon_depth; vec3 light = top_light * exp(-light_attenuation*absorption_color*ribboned_depth);
float top_light = depth_grad > 0.0 ? max(0.0, depth_grad*depth_grad) : 0.0; float light_intensity = (light.r + light.g + light.b) / 3.0;
raw_color += vec3(top_light); float extra_transparency = sqrt(light_intensity) * 0.9;
COLOR = vec4(raw_color, water.a); vec3 raw_color = diffuse_color.rgb * light;
raw_color = mix(raw_color.rgb, highlights.rgb, x*highlights.a);
COLOR = vec4(raw_color, diffuse_color.a * (1.0-extra_transparency));
if (depth < top_ribbon_depth) {
COLOR = vec4(mix(COLOR.rgb, top_ribbon_color.rgb, top_ribbon_color.a), clamp(top_ribbon_color.a, COLOR.a, 1.0));
}
} }
} }
//void light() {
// // Called for every pixel for every light affecting the CanvasItem.
// // Uncomment to replace the default light processing function with this one.
//}

View file

@ -6,9 +6,11 @@
[sub_resource type="ShaderMaterial" id="ShaderMaterial_7iiff"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_7iiff"]
resource_local_to_scene = true resource_local_to_scene = true
shader = ExtResource("1_vkr3x") shader = ExtResource("1_vkr3x")
shader_parameter/flow = Vector2(0, 0)
shader_parameter/water = Color(0.31764707, 0.5921569, 0.6431373, 0.64705884) shader_parameter/water = Color(0.31764707, 0.5921569, 0.6431373, 0.64705884)
shader_parameter/highlights = Color(0.51546186, 0.75253254, 0.7743172, 1) shader_parameter/highlights = Color(0.51546186, 0.75253254, 0.7743172, 1)
shader_parameter/top_border = 0.0 shader_parameter/top_border = 0.0
shader_parameter/light_band_size = 8.0
[node name="WaterArea" type="Polygon2D" unique_id=469119063] [node name="WaterArea" type="Polygon2D" unique_id=469119063]
material = SubResource("ShaderMaterial_7iiff") material = SubResource("ShaderMaterial_7iiff")