summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-11-09 16:48:18 -0800
committeryum <yum.food.vr@gmail.com>2025-11-09 16:48:18 -0800
commit66fe6def0c13e24f6cc95915371ad132aac35bf9 (patch)
tree4e5ef25e46f50fbe893e05c94d5833943d73986f
parente7008baac77c82d2865e0d54928269528be034d2 (diff)
spotlight, clearcoat bugfixes; and add third matcap
-rw-r--r--2ner.shader24
-rw-r--r--features.cginc6
-rw-r--r--globals.cginc15
-rw-r--r--matcaps.cginc24
-rw-r--r--yum_brdf.cginc26
-rw-r--r--yum_lighting.cginc34
6 files changed, 98 insertions, 31 deletions
diff --git a/2ner.shader b/2ner.shader
index 236138e..21bc244 100644
--- a/2ner.shader
+++ b/2ner.shader
@@ -201,6 +201,30 @@ Shader "yum_food/2ner"
//endex
[HideInInspector] m_end_Matcap1("Matcap 1", Float) = 0
//endex
+ //ifex _Matcap2_Enabled==0
+ [HideInInspector] m_start_Matcap2("Matcap 2", Float) = 0
+ [ThryToggle(_MATCAP2)]_Matcap2_Enabled("Enable", Float) = 0
+ _Matcap2("Matcap", 2D) = "white" {}
+ [Toggle(_)]_Matcap2_Invert("Invert", Float) = 0
+ [ThryWideEnum(Replace, 0, Add, 1, Multiply, 2, Subtract, 3, AddProduct, 4)]
+ _Matcap2_Mode("Mode", Int) = 0
+ [ThryWideEnum(_0000b, 0, _0001b, 1, _0010b, 2, _0011b, 3, _0100b, 4, _0101b, 5, _0110b, 6, _0111b, 7, _1000b, 8, _1001b, 9, _1010b, 10, _1011b, 11, _1100b, 12, _1101b, 13, _1110b, 14, _1111b, 15)]
+ _Matcap2_Target_Mask("Target mask (albedo|diffuse<<1|specular<<2)", Int) = 1
+ _Matcap2_Strength("Strength", Float) = 1
+ //ifex _Matcap2_Mask_Enabled==0
+ [HideInInspector] m_start_Matcap2_Mask("Mask", Float) = 0
+ [ThryToggle(_MATCAP2_MASK)]_Matcap2_Mask_Enabled("Enable", Float) = 0
+ _Matcap2_Mask("Mask", 2D) = "white" {}
+ [HideInInspector] m_end_Matcap2_Mask("Mask", Float) = 0
+ //endex
+ //ifex _Matcap2_Quantization_Enabled==0
+ [HideInInspector] m_start_Matcap2_Quantization("Quantization", Float) = 0
+ [ThryToggle(_MATCAP2_QUANTIZATION)]_Matcap2_Quantization_Enabled("Enable", Float) = 0
+ _Matcap2_Quantization_Steps("Steps", Float) = 1
+ [HideInInspector] m_end_Matcap2_Quantization("Quantization", Float) = 0
+ //endex
+ [HideInInspector] m_end_Matcap2("Matcap 2", Float) = 0
+ //endex
[HideInInspector] m_end_Matcaps("Matcaps", Float) = 0
[HideInInspector] m_start_Rim_Lighting("Rim lighting", Float) = 0
diff --git a/features.cginc b/features.cginc
index 8467c62..696a08e 100644
--- a/features.cginc
+++ b/features.cginc
@@ -86,6 +86,12 @@
#pragma shader_feature_local _MATCAP1_QUANTIZATION
//endex
+//ifex _Matcap2_Enabled==0
+#pragma shader_feature_local _MATCAP2
+#pragma shader_feature_local _MATCAP2_MASK
+#pragma shader_feature_local _MATCAP2_QUANTIZATION
+//endex
+
//ifex _Rim_Lighting0_Enabled==0
#pragma shader_feature_local _RIM_LIGHTING0
#pragma shader_feature_local _RIM_LIGHTING0_MASK
diff --git a/globals.cginc b/globals.cginc
index 25b5fc6..7713f8c 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -178,6 +178,21 @@ float _Matcap1_Quantization_Steps;
#endif
#endif
+#if defined(_MATCAP2)
+texture2D _Matcap2;
+uint _Matcap2_Mode;
+uint _Matcap2_Target_Mask;
+float _Matcap2_Invert;
+float _Matcap2_Strength;
+#if defined(_MATCAP2_MASK)
+texture2D _Matcap2_Mask;
+float4 _Matcap2_Mask_ST;
+#endif
+#if defined(_MATCAP2_QUANTIZATION)
+float _Matcap2_Quantization_Steps;
+#endif
+#endif
+
#if defined(_RIM_LIGHTING0)
uint _Rim_Lighting0_Mode;
float _Rim_Lighting0_Center;
diff --git a/matcaps.cginc b/matcaps.cginc
index 00a4ea6..6c84e54 100644
--- a/matcaps.cginc
+++ b/matcaps.cginc
@@ -7,7 +7,7 @@
#include "yum_lighting.cginc"
#include "yum_pbr.cginc"
-#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) || defined(_RIM_LIGHTING2) || defined(_RIM_LIGHTING3)
+#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_MATCAP2) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) || defined(_RIM_LIGHTING2) || defined(_RIM_LIGHTING3)
float2 getMatcapUV(v2f i, inout YumPbr pbr) {
const float3 cam_normal = normalize(mul(UNITY_MATRIX_V, float4(pbr.normal, 0)));
const float3 cam_view_dir = normalize(mul(UNITY_MATRIX_V, float4(-i.eyeVec.xyz, 0)));
@@ -69,7 +69,7 @@ float getAngleAttenuation(float2 muv, float2 target_vector, float power) {
}
void applyMatcapsAndRimLighting(v2f i, inout YumPbr pbr, inout YumLighting l) {
-#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) || defined(_RIM_LIGHTING2) || defined(_RIM_LIGHTING3)
+#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_MATCAP2) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) || defined(_RIM_LIGHTING2) || defined(_RIM_LIGHTING3)
float2 muv = getMatcapUV(i, pbr);
#endif
#if defined(_MATCAP0)
@@ -112,6 +112,26 @@ void applyMatcapsAndRimLighting(v2f i, inout YumPbr pbr, inout YumLighting l) {
#endif
applyMatcap(pbr, l, m1, _Matcap1_Mode, _Matcap1_Target_Mask, m1_mask);
#endif // _MATCAP1
+#if defined(_MATCAP2)
+#if defined(_MATCAP2_QUANTIZATION)
+ float2 m2uv = muv * 2 - 1;
+ float m2uv_r = length(m2uv);
+ float m2uv_r_q = floor(m2uv_r * _Matcap2_Quantization_Steps) / _Matcap2_Quantization_Steps;
+ m2uv = m2uv_r_q * (m2uv / max(1E-4, m2uv_r));
+ m2uv = m2uv * 0.5 + 0.5;
+#else
+ float2 m2uv = muv;
+#endif // _MATCAP2_QUANTIZATION
+ float3 m2 = _Matcap2.Sample(linear_repeat_s, m2uv);
+ m2 = lerp(m2, 1 - m2, _Matcap2_Invert);
+ m2 *= _Matcap2_Strength;
+#if defined(_MATCAP2_MASK)
+ float m2_mask = _Matcap2_Mask.Sample(linear_repeat_s, UV_SCOFF(i, _Matcap2_Mask_ST, /*which_channel=*/0));
+#else
+ float m2_mask = 1;
+#endif
+ applyMatcap(pbr, l, m2, _Matcap2_Mode, _Matcap2_Target_Mask, m2_mask);
+#endif // _MATCAP2
#if defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) || defined(_RIM_LIGHTING2) || defined(_RIM_LIGHTING3)
float rl_radius = length(muv - 0.5);
diff --git a/yum_brdf.cginc b/yum_brdf.cginc
index 5cf962b..b8d4d0b 100644
--- a/yum_brdf.cginc
+++ b/yum_brdf.cginc
@@ -145,7 +145,7 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
direct_specular_cc = max(0, direct_specular_cc);
// Energy conservation: reduce base layer contribution by clearcoat Fresnel
- remainder -= Fcc * _Clearcoat_Strength;
+ remainder -= Fcc;
remainder = max(0, remainder);
#endif
@@ -191,7 +191,7 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
}
#endif
- float3 indirect_standard;
+ float3 indirect_standard = 0;
{
float remainder = 1.0f;
@@ -224,6 +224,20 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
#endif
float3 cc_env_refl = UnityGI_prefilteredRadiance(cc_data, cc_roughness_perceptual, cc_reflect_dir);
+#if defined(_FALLBACK_CUBEMAP)
+ if (!SceneHasReflections() || _Fallback_Cubemap_Force) {
+ // Set up data for fallback sampling similar to Unity's system
+
+ #ifdef UNITY_SPECCUBE_BOX_PROJECTION
+ cc_reflect_dir = BoxProjectedCubemapDirection(cc_reflect_dir, i.worldPos, /*probe_position=*/0, /*box_min=*/-1, /*box_max=*/1);
+ #endif
+
+ half mip = cc_roughness_perceptual * UNITY_SPECCUBE_LOD_STEPS;
+ float4 envSample = UNITY_SAMPLE_TEXCUBE_LOD(_Fallback_Cubemap, cc_reflect_dir, mip);
+ cc_env_refl = DecodeHDR(envSample, _Fallback_Cubemap_HDR) * _Fallback_Cubemap_Brightness * light.diffuse_luminance;
+ }
+#endif
+
#if defined(_BRIGHTNESS_CONTROL)
cc_env_refl *= _Brightness_Multiplier;
#endif
@@ -233,7 +247,8 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
float3 indirect_specular_cc = Fcc * cc_env_refl;
// Energy conservation
- remainder *= (1.0f - Fcc * _Clearcoat_Strength);
+ indirect_standard += indirect_specular_cc;
+ remainder = saturate(remainder - Fcc);
#endif
const float dielectric_f0 = computeDielectricF0(_reflectance);
@@ -251,10 +266,7 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
float3 Fr = E * light.specular * remainder;
- indirect_standard = Fr + Fd;
-#if defined(_CLEARCOAT) && (defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS))
- indirect_standard += indirect_specular_cc;
-#endif
+ indirect_standard += Fr + Fd;
}
#if defined(_MATERIAL_TYPE_CLOTH)
diff --git a/yum_lighting.cginc b/yum_lighting.cginc
index 80b1f66..363e364 100644
--- a/yum_lighting.cginc
+++ b/yum_lighting.cginc
@@ -117,41 +117,40 @@ struct YumLighting {
float getShadowAttenuation(v2f i)
{
float attenuation;
+ float shadow;
// This whole block is yoinked from AutoLight.cginc. I needed a way to
// control shadow strength so I had to duplicate the code.
#if defined(DIRECTIONAL_COOKIE)
DECLARE_LIGHT_COORD(i, i.worldPos);
- float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
+ shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
attenuation = tex2D(_LightTexture0, lightCoord).w;
#elif defined(POINT_COOKIE)
DECLARE_LIGHT_COORD(i, i.worldPos);
- float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
+ shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
attenuation = tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).r *
texCUBE(_LightTexture0, lightCoord).w;
#elif defined(DIRECTIONAL)
- float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
+ shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
attenuation = 1;
#elif defined(SPOT)
DECLARE_LIGHT_COORD(i, i.worldPos);
- float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
+ shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
attenuation = (lightCoord.z > 0) * UnitySpotCookie(lightCoord) *
UnitySpotAttenuate(lightCoord.xyz);
#elif defined(POINT)
unityShadowCoord3 lightCoord =
mul(unity_WorldToLight, unityShadowCoord4(i.worldPos, 1)).xyz;
- float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
+ shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
attenuation = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).r;
#else
- float shadow = 1;
+ shadow = 1;
attenuation = 1;
#endif
+ float realtimeAttenuation = attenuation * lerp(1, shadow, _Shadow_Strength);
- attenuation *= shadow;
+ GetBakedAttenuation(realtimeAttenuation, i.uv01.zw, i.worldPos);
- GetBakedAttenuation(attenuation, i.uv01.zw, i.worldPos);
-
- attenuation = lerp(1, attenuation, _Shadow_Strength);
- return attenuation;
+ return realtimeAttenuation;
}
float3 getDirectLightDirection(v2f i) {
@@ -162,13 +161,6 @@ float3 getDirectLightDirection(v2f i) {
#endif
}
-float4 getDirectLightColorIntensity() {
- // Properly separate light color from intensity like filamented
- if (_LightColor0.w <= 0) return float4(0, 0, 0, 0);
- _LightColor0 += 0.000001; // Avoid division by zero
- return float4(_LightColor0.xyz / _LightColor0.w, _LightColor0.w);
-}
-
float GetLodRoughness(float roughness) {
return roughness * (1.7 - 0.7 * roughness);
}
@@ -323,9 +315,8 @@ YumLighting GetYumLighting(v2f i, YumPbr pbr) {
light.dir = getDirectLightDirection(i);
// Use proper light color/intensity separation
- float4 lightColorIntensity = getDirectLightColorIntensity();
- light.direct = lightColorIntensity.rgb * lightColorIntensity.w;
-
+ light.direct = _LightColor0.rgb;
+
// Calculate attenuation first, before diffuse lighting
light.attenuation = getShadowAttenuation(i);
@@ -401,4 +392,3 @@ YumLighting GetYumLighting(v2f i, YumPbr pbr) {
}
#endif // __YUM_LIGHTING_INC
-