Got first draft of the water shader implemented

This commit is contained in:
nc5432 2025-01-31 14:55:49 -05:00
parent 558db5f9a6
commit 13116c5596
3 changed files with 80 additions and 0 deletions

21
Materials/Water.tres Normal file
View File

@ -0,0 +1,21 @@
[gd_resource type="ShaderMaterial" load_steps=4 format=3 uid="uid://b1f46mvfaphsh"]
[ext_resource type="Shader" 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/foamHeight = 0.085
shader_parameter/amplitudes = PackedFloat32Array(0.3, 0.2, 0.1, 0.01, 0.015)
shader_parameter/waveSpeeds = PackedFloat32Array(0.5, 2, 5, 10, 9)
shader_parameter/xAmplifiers = PackedFloat32Array(1, 2, 1, 10, 0)
shader_parameter/yAmplifiers = PackedFloat32Array(0, 0.5, 2, 10, 0)
shader_parameter/foamTex = SubResource("NoiseTexture2D_roaec")

17
Scenes/WaterPlane.tscn Normal file
View File

@ -0,0 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://cvgligyy2psi8"]
[ext_resource type="Material" uid="uid://b1f46mvfaphsh" path="res://Materials/Water.tres" id="1_p35v0"]
[sub_resource type="PlaneMesh" id="PlaneMesh_gqyhx"]
size = Vector2(5, 5)
subdivide_width = 50
subdivide_depth = 50
[node name="WaterPlane" type="Node3D"]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_gqyhx")
surface_material_override/0 = ExtResource("1_p35v0")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.765573, 0.643349, 0, -0.643349, 0.765573, 0, 0.851548, 0)

42
Shaders/Water.gdshader Normal file
View File

@ -0,0 +1,42 @@
shader_type spatial;
uniform vec3 color = vec3(0.1, 0.4, 0.8);
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] * sin((waveSpeeds[i] * TIME) + (xAmplifiers[i] * (x)) + (yAmplifiers[i] * (y)));
}
return offset;
}
void vertex() {
yOffset = VERTEX.y;
vec3 b = vec3(VERTEX.x, VERTEX.y + calcHeight(NODE_POSITION_WORLD.x + VERTEX.x + 0.01, NODE_POSITION_WORLD.z + VERTEX.z), VERTEX.z);
vec3 c = vec3(VERTEX.x, VERTEX.y + calcHeight(NODE_POSITION_WORLD.x + VERTEX.x, NODE_POSITION_WORLD.z + VERTEX.z + 0.1), VERTEX.z);
VERTEX.y += calcHeight(NODE_POSITION_WORLD.x + VERTEX.x, NODE_POSITION_WORLD.z + VERTEX.z);
NORMAL = normalize(cross(b - VERTEX, c - VERTEX));
yOffset += VERTEX.y;
}
void fragment() {
ALBEDO = color * clamp(1.0 + 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;
}
//void light() {
// Called for every pixel for every light affecting the material.
// Uncomment to replace the default light processing function with this one.
//}