From 09aa66c9c13965105133ca000da4d2c37d455877 Mon Sep 17 00:00:00 2001 From: yum Date: Wed, 23 Oct 2024 17:50:51 -0700 Subject: Refine origin damping behavior, fix normals precision 1E-3 is much too coarse for normal calculation. --- Editor/tooner.cs | 2 ++ gerstner.cginc | 21 +++++++++++++++++---- globals.cginc | 1 + tooner.shader | 1 + tooner_lighting.cginc | 3 +++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Editor/tooner.cs b/Editor/tooner.cs index 62d0032..2a6d805 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -2173,6 +2173,8 @@ public class ToonerGUI : ShaderGUI { FloatProperty(bc, "g"); bc = FindProperty("_Gimmick_Gerstner_Water_Scale"); VectorProperty(bc, "Scale"); + bc = FindProperty("_Gimmick_Gerstner_Water_Origin_Damping_Direction"); + FloatProperty(bc, "Origin damping direction"); EditorGUI.indentLevel -= 1; } diff --git a/gerstner.cginc b/gerstner.cginc index da7d2f5..e6ed3c2 100644 --- a/gerstner.cginc +++ b/gerstner.cginc @@ -102,10 +102,23 @@ float3 compute_gerstner(float3 pp, GerstnerParams p) } const float3 raw_result = float3(g_xi / p.scale.x, g_eta / p.scale.y, g_zeta * p.scale.z); - const float origin_damping_factor = 1 - pow(0.5, max(0, length(raw_result)*5000-2)); + const float3 raw_result_world = mul(unity_ObjectToWorld, float4(raw_result, 1)).xyz; + float3 result_world = raw_result_world; - float3 result = raw_result; - result.z *= origin_damping_factor; + if (_Gimmick_Gerstner_Water_Origin_Damping_Direction > 0) { + result_world.y = max(_Gimmick_Gerstner_Water_Origin_Damping_Direction, result_world.y); + } else { + result_world.y = min(_Gimmick_Gerstner_Water_Origin_Damping_Direction, result_world.y); + } + + result_world.y = lerp(result_world.y, raw_result_world.y, + // If within 20m cylindrical distance, apply 100m wide damping. + // TODO parameterize this! + saturate((length(raw_result_world.xz) - 20) * 0.01) * + // Only enable if mesh is on the wrong side of the damping vector. + (sign(raw_result_world.y - _Gimmick_Gerstner_Water_Origin_Damping_Direction) != sign(_Gimmick_Gerstner_Water_Origin_Damping_Direction))); + + float3 result = mul(unity_WorldToObject, float4(result_world, 1)).xyz; return result; } @@ -117,7 +130,7 @@ float3 gerstner_vert(float3 pp, GerstnerParams p) float3 gerstner_frag(float3 pp, GerstnerParams p) { const float3 g0 = compute_gerstner(pp, p); - const float3 e = float3(.001, 0, 0); + const float3 e = float3(1E-8, 0, 0); const float3 g0_da = compute_gerstner(pp + e.xyz, p); const float3 g0_db = compute_gerstner(pp + e.yxz, p); const float3 ds_da = g0_da - g0; diff --git a/globals.cginc b/globals.cginc index 044d9fe..6800b7b 100644 --- a/globals.cginc +++ b/globals.cginc @@ -752,6 +752,7 @@ float4 _Gimmick_Gerstner_Water_p; float4 _Gimmick_Gerstner_Water_k_x; float4 _Gimmick_Gerstner_Water_k_y; float4 _Gimmick_Gerstner_Water_t_f; +float _Gimmick_Gerstner_Water_Origin_Damping_Direction; #if defined(_GIMMICK_GERSTNER_WATER_OCTAVE_1) float4 _Gimmick_Gerstner_Water_a1; float4 _Gimmick_Gerstner_Water_p1; diff --git a/tooner.shader b/tooner.shader index bb9a65a..f6beeee 100644 --- a/tooner.shader +++ b/tooner.shader @@ -673,6 +673,7 @@ Shader "yum_food/tooner" _Gimmick_Gerstner_Water_h("Mean water depth", Float) = 10 _Gimmick_Gerstner_Water_g("Gravity", Float) = 9.8 _Gimmick_Gerstner_Water_Scale("Scale", Vector) = (1000, 1000, .1) + _Gimmick_Gerstner_Water_Origin_Damping_Direction("Origin damping direction", Float) = 1 } SubShader { diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc index 5d055c0..87cf2e4 100644 --- a/tooner_lighting.cginc +++ b/tooner_lighting.cginc @@ -1297,6 +1297,9 @@ float4 effect(inout v2f i, out float depth) } #endif #if defined(_GIMMICK_GERSTNER_WATER) +#if defined(_EXPLODE) + if (_Explode_Phase < 1E-6) +#endif { GerstnerParams p = getGerstnerParams(); i.normal = UnityObjectToWorldNormal(gerstner_frag(i.objPos.xyz, p)); -- cgit v1.2.3