From d1e5b99e569113b0458edb5b0d71601d52ce9c47 Mon Sep 17 00:00:00 2001 From: yum Date: Mon, 1 Dec 2025 16:16:20 -0800 Subject: add basic shadows feature, a la liltoon --- 2ner.cginc | 2 +- 2ner.shader | 8 ++++++++ features.cginc | 4 ++++ globals.cginc | 6 ++++++ yum_lighting.cginc | 30 +++++++++++++++++++++++------- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/2ner.cginc b/2ner.cginc index 39d0fa8..7cbc96f 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -478,7 +478,6 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace #if defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS) || defined(OUTLINE_PASS) || defined(EXTRA_STENCIL_COLOR_PASS) YumLighting l = GetYumLighting(i, pbr); - //return float4(l.occlusion, l.occlusion, l.occlusion, 1); #if defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS) applyMatcapsAndRimLighting(i, pbr, l); @@ -487,6 +486,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace #endif pbr.albedo.rgb = visualizeInFalseColor(pbr.albedo.rgb); + pbr.albedo.rgb = applyQuasiShadows(pbr.albedo.rgb, l); #if defined(_UNLIT) float4 lit = pbr.albedo; diff --git a/2ner.shader b/2ner.shader index 558109d..b3fd0b1 100644 --- a/2ner.shader +++ b/2ner.shader @@ -2259,6 +2259,14 @@ Shader "yum_food/2ner" _Min_Brightness("Value", Range(0, 1)) = 0 [HideInInspector] m_end_Min_Brightness("Minimum brightness", Float) = 0 //endex + //ifex _Quasi_Shadows_Enabled==0 + [HideInInspector] m_start_Quasi_Shadows("Quasi Shadows", Float) = 0 + [ThryToggle(_QUASI_SHADOWS)] _Quasi_Shadows_Enabled("Enable", Float) = 0 + _Quasi_Shadows_0_Color("Color 0", Color) = (1, 1, 1, 1) + _Quasi_Shadows_0_Threshold("Threshold 0", Range(0, 1)) = 0.5 + _Quasi_Shadows_0_Width("Width 0", Range(0, 1)) = 0.1 + [HideInInspector] m_end_Quasi_Shadows("Quasi Shadows", Float) = 0 + //endex //ifex _Quantize_NoL_Enabled==0 [HideInInspector] m_start_Quantize_NoL("Quantize NoL", Float) = 0 [ThryToggle(_QUANTIZE_NOL)] _Quantize_NoL_Enabled("Enable", Float) = 0 diff --git a/features.cginc b/features.cginc index 63f2929..eedcc99 100644 --- a/features.cginc +++ b/features.cginc @@ -491,5 +491,9 @@ #pragma shader_feature_local _TROCHOID //endex +//ifex _Quasi_Shadows_Enabled==0 +#pragma shader_feature_local _QUASI_SHADOWS +//endex + #endif // __FEATURES_INC diff --git a/globals.cginc b/globals.cginc index 9b1cfe2..44d10d5 100644 --- a/globals.cginc +++ b/globals.cginc @@ -759,4 +759,10 @@ float _Trochoid_r_Power; texture2D _Trochoid_Color_Ramp; #endif // _TROCHOID +#if defined(_QUASI_SHADOWS) +float4 _Quasi_Shadows_0_Color; +float _Quasi_Shadows_0_Threshold; +float _Quasi_Shadows_0_Width; +#endif // _QUASI_SHADOWS + #endif // __GLOBALS_INC diff --git a/yum_lighting.cginc b/yum_lighting.cginc index 363e364..29b9846 100644 --- a/yum_lighting.cginc +++ b/yum_lighting.cginc @@ -266,7 +266,7 @@ float3 yumSH9(float4 n, float3 worldPos, inout YumLighting light) { L2 *= l2_wrap; #else float l1_wrap = 1.0f; -#endif +#endif // _WRAPPED_LIGHTING light.L00 = L00; light.L01r = unity_SHAr.xyz * l1_wrap; @@ -274,9 +274,16 @@ float3 yumSH9(float4 n, float3 worldPos, inout YumLighting light) { light.L01b = unity_SHAb.xyz * l1_wrap; return L0 + L1 + L2; -#else +#else // !YUM_SH9_STANDARD LightVolumeSH(worldPos, light.L00, light.L01r, light.L01g, light.L01b); + // Hack to get directional information from SH. + float3 light_dir = normalize(float3(luminance(light.L01r), luminance(light.L01g), luminance(light.L01b))); + light.derivedLight.l = light_dir; + light.derivedLight.colorIntensity = float4(light.L00, 1); + light.derivedLight.attenuation = 1; + light.derivedLight.NoL = saturate(dot(n.xyz, light_dir)); + #if defined(_WRAPPED_LIGHTING) float wrap_term = _Wrap_NoL_Diffuse_Strength; // Hack. Not energy preserving but sorta close. I think this looks better at fully flat mode. @@ -284,7 +291,7 @@ float3 yumSH9(float4 n, float3 worldPos, inout YumLighting light) { light.L01r *= l1_wrap; light.L01g *= l1_wrap; light.L01b *= l1_wrap; -#endif +#endif // _WRAPPED_LIGHTING return light.L00 + float3( dot(light.L01r, n.xyz), @@ -297,15 +304,24 @@ float4 getIndirectDiffuse(v2f i, float4 vertexLightColor, inout YumLighting light) { float4 diffuse = vertexLightColor; #if defined(FORWARD_BASE_PASS) -#if defined(LIGHTMAP_ON) - diffuse.xyz = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)); -#else diffuse.xyz += max(0, yumSH9(float4(i.normal, 0), i.worldPos, light)); -#endif #endif return diffuse; } +float3 applyQuasiShadows(float3 color, YumLighting light) { + float3 result = color; +#if defined(_QUASI_SHADOWS) + float NoL = light.derivedLight.NoL; + float threshold = _Quasi_Shadows_0_Threshold; + float width = _Quasi_Shadows_0_Width; + float3 shadow_color = _Quasi_Shadows_0_Color.rgb; + float interp = smoothstep(threshold - width, threshold + width, NoL); + result = lerp(color * shadow_color, color, interp); +#endif + return result; +} + YumLighting GetYumLighting(v2f i, YumPbr pbr) { YumLighting light = (YumLighting) 0; -- cgit v1.2.3