Foam work

This commit is contained in:
nc5432 2025-04-15 11:03:33 -04:00
parent 5927508474
commit 5665737c76
9 changed files with 71 additions and 13 deletions

View File

@ -1,14 +1,14 @@
[gd_scene load_steps=4 format=3 uid="uid://b6hynk4kyhnqt"]
[ext_resource type="Script" uid="uid://13xeofn8faqe" path="res://Scripts/CopyTexture.gd" id="1_8ia8i"]
[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/foamDecayRate = 0.2
shader_parameter/foamGrowth = 0.5
shader_parameter/foamHeight = 0.58
shader_parameter/speed = 1.355
shader_parameter/amplitude = 0.085
shader_parameter/frequency = 1.0
@ -16,9 +16,10 @@ 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"]
render_target_clear_mode = 1
script = ExtResource("1_8ia8i")
[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource("ShaderMaterial_3m5rl")
@ -27,3 +28,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="TextureRect" type="TextureRect" parent="."]
offset_right = 40.0
offset_bottom = 40.0

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=21 format=3 uid="uid://bfy776lrcd0cl"]
[gd_scene load_steps=23 format=3 uid="uid://bfy776lrcd0cl"]
[ext_resource type="Material" uid="uid://dipb5q5o7g4dx" path="res://Materials/GerstnerWater.tres" id="1_20nd5"]
[ext_resource type="Shader" uid="uid://cs1j12437apag" path="res://Shaders/SinWater.gdshader" id="1_y7q7p"]
[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"]
@ -10,8 +10,26 @@
[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="ViewportTexture" id="ViewportTexture_y7q7p"]
viewport_path = NodePath("MeshInstance3D/FoamTex")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_0u5pq"]
resource_local_to_scene = true
render_priority = 0
shader = ExtResource("1_y7q7p")
shader_parameter/color = Vector3(0.05, 0.2, 0.4)
shader_parameter/foamHeight = 0.58
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/foam = SubResource("ViewportTexture_y7q7p")
[sub_resource type="PlaneMesh" id="PlaneMesh_w7hsm"]
material = ExtResource("1_20nd5")
material = SubResource("ShaderMaterial_0u5pq")
size = Vector2(50, 50)
subdivide_width = 1000
subdivide_depth = 1000

11
Scripts/CopyTexture.gd Normal file
View File

@ -0,0 +1,11 @@
extends SubViewport
@onready var textureRect: TextureRect = $TextureRect
@onready var shader: ShaderMaterial
func _ready() -> void:
shader = $ColorRect.material
func _process(delta: float) -> void:
textureRect.texture = get_texture()
shader.set_shader_parameter("tex", textureRect.texture)

View File

@ -0,0 +1 @@
uid://13xeofn8faqe

View File

@ -0,0 +1,4 @@
extends Node
func _process(delta: float) -> void:
RenderingServer.global_shader_parameter_set("DELTA", delta)

View File

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

View File

@ -1,5 +1,7 @@
shader_type canvas_item;
uniform float foamDecayRate = 0.2;
uniform float foamGrowth = 0.5;
uniform float foamHeight = 0.2;
uniform float speed = 1.0;
uniform sampler2D foamTex;
@ -10,17 +12,22 @@ uniform float amplitudeScaleFactor = 0.82;
uniform float frequencyScaleFactor = 1.18;
uniform float dirs[40];
uniform sampler2D tex;
//global uniform float DELTA;
void fragment() {
//float brightness = texture(tex, UV).r - (foamDecayRate * DELTA);
float brightness = texture(tex, UV).r * foamDecayRate;
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;
float x = (UV.x * 50.0) - 25.0;
float y = (UV.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);
//if (offset >= foamHeight * 1.8) brightness += foamGrowth * DELTA;
if (offset >= foamHeight * 1.8) brightness += foamGrowth;
COLOR = vec4(brightness, brightness, brightness, 1.0);
}

View File

@ -11,6 +11,7 @@ uniform int waveCount = 5;
uniform float amplitudeScaleFactor = 0.82;
uniform float frequencyScaleFactor = 1.18;
uniform float dirs[40];
uniform sampler2D foam;
uniform sampler2D depth: hint_depth_texture;
@ -39,9 +40,12 @@ void vertex() {
void fragment() {
ALBEDO = color * clamp(yOffset, 0.2, 11.0);
vec4 foamLevel = texture(foam, UV);
ALBEDO += vec3(foamLevel.r, foamLevel.r, foamLevel.r);
//SPECULAR = 1.0;
float strength = yOffset * texture(foamTex, UV).r;
if (strength >= foamHeight) ALBEDO = ALBEDO + vec3(1, 1, 1) * (strength + foamHeight);
//float strength = yOffset * 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);

View File

@ -30,3 +30,10 @@ enabled=PackedStringArray()
[physics]
3d/physics_engine="Jolt Physics"
[shader_globals]
DELTA={
"type": "float",
"value": 0.0
}