From ef6b610577f851d8c70840f945c0ec2161450302 Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 27 Feb 2025 18:02:04 -0800 Subject: Add detail maps --- 2ner.cginc | 2 +- 2ner.shader | 12 +++++++++++- features.cginc | 4 ++++ globals.cginc | 9 +++++++++ math.cginc | 10 ++++++++++ yum_pbr.cginc | 23 ++++++++++++++++++----- 6 files changed, 53 insertions(+), 7 deletions(-) diff --git a/2ner.cginc b/2ner.cginc index f981a4c..29c107a 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -172,7 +172,7 @@ float4 frag(v2f i) : SV_Target { #if defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS) || defined(OUTLINE_PASS) || defined(EXTRA_STENCIL_COLOR_PASS) YumLighting l = GetYumLighting(i, pbr); -#if defined(FORWARD_BASE_PASS) +#if defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS) applyMatcapsAndRimLighting(i, pbr, l); pbr.albedo.rgb = max(0, pbr.albedo.rgb); l.diffuse = max(0, l.diffuse); diff --git a/2ner.shader b/2ner.shader index c1433e7..91008b6 100644 --- a/2ner.shader +++ b/2ner.shader @@ -60,6 +60,16 @@ Shader "yum_food/2ner" [HideInInspector] m_end_AO("Metallics", Float) = 0 //endex + //ifex _Detail_Maps_Enabled==0 + [HideInInspector] m_start_Detail_Maps("Detail maps", Float) = 0 + [ThryToggle(_DETAIL_MAPS)]_Detail_Maps_Enabled("Enable", Float) = 0 + _DetailMask("Mask", 2D) = "white" {} + _DetailAlbedoMap("Base color", 2D) = "white" {} + [Normal]_DetailNormalMap("Normals", 2D) = "bump" {} + _DetailNormalMapScale("Normal intensity", Range(0, 10)) = 1 + [HideInInspector] m_end_Detail_Maps("Detail maps", Float) = 0 + //endex + //ifex _Metallics_Enabled==0 [HideInInspector] m_reflectionOptions("Reflections", Float) = 0 [HideInInspector] m_start_Metallic("Metallics", Float) = 0 @@ -159,7 +169,7 @@ Shader "yum_food/2ner" _Rim_Lighting1_Target_Mask("Target mask (albedo|diffuse<<1|specular<<2)", Int) = 1 //ifex _Rim_Lighting1_Mask_Enabled==0 [HideInInspector] m_start_Rim_Lighting1_Mask("Mask", Float) = 0 - [ThryToggle(_RIM_LIGHTINg1_MASK)]_Rim_Lighting1_Mask_Enabled("Enable", Float) = 0 + [ThryToggle(_RIM_LIGHTING1_MASK)]_Rim_Lighting1_Mask_Enabled("Enable", Float) = 0 _Rim_Lighting1_Mask("Mask", 2D) = "white" {} [HideInInspector] m_end_Rim_Lighting1_Mask("Mask", Float) = 0 //endex diff --git a/features.cginc b/features.cginc index 3635f7a..7f0fe79 100644 --- a/features.cginc +++ b/features.cginc @@ -9,6 +9,10 @@ #pragma shader_feature_local _AMBIENT_OCCLUSION //endex +//ifex _Detail_Maps_Enabled==0 +#pragma shader_feature_local _DETAIL_MAPS +//endex + //ifex _Emission_Enabled==0 #pragma shader_feature_local _EMISSION //endex diff --git a/globals.cginc b/globals.cginc index 83ba8fb..20f866c 100644 --- a/globals.cginc +++ b/globals.cginc @@ -30,6 +30,15 @@ sampler2D _OcclusionMap; float _OcclusionStrength; #endif +#if defined(_DETAIL_MAPS) +texture2D _DetailMask; +sampler2D _DetailAlbedoMap; +float4 _DetailAlbedoMap_ST; +sampler2D _DetailNormalMap; +float4 _DetailNormalMap_ST; +float _DetailNormalMapScale; +#endif + float _Shadow_Strength; #if defined(_WRAPPED_LIGHTING) diff --git a/math.cginc b/math.cginc index 85fe4ea..436cc56 100644 --- a/math.cginc +++ b/math.cginc @@ -184,6 +184,16 @@ float4 alphaBlend(float4 behind, float4 front) { return float4(front.rgb * front.a + behind.rgb * (1 - front.a), front.a + behind.a * (1 - front.a)); } +// Reoriented normal mapping +// https://blog.selfshadow.com/publications/blending-in-detail/ +// Inputs are in tangent space. +float3 blendNormalsHill12(float3 n0, float3 n1) { + n0.z += 1.0; + n1.xy = -n1.xy; + + return normalize(n0 * dot(n0, n1) - n1 * n0.z); +} + #endif // __MATH_INC diff --git a/yum_pbr.cginc b/yum_pbr.cginc index ce1c411..5faff90 100644 --- a/yum_pbr.cginc +++ b/yum_pbr.cginc @@ -11,7 +11,7 @@ struct YumPbr { float4 albedo; float3 normal; -#if defined(_EMISSION) || (defined(_GLITTER) && defined(FORWARD_BASE_PASS)) +#if defined(_EMISSION) || (defined(_GLITTER) && (defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS))) float3 emission; #endif float smoothness; @@ -39,13 +39,26 @@ YumPbr GetYumPbr(v2f i) { UV_SCOFF(i, _MainTex_ST, /*which_channel=*/0)) * _Color; #endif - float3 normal_raw = UnpackScaleNormal( + float3 normal_tangent = UnpackScaleNormal( tex2D(_BumpMap, UV_SCOFF(i, _BumpMap_ST, /*which_channel=*/0)), _BumpScale); + +#if defined(_DETAIL_MAPS) + float detail_mask = _DetailMask.SampleLevel(linear_repeat_s, i.uv01.xy, 0); + float4 detail_albedo = tex2D(_DetailAlbedoMap, + UV_SCOFF(i, _DetailAlbedoMap_ST, /*which_channel=*/0)); + float3 detail_normal = UnpackScaleNormal( + tex2D(_DetailNormalMap, + UV_SCOFF(i, _DetailNormalMap_ST, /*which_channel=*/0)), + _DetailNormalMapScale); + result.albedo = lerp(result.albedo, result.albedo * detail_albedo, detail_mask); + normal_tangent = lerp(normal_tangent, blendNormalsHill12(normal_tangent, detail_normal), detail_mask); +#endif + float3x3 tangentToWorld = float3x3(i.tangent, i.binormal, i.normal); - result.normal = normalize(mul(normal_raw, tangentToWorld)); + result.normal = normalize(mul(normal_tangent, tangentToWorld)); -#if defined(FORWARD_BASE_PASS) && defined(_GLITTER) +#if (defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS)) && defined(_GLITTER) GlitterParams glitter_p; glitter_p.color = _Glitter_Color; glitter_p.layers = _Glitter_Layers; @@ -75,7 +88,7 @@ YumPbr GetYumPbr(v2f i) { result.emission = tex2D(_EmissionMap, UV_SCOFF(i, _EmissionMap_ST, /*which_channel=*/0)) * _EmissionColor; #endif -#if defined(FORWARD_BASE_PASS) && defined(_GLITTER) +#if (defined(FORWARD_BASE_PASS) || defined(FORWARD_ADD_PASS)) && defined(_GLITTER) float3 gitter_emission = glitter_albedo.rgb * glitter_albedo.a * _Glitter_Emission; #if defined(_EMISSION) result.emission += gitter_emission; -- cgit v1.2.3