Worked on Gerstner waves and foam textures

This commit is contained in:
nc5432 2025-04-08 10:33:02 -04:00
parent 99c4a764df
commit 5927508474
18 changed files with 225 additions and 86 deletions

View File

@ -0,0 +1,6 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://b4imtle5888n8"]
[ext_resource type="Shader" uid="uid://bik7guyuy8hjm" path="res://Shaders/GerstnerFoam.gdshader" id="1_yjp2h"]
[resource]
shader = ExtResource("1_yjp2h")

View File

@ -0,0 +1,15 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://dipb5q5o7g4dx"]
[ext_resource type="Shader" uid="uid://d3g1qmicakwh2" path="res://Shaders/GerstnerWater.gdshader" id="1_ln7ti"]
[resource]
render_priority = 0
shader = ExtResource("1_ln7ti")
shader_parameter/color = Vector3(0.1, 0.4, 0.8)
shader_parameter/speed = 1.0
shader_parameter/amplitude = 0.2
shader_parameter/frequency = 1.0
shader_parameter/waveCount = 16
shader_parameter/amplitudeScaleFactor = 0.82
shader_parameter/frequencyScaleFactor = 1.18
shader_parameter/dirs = PackedFloat32Array(0.981, -0.588, 0.035, -0.977, -0.809, -0.351, 0.989, -0.24, -0.192, 0.907, -0.479, 0.499, 0.388, -0.97, 0.882, -0.845, 0.246, -0.068, -0.41, -0.202, -0.914, 0.605, -0.618, 0.898, -0.615, -0.508, -0.223, -0.841, 0.396, -0.609, 0.746, 0.129, 0.724, 0.54, -0.936, 0.589, 0.874, -0.715, -0.353, 0.243)

56
Materials/SinFoam.tres Normal file
View File

@ -0,0 +1,56 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://bv06fr71bp0mr"]
[sub_resource type="Shader" id="Shader_n7650"]
code = "shader_type canvas_item;
render_mode world_vertex_coords;
uniform float foamHeight = 0.2;
uniform float speed = 1.0;
uniform sampler2D foamTex;
uniform float amplitude = 0.2;
uniform float frequency = 1.0;
uniform int waveCount = 5;
uniform float amplitudeScaleFactor = 0.82;
uniform float frequencyScaleFactor = 1.18;
uniform float dirs[40];
uniform sampler2D tex;
varying float yOffset;
float calcHeight(float x, float y){
float amp = 1.0;
float freq = 1.0;
float offset = 0.0;
for (int i = 0; i < waveCount; i++){
offset += amplitude * amp * pow(E, sin(frequency * freq * (speed * TIME + (dirs[2 * i] * x) + (dirs[2 * i + 1] * y))));
amp *= amplitudeScaleFactor;
freq *= frequencyScaleFactor;
}
return offset;
}
void vertex() {
yOffset = VERTEX.y;
VERTEX.y += calcHeight(VERTEX.x, VERTEX.y);
yOffset += VERTEX.y;
}
void fragment() {
COLOR = vec4(1.0, 1.0, 1.0, 0.5);
}
"
[sub_resource type="CanvasTexture" id="CanvasTexture_n7650"]
[resource]
resource_local_to_scene = true
shader = SubResource("Shader_n7650")
shader_parameter/foamHeight = 0.2
shader_parameter/speed = 1.0
shader_parameter/amplitude = 0.2
shader_parameter/frequency = 1.0
shader_parameter/waveCount = 5
shader_parameter/amplitudeScaleFactor = 0.82
shader_parameter/frequencyScaleFactor = 1.18
shader_parameter/dirs = PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
shader_parameter/tex = SubResource("CanvasTexture_n7650")

View File

@ -1,6 +1,6 @@
[gd_resource type="ShaderMaterial" load_steps=4 format=3 uid="uid://doo0lbnv6560s"]
[ext_resource type="Shader" uid="uid://cs1j12437apag" path="res://Shaders/Water2.gdshader" id="1_qkwc8"]
[ext_resource type="Shader" uid="uid://cs1j12437apag" path="res://Shaders/SinWater.gdshader" id="1_qkwc8"]
[sub_resource type="FastNoiseLite" id="FastNoiseLite_qwrmt"]
frequency = 0.0126

View File

@ -1,22 +0,0 @@
[gd_resource type="ShaderMaterial" load_steps=4 format=3 uid="uid://b1f46mvfaphsh"]
[ext_resource type="Shader" uid="uid://dm3to0j0v8mro" path="res://Shaders/Water.gdshader" id="1_33bm8"]
[sub_resource type="FastNoiseLite" id="FastNoiseLite_qwrmt"]
frequency = 0.0126
fractal_type = 2
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_roaec"]
noise = SubResource("FastNoiseLite_qwrmt")
[resource]
render_priority = 0
shader = ExtResource("1_33bm8")
shader_parameter/color = Vector3(0.1, 0.4, 0.8)
shader_parameter/reflectiveness = 10.0
shader_parameter/foamHeight = 0.515
shader_parameter/foamTex = SubResource("NoiseTexture2D_roaec")
shader_parameter/amplitudes = PackedFloat32Array(0.1, 0.02, 0.02, 0.01, 0.005, 0.2)
shader_parameter/waveSpeeds = PackedFloat32Array(0.5, 2, 2.2, 5, 10, 0.1)
shader_parameter/xAmplifiers = PackedFloat32Array(1, 3, -3, 15, 25, 0.1)
shader_parameter/yAmplifiers = PackedFloat32Array(0, 3, 2, -10, -20, 0.2)

29
Scenes/SinFoam.tscn Normal file
View File

@ -0,0 +1,29 @@
[gd_scene load_steps=4 format=3 uid="uid://b6hynk4kyhnqt"]
[ext_resource type="Shader" uid="uid://w33608b0vm7s" path="res://Shaders/SinFoam.gdshader" id="1_tfkfi"]
[sub_resource type="ViewportTexture" id="ViewportTexture_8ia8i"]
viewport_path = NodePath(".")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_3m5rl"]
resource_local_to_scene = true
shader = ExtResource("1_tfkfi")
shader_parameter/foamHeight = 0.2
shader_parameter/speed = 1.355
shader_parameter/amplitude = 0.085
shader_parameter/frequency = 1.0
shader_parameter/waveCount = 20
shader_parameter/amplitudeScaleFactor = 0.835
shader_parameter/frequencyScaleFactor = 1.145
shader_parameter/dirs = PackedFloat32Array(0.981, -0.588, 0.035, -0.977, -0.809, -0.351, 0.989, -0.24, -0.192, 0.907, -0.479, 0.499, 0.388, -0.97, 0.882, -0.845, 0.246, -0.068, -0.41, -0.202, -0.914, 0.605, -0.618, 0.898, -0.615, -0.508, -0.223, -0.841, 0.396, -0.609, 0.746, 0.129, 0.724, 0.54, -0.936, 0.589, 0.874, -0.715, -0.353, 0.243)
shader_parameter/tex = SubResource("ViewportTexture_8ia8i")
[node name="FoamTex" type="SubViewport"]
[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource("ShaderMaterial_3m5rl")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=22 format=3 uid="uid://cvgligyy2psi8"]
[gd_scene load_steps=21 format=3 uid="uid://bfy776lrcd0cl"]
[ext_resource type="Material" uid="uid://b1f46mvfaphsh" path="res://Materials/Water.tres" id="1_p35v0"]
[ext_resource type="Material" uid="uid://doo0lbnv6560s" path="res://Materials/Water2.tres" id="2_qhg81"]
[ext_resource type="Material" uid="uid://dipb5q5o7g4dx" path="res://Materials/GerstnerWater.tres" id="1_20nd5"]
[ext_resource type="PackedScene" uid="uid://b6hynk4kyhnqt" path="res://Scenes/SinFoam.tscn" id="2_y7q7p"]
[ext_resource type="Texture2D" uid="uid://v6apgwie2maw" path="res://autumn_field_puresky_4k.exr" id="3_vl8or"]
[ext_resource type="Script" uid="uid://ptic0lam0bio" path="res://Scripts/BuoyantBody.gd" id="4_f67eq"]
[ext_resource type="Shader" uid="uid://20hv5g0m3ym3" path="res://Shaders/TimeTest.gdshader" id="5_25la2"]
@ -10,15 +10,8 @@
[ext_resource type="Script" uid="uid://0f5o547ecwde" path="res://Scripts/WaveManager.gd" id="8_jes3c"]
[ext_resource type="PackedScene" uid="uid://lp8cghhpbii2" path="res://Models/SM_rubber_duck.FBX" id="9_t1a22"]
[sub_resource type="PlaneMesh" id="PlaneMesh_gqyhx"]
lightmap_size_hint = Vector2i(52, 52)
material = ExtResource("1_p35v0")
size = Vector2(10, 10)
subdivide_width = 200
subdivide_depth = 200
[sub_resource type="PlaneMesh" id="PlaneMesh_w7hsm"]
material = ExtResource("2_qhg81")
material = ExtResource("1_20nd5")
size = Vector2(50, 50)
subdivide_width = 1000
subdivide_depth = 1000
@ -68,19 +61,18 @@ points = PackedVector3Array(-0.0546342, -0.0177093, 0.0228165, -0.0511687, -0.01
[node name="WaterPlane" type="Node3D"]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
visible = false
mesh = SubResource("PlaneMesh_gqyhx")
[node name="MeshInstance3D2" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.400336, 0, 11.821)
cast_shadow = 0
mesh = SubResource("PlaneMesh_w7hsm")
[node name="FoamTex" parent="MeshInstance3D" instance=ExtResource("2_y7q7p")]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_8bg6o")
[node name="Cube" type="RigidBody3D" parent="." node_paths=PackedStringArray("samplePoints")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 11.4682, 1.58338, 8.16784)
visible = false
linear_damp = 1.5
angular_damp = 5.0
script = ExtResource("4_f67eq")
@ -118,6 +110,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, -0.5, -0.5)
[node name="Boat" type="RigidBody3D" parent="." node_paths=PackedStringArray("samplePoints")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 15.5064, 0.747465, 0.384609)
visible = false
linear_damp = 1.5
angular_damp = 5.0
script = ExtResource("4_f67eq")
@ -185,6 +178,7 @@ dirs = Array[Vector2]([Vector2(0.981, -0.588), Vector2(0.035, -0.977), Vector2(-
[node name="Duck" type="RigidBody3D" parent="." node_paths=PackedStringArray("samplePoints")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.97188, 1.86235, 2.06099)
visible = false
linear_damp = 1.5
angular_damp = 5.0
script = ExtResource("4_f67eq")

View File

@ -0,0 +1,14 @@
shader_type canvas_item;
void vertex() {
// Called for every vertex the material is visible on.
}
void fragment() {
// Called for every pixel the material is visible on.
}
//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

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

View File

@ -0,0 +1,66 @@
shader_type spatial;
render_mode world_vertex_coords;
uniform vec3 color = vec3(0.1, 0.4, 0.8);
uniform float speed = 1.0;
uniform float amplitude = 0.2;
uniform float frequency = 1.0;
uniform int waveCount = 5;
uniform float amplitudeScaleFactor = 0.82;
uniform float frequencyScaleFactor = 1.18;
uniform float dirs[40];
uniform sampler2D depth: hint_depth_texture;
varying float baseHeight;
vec3 calcHeight(float x, float y){
float amp = 1.0;
float freq = 1.0;
vec2 xOffset = vec2(0.0, 0.0);
float yOffset = 0.0;
for (int i = 0; i < waveCount; i++){
xOffset += amplitude * amp * cos((frequency * freq * (speed * TIME + (dirs[2 * i] * x) + (dirs[2 * i + 1] * y)))) * dirs[2 * i + 1];
yOffset += amplitude * amp * sin((frequency * freq * (speed * TIME + (dirs[2 * i] * x) + (dirs[2 * i + 1] * y))));
amp *= amplitudeScaleFactor;
freq *= frequencyScaleFactor;
}
return vec3(xOffset, yOffset);
}
void vertex() {
baseHeight = VERTEX.y;
vec3 offsets = calcHeight(VERTEX.x + 0.01, VERTEX.z);
vec3 b = vec3(VERTEX.x + 0.01 + offsets[0], VERTEX.y + offsets[2], VERTEX.z + offsets[1]);
offsets = calcHeight(VERTEX.x, VERTEX.z + 0.01);
vec3 c = vec3(VERTEX.x + offsets[0], VERTEX.y + offsets[2], VERTEX.z + offsets[1] + 0.01);
offsets = calcHeight(VERTEX.x, VERTEX.z);
VERTEX.y += offsets[2];
VERTEX.x += offsets[0];
VERTEX.z += offsets[1];
NORMAL = normalize(cross(c - VERTEX, b - VERTEX));
baseHeight += VERTEX.y;
}
void fragment() {
ALBEDO = color * clamp(baseHeight, 0.2, 11.0);
//SPECULAR = 1.0;
//float strength = baseHeight * texture(foamTex, UV).r;
//if (strength >= foamHeight) ALBEDO = ALBEDO + vec3(1, 1, 1) * (strength + foamHeight);
ALPHA = 0.9;
float dep = texture(depth, SCREEN_UV).x;
vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, dep);
vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
view.xyz /= view.w;
float linear_depth = -view.z;
float object_depth = FRAGCOORD.z;
vec3 object_ndc = vec3(SCREEN_UV * 2.0 - 1.0, object_depth);
vec4 object_view = INV_PROJECTION_MATRIX * vec4(object_ndc, 1.0);
object_view.xyz /= object_view.w;
float linear_object_depth = -object_view.z;
if (linear_depth - linear_object_depth <= 0.7){
discard;
//ALPHA = 0.9 * clamp(linear_depth - linear_object_depth - 0.7, 0.0, 1.0);
}
}

View File

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

26
Shaders/SinFoam.gdshader Normal file
View File

@ -0,0 +1,26 @@
shader_type canvas_item;
uniform float foamHeight = 0.2;
uniform float speed = 1.0;
uniform sampler2D foamTex;
uniform float amplitude = 0.2;
uniform float frequency = 1.0;
uniform int waveCount = 5;
uniform float amplitudeScaleFactor = 0.82;
uniform float frequencyScaleFactor = 1.18;
uniform float dirs[40];
uniform sampler2D tex;
void fragment() {
float amp = 1.0;
float freq = 1.0;
float offset = 0.0;
float x = (FRAGCOORD.x * 50.0) - 25.0;
float y = (FRAGCOORD.y * 50.0) - 25.0;
for (int i = 0; i < waveCount; i++){
offset += amplitude * amp * pow(E, sin(frequency * freq * (speed * TIME + (dirs[2 * i] * x) + (dirs[2 * i + 1] * y))));
amp *= amplitudeScaleFactor;
freq *= frequencyScaleFactor;
}
COLOR = vec4(1.0, 1.0, 1.0, offset);
}

View File

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

View File

@ -1,46 +0,0 @@
shader_type spatial;
render_mode world_vertex_coords;
uniform vec3 color = vec3(0.1, 0.4, 0.8);
uniform float reflectiveness = 10.0;
uniform float foamHeight = 0.2;
uniform sampler2D foamTex;
const int WAVE_COUNT = 6;
uniform float amplitudes[WAVE_COUNT];
uniform float waveSpeeds[WAVE_COUNT];
uniform float xAmplifiers[WAVE_COUNT];
uniform float yAmplifiers[WAVE_COUNT];
varying float yOffset;
float calcHeight(float x, float y){
float offset = 0.0;
for (int i = 0; i < WAVE_COUNT; i++){
offset += amplitudes[i] * pow(E, sin((waveSpeeds[i] * TIME) + (xAmplifiers[i] * (x)) + (yAmplifiers[i] * (y))));
}
return offset;
}
void vertex() {
yOffset = VERTEX.y;
vec3 b = vec3(VERTEX.x + 0.001, VERTEX.y + calcHeight(VERTEX.x + 0.001, VERTEX.z), VERTEX.z);
vec3 c = vec3(VERTEX.x, VERTEX.y + calcHeight(VERTEX.x, VERTEX.z + 0.001), VERTEX.z + 0.001);
VERTEX.y += calcHeight(VERTEX.x, VERTEX.z);
NORMAL = normalize(cross(c - VERTEX, b - VERTEX));
yOffset += VERTEX.y;
}
void fragment() {
ALBEDO = color * clamp(0.8 + yOffset, 0.2, 2);
float strength = yOffset * texture(foamTex, UV).r;
if (strength >= foamHeight) ALBEDO = ALBEDO + vec3(1, 1, 1) * (strength + foamHeight);
//ALPHA = 0.9;
//ALBEDO = (NORMAL + 1.0) / 2.0;
}
//void light() {
//DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR;
//vec3 halfVector = normalize(normalize(VIEW) + normalize(LIGHT));
//SPECULAR_LIGHT += clamp(pow(dot(NORMAL, halfVector), reflectiveness), 0.0, 1.0) * LIGHT_COLOR;
//}

View File

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

View File

@ -11,7 +11,6 @@ config_version=5
[application]
config/name="water shader"
run/main_scene="uid://cvgligyy2psi8"
config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg"