From 93f27e3a8545fef6ccc293d033e5f422ed821215 Mon Sep 17 00:00:00 2001 From: yum Date: Sun, 2 Nov 2025 09:19:50 -0800 Subject: generalize norm conversion code --- 3ner.cginc | 4 +--- 3ner.shader | 12 +++++++----- features.cginc | 4 ++-- globals.cginc | 8 +++++--- vertex.cginc | 38 ++++++++++++++++++++------------------ vertex_deformation.slang | 23 ++++++++++------------- 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/3ner.cginc b/3ner.cginc index 28facdc..24cf52d 100644 --- a/3ner.cginc +++ b/3ner.cginc @@ -266,8 +266,6 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace) : SV_Target { i.tangent.xyz = UnityObjectToWorldNormal(i.tangent.xyz); i.normal *= facing ? 1 : -1; - i.normal = normalize(i.normal); - i.tangent.xyz = normalize(i.tangent.xyz); Pbr pbr = getPbr(i); #if defined(_UNLIT) @@ -278,7 +276,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace) : SV_Target { GetLighting(i, pbr, light_data); #if 0 - float c = light_data.direct.NoL; + float c = light_data.common.NoV; return float4(c,c,c,1); #endif return brdf(pbr, light_data); diff --git a/3ner.shader b/3ner.shader index e3b9ae9..f84b3fe 100644 --- a/3ner.shader +++ b/3ner.shader @@ -117,11 +117,13 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Vertex_Deformation_Tubes("Tubes", Float) = 0 //endex - //ifex _Vertex_Deformation_L2_Linf_Enabled==0 - [HideInInspector] m_start_Vertex_Deformation_L2_Linf("L2 -> L inf", Float) = 0 - [ThryToggle(_VERTEX_DEFORMATION_L2_LINF)] _Vertex_Deformation_L2_Linf_Enabled("Enable", Float) = 0 - _Vertex_Deformation_L2_Linf_t("t", Range(0,1)) = 0 - [HideInInspector] m_end_Vertex_Deformation_L2_Linf("L2 -> L inf", Float) = 0 + //ifex _Vertex_Deformation_Norm_Conversion_Enabled==0 + [HideInInspector] m_start_Vertex_Deformation_Norm_Conversion("Norm Conversion", Float) = 0 + [ThryToggle(_VERTEX_DEFORMATION_NORM_CONVERSION)] _Vertex_Deformation_Norm_Conversion_Enabled("Enable", Float) = 0 + _Vertex_Deformation_Norm_Conversion_Input_k("Input norm", Range(-10, 10)) = 2 + _Vertex_Deformation_Norm_Conversion_Output_k("Output norm", Range(-10, 10)) = 1 + _Vertex_Deformation_Norm_Conversion_t("t", Range(0,1)) = 0 + [HideInInspector] m_end_Vertex_Deformation_Norm_Conversion("Norm Conversion", Float) = 0 //endex //ifex _Vertex_Deformation_Seal_Enabled==0 diff --git a/features.cginc b/features.cginc index b00c86b..cd9a8d7 100644 --- a/features.cginc +++ b/features.cginc @@ -36,8 +36,8 @@ #pragma shader_feature_local _VERTEX_DEFORMATION_XY_TUBE //endex -//ifex _Vertex_Deformation_L2_Linf_Enabled==0 -#pragma shader_feature_local _VERTEX_DEFORMATION_L2_LINF +//ifex _Vertex_Deformation_L2_L1_Enabled==0 +#pragma shader_feature_local _VERTEX_DEFORMATION_NORM_CONVERSION //endex //ifex _Vertex_Deformation_Seal_Enabled==0 diff --git a/globals.cginc b/globals.cginc index 6513c32..92fe8fd 100644 --- a/globals.cginc +++ b/globals.cginc @@ -70,9 +70,11 @@ float _Vertex_Deformation_XZ_Tube_t; float _Vertex_Deformation_YZ_Tube_t; #endif // _VERTEX_DEFORMATION_YZ_TUBE -#if defined(_VERTEX_DEFORMATION_L2_LINF) -float _Vertex_Deformation_L2_Linf_t; -#endif // _VERTEX_DEFORMATION_L2_LINF +#if defined(_VERTEX_DEFORMATION_NORM_CONVERSION) +float _Vertex_Deformation_Norm_Conversion_Input_k; +float _Vertex_Deformation_Norm_Conversion_Output_k; +float _Vertex_Deformation_Norm_Conversion_t; +#endif // _VERTEX_DEFORMATION_NORM_CONVERSION #if defined(_VERTEX_DEFORMATION_SEAL) float _Vertex_Deformation_Seal_A; diff --git a/vertex.cginc b/vertex.cginc index 1cbeb6d..634abb1 100644 --- a/vertex.cginc +++ b/vertex.cginc @@ -59,18 +59,20 @@ #define VERTEX_DEFORM_XY_TUBE_NORM #endif // VERTEX_DEFORMATION_XY_TUBE -#if defined(_VERTEX_DEFORMATION_L2_LINF) -#define VERTEX_DEFORM_L2_LINF_PREAMBLE \ - float t = _Vertex_Deformation_L2_Linf_t -#define VERTEX_DEFORM_L2_LINF_POS \ - objPos = l2_to_linf(objPos.xyz, t) -#define VERTEX_DEFORM_L2_LINF_NORM \ - l2_to_linf_normal(objPos, objNorm, objTan, t) +#if defined(_VERTEX_DEFORMATION_NORM_CONVERSION) +#define VERTEX_DEFORM_NORM_CONVERSION_PREAMBLE \ + float t = _Vertex_Deformation_Norm_Conversion_t; \ + float input_k = _Vertex_Deformation_Norm_Conversion_Input_k; \ + float output_k = _Vertex_Deformation_Norm_Conversion_Output_k +#define VERTEX_DEFORM_NORM_CONVERSION_POS \ + objPos = norm_conversion(objPos.xyz, input_k, output_k, t) +#define VERTEX_DEFORM_NORM_CONVERSION_NORM \ + norm_conversion_normal(objPos, objNorm, objTan, input_k, output_k, t) #else -#define VERTEX_DEFORM_L2_LINF_PREAMBLE -#define VERTEX_DEFORM_L2_LINF_POS -#define VERTEX_DEFORM_L2_LINF_NORM -#endif // _VERTEX_DEFORMATION_L2_LINF +#define VERTEX_DEFORM_NORM_CONVERSION_PREAMBLE +#define VERTEX_DEFORM_NORM_CONVERSION_POS +#define VERTEX_DEFORM_NORM_CONVERSION_NORM +#endif // _VERTEX_DEFORMATION_NORM_CONVERSION #if defined(_VERTEX_DEFORMATION_SEAL) #define VERTEX_DEFORM_SEAL_PREAMBLE \ @@ -138,8 +140,8 @@ void deform(inout float3 objPos) { VERTEX_DEFORM_XY_TUBE_POS; } { - VERTEX_DEFORM_L2_LINF_PREAMBLE; - VERTEX_DEFORM_L2_LINF_POS; + VERTEX_DEFORM_NORM_CONVERSION_PREAMBLE; + VERTEX_DEFORM_NORM_CONVERSION_POS; } { VERTEX_DEFORM_SEAL_PREAMBLE; @@ -170,8 +172,8 @@ void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTa VERTEX_DEFORM_XY_TUBE_NORM; } { - VERTEX_DEFORM_L2_LINF_PREAMBLE; - VERTEX_DEFORM_L2_LINF_NORM; + VERTEX_DEFORM_NORM_CONVERSION_PREAMBLE; + VERTEX_DEFORM_NORM_CONVERSION_NORM; } { VERTEX_DEFORM_SEAL_PREAMBLE; @@ -228,10 +230,10 @@ void undeform_normal(inout float3 objPos, inout float3 objNorm, inout float3 obj posHistory[posCount++] = objPos; } #endif -#if defined(_VERTEX_DEFORMATION_L2_LINF) +#if defined(_VERTEX_DEFORMATION_NORM_CONVERSION) { - VERTEX_DEFORM_L2_LINF_PREAMBLE; - VERTEX_DEFORM_L2_LINF_POS; + VERTEX_DEFORM_NORM_CONVERSION_PREAMBLE; + VERTEX_DEFORM_NORM_CONVERSION_POS; posHistory[posCount++] = objPos; } #endif diff --git a/vertex_deformation.slang b/vertex_deformation.slang index 5337e8a..1af5ec9 100644 --- a/vertex_deformation.slang +++ b/vertex_deformation.slang @@ -171,25 +171,22 @@ public void sine_wave_undeform_normal(float3 xyz, inout float3 normal, R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent); } -// Map from l2 (euclidean) norm to l-infinity (supremum, max) norm. [Differentiable] -public float3 l2_to_linf(float3 xyz, no_diff float t) { - float l2 = length(xyz); - float3 abs_xyz = abs(xyz); - float linf = max(abs_xyz.x, max(abs_xyz.y, abs_xyz.z)); - - float scale = lerp(1.0f, linf / l2, t); - +public float3 norm_conversion(float3 xyz, no_diff float input_k, no_diff float output_k, no_diff float t) { + float3 xyz_abs = abs(xyz)+1e-3f; + float lin = pow(pow(xyz_abs.x, input_k) + pow(xyz_abs.y, input_k) + pow(xyz_abs.z, input_k), 1.0f / input_k); + float lout = pow(pow(xyz_abs.x, output_k) + pow(xyz_abs.y, output_k) + pow(xyz_abs.z, output_k), 1.0f / output_k); + float scale = lerp(1.0f, lout / lin, t); return xyz * scale; } -public void l2_to_linf_normal(float3 xyz, inout float3 normal, inout float3 tangent, float t) { - R3R3_NORMALS(xyz, normal, tangent, l2_to_linf, t); +public void norm_conversion_normal(inout float3 xyz, inout float3 normal, inout float3 tangent, float input_k, float output_k, float t) { + R3R3_NORMALS(xyz, normal, tangent, norm_conversion, input_k, output_k, t); } -public void l2_to_linf_undeform_normal(float3 xyz, inout float3 normal, inout float3 tangent, float t) { +public void norm_conversion_undeform_normal(float3 xyz, inout float3 normal, inout float3 tangent, float input_k, float output_k, float t) { R3R3_DECLARE_BASIS_VECTORS(xyz); - R3R3_AUTODIFF_BASIS_VECTORS(l2_to_linf, t); + R3R3_AUTODIFF_BASIS_VECTORS(norm_conversion, input_k, output_k, t); R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent); } @@ -200,7 +197,7 @@ float3 rand3_hash3(float3 p) p = float3(dot(p, float3(127.1, 311.7, 74.7)), dot(p, float3(269.5, 183.3, 246.1)), dot(p, float3(113.5, 271.9, 124.6))); - return frac(sin(p) * 43758.5453123); + return frac(sin(p) * 43758.5453123)*2-1; } [Differentiable] -- cgit v1.2.3