From eb56b40f4a67530eab5422f374bfbcad71b1c6a0 Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 7 Mar 2026 16:56:29 -0800 Subject: Add normal map to decals --- 3ner.cginc | 2 -- 3ner.shader | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ decal.cginc | 14 ++++++++----- globals.cginc | 5 ++++- pbr.cginc | 35 ++++---------------------------- 5 files changed, 81 insertions(+), 39 deletions(-) diff --git a/3ner.cginc b/3ner.cginc index 8591b00..def3ed0 100755 --- a/3ner.cginc +++ b/3ner.cginc @@ -10,7 +10,6 @@ #include "brdf.cginc" #include "cnlohr.cginc" -#include "decal.cginc" #include "geometry.cginc" #include "pbr.cginc" #include "lighting.cginc" @@ -302,7 +301,6 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.normal *= facing ? 1 : -1; Pbr pbr = getPbr(i); - applyDecals(i, pbr); if (_Mode == 1) { clip(pbr.albedo.a - 0.5); diff --git a/3ner.shader b/3ner.shader index 31504e0..52bf73e 100755 --- a/3ner.shader +++ b/3ner.shader @@ -93,6 +93,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal0_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal0_Normal_Enabled==0 + [HideInInspector] m_start_Decal0_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL0_NORMAL)] _Decal0_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal0_Normal("Normal Map", 2D) = "bump" {} + _Decal0_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal0_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal0_Rotation_Enabled==0 [HideInInspector] m_start_Decal0_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL0_ROTATION)] _Decal0_Rotation_Enabled("Enable", Float) = 0 @@ -137,6 +145,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal1_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal1_Normal_Enabled==0 + [HideInInspector] m_start_Decal1_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL1_NORMAL)] _Decal1_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal1_Normal("Normal Map", 2D) = "bump" {} + _Decal1_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal1_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal1_Rotation_Enabled==0 [HideInInspector] m_start_Decal1_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL1_ROTATION)] _Decal1_Rotation_Enabled("Enable", Float) = 0 @@ -181,6 +197,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal2_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal2_Normal_Enabled==0 + [HideInInspector] m_start_Decal2_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL2_NORMAL)] _Decal2_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal2_Normal("Normal Map", 2D) = "bump" {} + _Decal2_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal2_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal2_Rotation_Enabled==0 [HideInInspector] m_start_Decal2_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL2_ROTATION)] _Decal2_Rotation_Enabled("Enable", Float) = 0 @@ -225,6 +249,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal3_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal3_Normal_Enabled==0 + [HideInInspector] m_start_Decal3_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL3_NORMAL)] _Decal3_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal3_Normal("Normal Map", 2D) = "bump" {} + _Decal3_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal3_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal3_Rotation_Enabled==0 [HideInInspector] m_start_Decal3_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL3_ROTATION)] _Decal3_Rotation_Enabled("Enable", Float) = 0 @@ -269,6 +301,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal4_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal4_Normal_Enabled==0 + [HideInInspector] m_start_Decal4_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL4_NORMAL)] _Decal4_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal4_Normal("Normal Map", 2D) = "bump" {} + _Decal4_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal4_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal4_Rotation_Enabled==0 [HideInInspector] m_start_Decal4_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL4_ROTATION)] _Decal4_Rotation_Enabled("Enable", Float) = 0 @@ -313,6 +353,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal5_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal5_Normal_Enabled==0 + [HideInInspector] m_start_Decal5_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL5_NORMAL)] _Decal5_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal5_Normal("Normal Map", 2D) = "bump" {} + _Decal5_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal5_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal5_Rotation_Enabled==0 [HideInInspector] m_start_Decal5_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL5_ROTATION)] _Decal5_Rotation_Enabled("Enable", Float) = 0 @@ -357,6 +405,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal6_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal6_Normal_Enabled==0 + [HideInInspector] m_start_Decal6_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL6_NORMAL)] _Decal6_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal6_Normal("Normal Map", 2D) = "bump" {} + _Decal6_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal6_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal6_Rotation_Enabled==0 [HideInInspector] m_start_Decal6_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL6_ROTATION)] _Decal6_Rotation_Enabled("Enable", Float) = 0 @@ -401,6 +457,14 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Decal7_Metallic_Gloss("Metallic Gloss", Float) = 0 //endex + //ifex _Decal7_Normal_Enabled==0 + [HideInInspector] m_start_Decal7_Normal("Normal Map", Float) = 0 + [ThryToggle(_DECAL7_NORMAL)] _Decal7_Normal_Enabled("Enable", Float) = 0 + [Normal] _Decal7_Normal("Normal Map", 2D) = "bump" {} + _Decal7_Normal_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Decal7_Normal("Normal Map", Float) = 0 + //endex + //ifex _Decal7_Rotation_Enabled==0 [HideInInspector] m_start_Decal7_Rotation("Rotation", Float) = 0 [ThryToggle(_DECAL7_ROTATION)] _Decal7_Rotation_Enabled("Enable", Float) = 0 diff --git a/decal.cginc b/decal.cginc index 10b2f7a..bfd4bf5 100644 --- a/decal.cginc +++ b/decal.cginc @@ -1,8 +1,7 @@ #ifndef __DECAL_INC #define __DECAL_INC -#include "globals.cginc" -#include "pbr.cginc" +#include "data.cginc" #include "interpolators.cginc" #include "texture_utils.cginc" @@ -30,10 +29,10 @@ float2 decal_rotate(float2 uv, float rotation) { #define APPLY_DECAL(N) \ { \ float2 uv = get_uv_by_channel(i, _Decal##N##_UV_Channel); \ - if (_Decal##N##_Rotation_Enabled) \ - uv = decal_rotate(uv, _Decal##N##_Rotation); \ uv -= _Decal##N##_MainTex_ST.zw; \ uv *= _Decal##N##_MainTex_ST.xy; \ + if (_Decal##N##_Rotation_Enabled) \ + uv = decal_rotate(uv, _Decal##N##_Rotation); \ float4 albedo = decal_sample(_Decal##N##_MainTex, uv, _Decal##N##_UV_Mode); \ if (_Decal##N##_SDF_Enabled) { \ float sd = _Decal##N##_SDF_Invert ? 1 - albedo.r : albedo.r; \ @@ -72,9 +71,14 @@ float2 decal_rotate(float2 uv, float rotation) { pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1); \ pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1); \ } \ + if (_Decal##N##_Normal_Enabled) { \ + float3 dn = UnpackNormal(decal_sample(_Decal##N##_Normal, uv, _Decal##N##_UV_Mode)); \ + dn.xy *= _Decal##N##_Normal_Scale; \ + normal_tangent = blendNormalsHill12(normal_tangent, float3(dn.xy * albedo.a, dn.z)); \ + } \ } -void applyDecals(v2f i, inout Pbr pbr) { +void applyDecals(v2f i, inout Pbr pbr, inout float3 normal_tangent) { #if defined(_DECAL0) APPLY_DECAL(0) #endif diff --git a/globals.cginc b/globals.cginc index 1886a8c..f07e27f 100755 --- a/globals.cginc +++ b/globals.cginc @@ -270,7 +270,10 @@ float _Parallax_Heightmap_Ray_Marching_Steps; float _Decal##N##_SDF_Invert; \ float _Decal##N##_Metallic_Gloss_Enabled; \ texture2D _Decal##N##_Metallic_Gloss; \ - float4 _Decal##N##_Metallic_Gloss_ST; + float4 _Decal##N##_Metallic_Gloss_ST; \ + float _Decal##N##_Normal_Enabled; \ + texture2D _Decal##N##_Normal; \ + float _Decal##N##_Normal_Scale; #if defined(_DECAL0) DECAL_GLOBALS(0) diff --git a/pbr.cginc b/pbr.cginc index 9fbf2de..0846f8f 100755 --- a/pbr.cginc +++ b/pbr.cginc @@ -2,41 +2,12 @@ #define __PBR_INC #include "filamented.cginc" -#include "globals.cginc" +#include "data.cginc" +#include "decal.cginc" #include "instancing.cginc" #include "interpolators.cginc" #include "texture_utils.cginc" -// From filament: min roughness s.t. MIN_PERCEPTUAL_ROUGHNESS^4 > 0 in target -// precision. Although we use fp32, the higher min roughness gives us a broader -// specular highlight, which is preferable. -#define MIN_PERCEPTUAL_ROUGHNESS 0.045 -#define MIN_ROUGHNESS 0.002025 - -struct Pbr { - float4 albedo; - float3 normal; - float3x3 tbn; - float smoothness; - float roughness_perceptual; - float roughness; - float metallic; -#if defined(_EMISSIONS) && defined(FORWARD_BASE_PASS) - float3 emission; -#endif -#if defined(_AMBIENT_OCCLUSION) - float ao; -#endif -#if defined(_BENT_NORMALS) - float3 bent_normal; -#endif -#if defined(_CLEARCOAT) - float cc_roughness; - float cc_roughness_perceptual; - float cc_strength; -#endif -}; - #if defined(_PARALLAX_HEIGHTMAP) float2 parallax_offset(float2 uv, float3 view_dir_world, float3x3 tbn) { float3 view_dir_tangent = mul(tbn, view_dir_world); @@ -205,6 +176,8 @@ Pbr getPbr(v2f i) { normal_tangent = blendNormalsHill12(normal_tangent, detail_normal); #endif + applyDecals(i, pbr, normal_tangent); + pbr.normal = normalize(mul(normal_tangent, pbr.tbn)); #if defined(_BENT_NORMALS) -- cgit v1.2.3