game: water shader experiments

This commit is contained in:
2025-08-19 06:26:50 +02:00
parent 0268789c54
commit c12ca7ba31
9 changed files with 228 additions and 1 deletions

View File

@@ -0,0 +1,91 @@
[gd_resource type="VisualShader" load_steps=9 format=3 uid="uid://fcm46siaht3c"]
[ext_resource type="Texture2D" uid="uid://cqjhrbw36g1hf" path="res://assets/sprites/3xwater.png" id="1_s0q3e"]
[sub_resource type="VisualShaderNodeTexture" id="VisualShaderNodeTexture_wu54b"]
output_port_for_preview = 0
texture = ExtResource("1_s0q3e")
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_s0q3e"]
output_port_for_preview = 0
input_name = "uv2"
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_wu54b"]
input_name = "uv"
[sub_resource type="VisualShaderNodeTexture" id="VisualShaderNodeTexture_2m85h"]
texture = ExtResource("1_s0q3e")
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_nkbt4"]
expanded_output_ports = [0]
input_name = "vertex"
[sub_resource type="VisualShaderNodeVectorOp" id="VisualShaderNodeVectorOp_88kc6"]
default_input_values = [0, Vector2(0, 0), 1, Vector2(50, 50)]
op_type = 0
[sub_resource type="VisualShaderNodeIf" id="VisualShaderNodeIf_kx7fy"]
default_input_values = [0, 0.0, 1, 0.0, 2, 1e-05, 3, Vector3(0, 0, 0), 4, Vector3(0, 0, 0), 5, Vector3(100, 0, 0)]
[resource]
code = "shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_schlick_ggx;
void vertex() {
// Input:3
vec3 n_out3p0 = VERTEX;
float n_out3p2 = n_out3p0.g;
vec3 n_out5p0;
// If:5
float n_in5p1 = 0.00000;
float n_in5p2 = 0.00001;
vec3 n_in5p3 = vec3(0.00000, 0.00000, 0.00000);
vec3 n_in5p4 = vec3(0.00000, 0.00000, 0.00000);
vec3 n_in5p5 = vec3(100.00000, 0.00000, 0.00000);
if(abs(n_out3p2 - n_in5p1) < n_in5p2)
{
n_out5p0 = n_in5p3;
}
else if(n_out3p2 < n_in5p1)
{
n_out5p0 = n_in5p5;
}
else
{
n_out5p0 = n_in5p4;
}
// VectorOp:4
vec2 n_out4p0 = vec2(n_out3p0.xy) + vec2(n_out5p0.xy);
// Output:0
VERTEX = vec3(n_out4p0, 0.0);
}
"
graph_offset = Vector2(-22.2715, 18.9148)
nodes/vertex/0/position = Vector2(1160, 240)
nodes/vertex/2/node = SubResource("VisualShaderNodeTexture_2m85h")
nodes/vertex/2/position = Vector2(40, 280)
nodes/vertex/3/node = SubResource("VisualShaderNodeInput_nkbt4")
nodes/vertex/3/position = Vector2(300, 140)
nodes/vertex/4/node = SubResource("VisualShaderNodeVectorOp_88kc6")
nodes/vertex/4/position = Vector2(860, 120)
nodes/vertex/5/node = SubResource("VisualShaderNodeIf_kx7fy")
nodes/vertex/5/position = Vector2(640, 340)
nodes/vertex/connections = PackedInt32Array(3, 0, 4, 0, 3, 2, 5, 0, 5, 0, 4, 1, 4, 0, 0, 0)
nodes/fragment/0/position = Vector2(840, 100)
nodes/fragment/2/node = SubResource("VisualShaderNodeTexture_wu54b")
nodes/fragment/2/position = Vector2(260, 180)
nodes/fragment/3/node = SubResource("VisualShaderNodeInput_s0q3e")
nodes/fragment/3/position = Vector2(-140, 200)
nodes/fragment/4/node = SubResource("VisualShaderNodeInput_wu54b")
nodes/fragment/4/position = Vector2(540, 220)

View File

@@ -0,0 +1,36 @@
shader_type canvas_item;
// Parameters
uniform sampler2D water_texture; // Your 3x3 water texture
uniform vec4 water_color : source_color = vec4(0.0, 0.0, 1.0, 1.0);
uniform float speed : hint_range(0.0, 10.0) = 1.0;
uniform float strength : hint_range(0.0, 0.2) = 0.05;
uniform float tile_repeat : hint_range(1.0, 10.0) = 3.0;
uniform vec2 pan_speed = vec2(0.2, 0.1); // how fast the water texture moves
void fragment() {
vec2 uv = UV;
// Sample the base tile texture
vec4 base_tex = texture(TEXTURE, uv);
// Compute mask for water pixels only
float is_water = step(0.8, 1.0 - distance(base_tex.rgb, water_color.rgb));
// Tile the water texture
vec2 water_uv = uv * tile_repeat;
water_uv = fract(water_uv);
// Apply panning / movement
water_uv += pan_speed * TIME;
// Add wave distortion on top of the moving texture
water_uv.y += sin(water_uv.x * 2.0 + TIME * speed) * strength * is_water;
water_uv.x += cos(water_uv.y * 2.0 + TIME * speed * 0.5) * strength * is_water;
// Sample the animated water texture
vec4 water_sample = texture(water_texture, water_uv);
// Combine: water animates, other pixels stay the same
COLOR = mix(base_tex, water_sample, is_water);
}

View File

@@ -0,0 +1 @@
uid://dxlfb81ouff0w

View File

@@ -0,0 +1,27 @@
shader_type canvas_item;
uniform vec4 water_color : source_color = vec4(0.0, 0.0, 1.0, 1.0);
uniform sampler2D noise_tex; // noise texture
uniform vec2 noise_speed = vec2(0.05, 0.01);
uniform float noise_strength = 0.2; // how much to lighten/darken
void fragment() {
vec2 uv = UV;
vec4 base_tex = texture(TEXTURE, uv);
float is_water = step(0.8, 1.0 - distance(base_tex.rgb, water_color.rgb));
// Scroll UVs for noise animation
vec2 noise_uv = SCREEN_UV + noise_speed * TIME;
vec4 noise_sample = texture(noise_tex, fract(noise_uv));
// Use noise (0..1) to brighten/darken the water color
float brightness = (noise_sample.r - 0.5) * 2.0 * noise_strength;
vec4 animated_water = water_color + vec4(vec3(brightness), 0.0);
COLOR = mix(base_tex, animated_water, is_water);
}
//void light() {
// // Called for every pixel for every light affecting the material.
// // Uncomment to replace the default light processing function with this one.
//}

View File

@@ -0,0 +1 @@
uid://clpp23h7fpamt