From 08762a99117fdddb3b48b450d02dd515b3eb78af Mon Sep 17 00:00:00 2001 From: yum Date: Fri, 7 Mar 2025 13:48:04 -0800 Subject: Add fallback cubemap feature --- 2ner.shader | 8 ++++++++ features.cginc | 4 ++++ globals.cginc | 7 +++++++ poi.cginc | 7 +++++++ yum_lighting.cginc | 22 +++++++++++++++++++--- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/2ner.shader b/2ner.shader index 0574e7b..ea84e4d 100644 --- a/2ner.shader +++ b/2ner.shader @@ -601,6 +601,14 @@ Shader "yum_food/2ner" //endex [HideInInspector] m_lightingOptions("Lighting Options", Float) = 0 + //ifex _Fallback_Cubemap_Enabled==0 + [HideInInspector] m_start_Fallback_Cubemap("Fallback Cubemap", Float) = 0 + [ThryToggle(_FALLBACK_CUBEMAP)] _Fallback_Cubemap_Enabled("Enable", Float) = 0 + [MaterialToggle] _Fallback_Cubemap_Force("Force", Float) = 0 + _Fallback_Cubemap("Cubemap", Cube) = "" {} + _Fallback_Cubemap_Brightness("Brightness", Float) = 1.0 + [HideInInspector] m_end_Fallback_Cubemap("Fallback Cubemap", Float) = 0 + //endex //ifex _Receive_Shadows_Enabled==0 [HideInInspector] m_start_Shadow_Receiving("Receive shadows", Float) = 0 [ThryToggle(_RECEIVE_SHADOWS)] _Receive_Shadows_Enabled("Enable", Float) = 1 diff --git a/features.cginc b/features.cginc index e6782f6..820c045 100644 --- a/features.cginc +++ b/features.cginc @@ -22,6 +22,10 @@ #pragma shader_feature_local _EMISSION //endex +//ifex _Fallback_Cubemap_Enabled==0 +#pragma shader_feature_local _FALLBACK_CUBEMAP +//endex + //ifex _Wrapped_Lighting_Enabled==0 #pragma shader_feature_local _WRAPPED_LIGHTING //endex diff --git a/globals.cginc b/globals.cginc index a805830..a75f747 100644 --- a/globals.cginc +++ b/globals.cginc @@ -26,6 +26,13 @@ float4 _EmissionMap_ST; float3 _EmissionColor; #endif +#if defined(_FALLBACK_CUBEMAP) +UNITY_DECLARE_TEXCUBE(_Fallback_Cubemap); +half4 _Fallback_Cubemap_HDR; +float _Fallback_Cubemap_Brightness; +float _Fallback_Cubemap_Force; +#endif + #if defined(_AMBIENT_OCCLUSION) sampler2D _OcclusionMap; float _OcclusionStrength; diff --git a/poi.cginc b/poi.cginc index 7925f5e..df89bbf 100644 --- a/poi.cginc +++ b/poi.cginc @@ -123,4 +123,11 @@ float3 getPoiLightingIndirect() { return BetterSH9(float4(0, 0, 0, 1)); } +bool SceneHasReflections() +{ + float width, height; + unity_SpecCube0.GetDimensions(width, height); + return !(width * height < 2); +} + #endif // __POI_INC diff --git a/yum_lighting.cginc b/yum_lighting.cginc index 8570488..759eee6 100644 --- a/yum_lighting.cginc +++ b/yum_lighting.cginc @@ -100,7 +100,7 @@ float3 getDirectLightDirection(v2f i) { } float GetLodRoughness(float roughness) { - return (1.0 / UNITY_SPECCUBE_LOD_STEPS) * roughness * (1.7 - 0.7 * roughness); + return roughness * (1.7 - 0.7 * roughness); } float3 getIndirectSpecular(v2f i, YumPbr pbr, float3 view_dir) { @@ -122,8 +122,24 @@ float3 getIndirectSpecular(v2f i, YumPbr pbr, float3 view_dir) { data.boxMin[1] = unity_SpecCube1_BoxMin; data.probePosition[1] = unity_SpecCube1_ProbePosition; #endif - return UnityGI_prefilteredRadiance(data, pbr.roughness_perceptual, - reflect_dir); + +#if defined(_FALLBACK_CUBEMAP) + // Check if there's no valid scene cubemap + if (!SceneHasReflections() || _Fallback_Cubemap_Force) { + // Set up data for fallback sampling similar to Unity's system + half3 reflectVector = reflect(-view_dir, pbr.normal); + + #ifdef UNITY_SPECCUBE_BOX_PROJECTION + reflectVector = BoxProjectedCubemapDirection(reflectVector, data.worldPos, data.probePosition[0], data.boxMin[0], data.boxMax[0]); + #endif + + half mip = roughness * UNITY_SPECCUBE_LOD_STEPS; + float4 envSample = UNITY_SAMPLE_TEXCUBE_LOD(_Fallback_Cubemap, reflectVector, mip); + return DecodeHDR(envSample, _Fallback_Cubemap_HDR) * _Fallback_Cubemap_Brightness; + } +#endif + + return UnityGI_prefilteredRadiance(data, roughness, reflect_dir); } float4 getIndirectDiffuse(v2f i, float4 vertexLightColor) { -- cgit v1.2.3