From 281dfbc4f05f2b6f183e7eaa648c85cae5d495ed Mon Sep 17 00:00:00 2001 From: Misaki Date: Sun, 2 Feb 2025 17:04:05 +0900 Subject: [PATCH] Improve AmbientScope and OutlineScope --- Editor/MeterialEditor/ShaderGUIExpandable.cs | 2 +- .../MeterialEditor/UIScopes/AmbientScope.cs | 134 +++++++++--------- .../MeterialEditor/UIScopes/OutlineScope.cs | 16 ++- Runtime/Models/ShaderKeywordEnum.cs | 15 ++ Runtime/Shaders/HDRPToon.shader | 43 +++--- .../Includes/Lighting/UtsLightEvaluation.hlsl | 56 +++++--- .../Includes/Lighting/UtsLightLoop.hlsl | 16 +-- .../Properties/UtsHdrpProperties.hlsl | 9 -- .../Includes/Properties/UtsTextures.hlsl | 8 +- .../Properties/UtsUnityPerMaterial.hlsl | 31 +++- .../Includes/ShaderPass/HDRPToonOutline.hlsl | 13 +- 11 files changed, 200 insertions(+), 143 deletions(-) diff --git a/Editor/MeterialEditor/ShaderGUIExpandable.cs b/Editor/MeterialEditor/ShaderGUIExpandable.cs index 7ac9d4e..c94fdcc 100644 --- a/Editor/MeterialEditor/ShaderGUIExpandable.cs +++ b/Editor/MeterialEditor/ShaderGUIExpandable.cs @@ -7,7 +7,7 @@ namespace Misaki.HdrpToon.Editor Shadow = 1 << 2, MaterialFeature = 1 << 3, SurfaceInputs = 1 << 4, - AmbientMode = 1 << 5, + Ambient = 1 << 5, Highlight = 1 << 6, Rimlight = 1 << 7, MatCap = 1 << 8, diff --git a/Editor/MeterialEditor/UIScopes/AmbientScope.cs b/Editor/MeterialEditor/UIScopes/AmbientScope.cs index de4e190..55dfa2f 100644 --- a/Editor/MeterialEditor/UIScopes/AmbientScope.cs +++ b/Editor/MeterialEditor/UIScopes/AmbientScope.cs @@ -6,106 +6,110 @@ namespace Misaki.HdrpToon.Editor { public class AmbientScope : MaterialUIScope { - protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.AmbientMode; + protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.Ambient; protected override GUIContent Header => new("Ambient Settings"); private static class Properties { - public static MaterialProperty IndirectDiffuseMode; - public static MaterialProperty IndirectDiffuseIntensity; - public static MaterialProperty IndirectSpecularMode; - public static MaterialProperty IndirectReflectionIntensity; - public static MaterialProperty EnableSSAO; - public static MaterialProperty SSAOFactor; - public static MaterialProperty EnableSSGI; - public static MaterialProperty SSGIFactor; - public static MaterialProperty EnableSSR; + public static MaterialProperty indirectDiffuseMode; + public static MaterialProperty indirectSpecularMode; + + public static MaterialProperty indirectDiffuseMatCapMap; + public static MaterialProperty indirectDiffuseMatCapLod; + + public static MaterialProperty indirectDiffuseIntensity; + public static MaterialProperty ssaoWeight; + public static MaterialProperty ssgiWeight; + + public static MaterialProperty indirectSpecularMatCapMap; + public static MaterialProperty indirectSpecularMatCapLod; + + public static MaterialProperty indirectReflectionIntensity; + public static MaterialProperty ssrWeight; } private static class Styles { - public static readonly GUIContent IndirectDiffuseModeText = - new("Indirect Diffuse Mode", "Specifies indirect diffuse mode."); + public static readonly GUIContent indirectDiffuseModeText = new("Indirect Diffuse Mode", "Specifies indirect diffuse mode."); + public static readonly GUIContent indirectSpecularModeText = new("Indirect Specular Mode", "Specifies indirect specular mode."); - public static readonly GUIContent IndirectDiffuseIntensityText = - new("Indirect Diffuse Intensity", - "The Environment Light color is added to the material color according to the GI Intensity value."); + public static readonly GUIContent indirectDiffuseMatCapMapText = new("MatCap Map", "The material capture map for indirect diffuse evaluation, with the additional setting for controlling the LOD offset when sampling the indirect diffuse material capture map."); - public static readonly GUIContent IndirectSpecularModeText = - new("Indirect Specular Mode", "Specifies indirect specular mode."); + public static readonly GUIContent indirectDiffuseIntensityText = new("Intensity", "The indirect diffuse color is added to the material color according to the intensity value."); + public static readonly GUIContent ssaoWeightText = new("SSAO Weight", "The weight of SSAO."); + public static readonly GUIContent ssgiWeightText = new("SSGI Weight", "The weight of SSGI."); - public static readonly GUIContent IndirectReflectionIntensityText = - new("Indirect Reflection Intensity", - "The Environment Light color is added to the material color according to the GI Intensity value."); + public static readonly GUIContent indirectSpecularMatCapMapText = new("MatCap Map", "The material capture map for indirect specular evaluation, with the additional setting for controlling the LOD offset when sampling the indirect specular material capture map."); - public static readonly GUIContent EnableSSAOText = new("Enable SSAO", "Enable SSAO."); - public static readonly GUIContent SSAOFactorText = new("SSAO Factor", "The weight of SSAO."); - public static readonly GUIContent EnableSSGIText = new("Enable SSGI", "Enable SSGI."); - public static readonly GUIContent SSGIFactorText = new("SSGI Factor", "The weight of SSGI."); - public static readonly GUIContent EnableSSRText = new("Enable SSR", "Enable SSR."); + public static readonly GUIContent indirectReflectionIntensityText = new("Intensity", "The indirect reflection color is added to the material color according to the intensity value."); + public static readonly GUIContent ssrWeightText = new("SSR Weight", "SSR Weight"); } - private readonly string[] _indirectDiffuseModeDefines = - { "_INDIRECT_DIFFUSE_OFF", "_INDIRECT_DIFFUSE_IBL", "_INDIRECT_DIFFUSE_MATCAP", "_INDIRECT_DIFFUSE_RAMP" }; - - private readonly string[] _indirectSpecularModeDefines = - { "_INDIRECT_SPECULAR_OFF", "_INDIRECT_SPECULAR_IBL", "_INDIRECT_SPECULAR_MATCAP" }; - public override void LoadMaterialProperties() { - Properties.IndirectDiffuseMode = FindProperty("_Indirect_Diffuse_Mode"); - Properties.IndirectDiffuseIntensity = FindProperty("_ID_Intensity"); - Properties.IndirectSpecularMode = FindProperty("_Indirect_Specular_Mode"); - Properties.IndirectReflectionIntensity = FindProperty("_IR_Intensity"); - Properties.EnableSSAO = FindProperty("_ReceivesSSAO"); - Properties.SSAOFactor = FindProperty("_AO_Factor"); - Properties.EnableSSGI = FindProperty("_ReceivesSSGI"); - Properties.SSGIFactor = FindProperty("_GI_Factor"); - Properties.EnableSSR = FindProperty("_ReceivesSSR"); + Properties.indirectDiffuseMode = FindProperty("_Indirect_Diffuse_Mode"); + Properties.indirectSpecularMode = FindProperty("_Indirect_Specular_Mode"); + + Properties.indirectDiffuseMatCapMap = FindProperty("_IndirectDiffuseMatCapMap"); + Properties.indirectDiffuseMatCapLod = FindProperty("_IndirectDiffuseMatCapLod"); + + Properties.indirectDiffuseIntensity = FindProperty("_IndirectDiffuseIntensity"); + Properties.ssaoWeight = FindProperty("_SSAOWeight"); + Properties.ssgiWeight = FindProperty("_SSGIWeight"); + + Properties.indirectSpecularMatCapMap = FindProperty("_IndirectSpecularMatCapMap"); + Properties.indirectSpecularMatCapLod = FindProperty("_IndirectSpecularMatCapLod"); + + Properties.indirectReflectionIntensity = FindProperty("_IndirectSpecularIntensity"); + Properties.ssrWeight = FindProperty("_SSRWeight"); } protected override void DrawContent() { - editor.ShaderProperty(Properties.IndirectDiffuseMode, Styles.IndirectDiffuseModeText); - for (int i = 0; i < _indirectDiffuseModeDefines.Length; i++) - { - editor.SetKeyword(_indirectDiffuseModeDefines[i], Properties.IndirectDiffuseMode.intValue == i); - } + editor.ShaderProperty(Properties.indirectDiffuseMode, Styles.indirectDiffuseModeText); + editor.ShaderProperty(Properties.indirectSpecularMode, Styles.indirectSpecularModeText); - if (Properties.IndirectDiffuseMode.intValue != 0) + var indirectDiffuseMode = Properties.indirectDiffuseMode.GetEnumValue(); + var indirectSpecularMode = Properties.indirectSpecularMode.GetEnumValue(); + + if (indirectDiffuseMode != IndirectDiffuseMode.Off) { + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Indirect Diffuse", EditorStyles.boldLabel); + EditorGUI.indentLevel++; - editor.ShaderProperty(Properties.IndirectDiffuseIntensity, Styles.IndirectDiffuseIntensityText); - editor.ShaderProperty(Properties.EnableSSAO, Styles.EnableSSAOText); - if (Properties.EnableSSAO.floatValue > 0) + + if (indirectDiffuseMode == IndirectDiffuseMode.Matcap) { - EditorGUI.indentLevel++; - editor.ShaderProperty(Properties.SSAOFactor, Styles.SSAOFactorText); - EditorGUI.indentLevel--; + editor.TexturePropertySingleLine(Styles.indirectDiffuseMatCapMapText, Properties.indirectDiffuseMatCapMap, Properties.indirectDiffuseMatCapLod); } - editor.ShaderProperty(Properties.EnableSSGI, Styles.EnableSSGIText); - if (Properties.EnableSSGI.floatValue > 0) + editor.ShaderProperty(Properties.indirectDiffuseIntensity, Styles.indirectDiffuseIntensityText); + editor.ShaderProperty(Properties.ssaoWeight, Styles.ssaoWeightText); + + if (indirectDiffuseMode == IndirectDiffuseMode.IBL) { - EditorGUI.indentLevel++; - editor.ShaderProperty(Properties.SSGIFactor, Styles.SSGIFactorText); - EditorGUI.indentLevel--; + editor.ShaderProperty(Properties.ssgiWeight, Styles.ssgiWeightText); } EditorGUI.indentLevel--; } - editor.ShaderProperty(Properties.IndirectSpecularMode, Styles.IndirectSpecularModeText); - for (int i = 0; i < _indirectSpecularModeDefines.Length; i++) + if (indirectSpecularMode != (int)IndirectSpecularMode.Off) { - editor.SetKeyword(_indirectSpecularModeDefines[i], Properties.IndirectSpecularMode.intValue == i); - } + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Indirect Specular", EditorStyles.boldLabel); - if (Properties.IndirectSpecularMode.intValue != 0) - { EditorGUI.indentLevel++; - editor.ShaderProperty(Properties.IndirectReflectionIntensity, Styles.IndirectReflectionIntensityText); - editor.ShaderProperty(Properties.EnableSSR, Styles.EnableSSRText); + + if (indirectSpecularMode == IndirectSpecularMode.Matcap) + { + editor.TexturePropertySingleLine(Styles.indirectSpecularMatCapMapText, Properties.indirectSpecularMatCapMap, Properties.indirectSpecularMatCapLod); + } + + editor.ShaderProperty(Properties.indirectReflectionIntensity, Styles.indirectReflectionIntensityText); + editor.ShaderProperty(Properties.ssrWeight, Styles.ssrWeightText); + EditorGUI.indentLevel--; } } diff --git a/Editor/MeterialEditor/UIScopes/OutlineScope.cs b/Editor/MeterialEditor/UIScopes/OutlineScope.cs index c546722..cd1d0d0 100644 --- a/Editor/MeterialEditor/UIScopes/OutlineScope.cs +++ b/Editor/MeterialEditor/UIScopes/OutlineScope.cs @@ -14,6 +14,8 @@ namespace Misaki.HdrpToon.Editor public static MaterialProperty outlineColor; public static MaterialProperty outlineColorMap; public static MaterialProperty albedoAffectOutline; + public static MaterialProperty skyColorAffectOutline; + public static MaterialProperty skyColorIntensity; public static MaterialProperty fadeIn; public static MaterialProperty fadeOut; @@ -26,6 +28,9 @@ namespace Misaki.HdrpToon.Editor public static readonly GUIContent outlineWidthText = new("Outline Width", "Specifies the width of the outline."); public static readonly GUIContent outlineColorText = new("Outline Color", "Specifies the color of the outline."); public static readonly GUIContent albedoAffectOutlineText = new("Albedo Affect Outline", "Enable to affect the outline color with the albedo color."); + public static readonly GUIContent skyColorAffectOutlineText = new("Sky Color Affect Outline", "Enable to affect the outline color with the sky color."); + public static readonly GUIContent skyColorIntensityText = new("Sky Color Intensity", "The intensity of the sky color when add to the outline color."); + public static readonly GUIContent fadeInText = new("Fade In Distance", "Specify the nearest distance, where the outline width changes with the distance between the camera and the object. The outline will be the maximum width at this distance."); public static readonly GUIContent fadeOutText = new("Fade Out Distance", "Specify the furthest distance, where the outline width changes with the distance between the camera and the object. The outline will be zero at this distance."); public static readonly GUIContent useSmoothedNormalText = new("Use Smoothed Normal", "Enable to use smoothed normal(that packed in uv2) for outline calculation."); @@ -45,6 +50,8 @@ namespace Misaki.HdrpToon.Editor Properties.outlineColor = FindProperty("_OutlineColor"); Properties.outlineColorMap = FindProperty("_OutlineColorMap"); Properties.albedoAffectOutline = FindProperty("_AlbedoAffectOutline"); + Properties.skyColorAffectOutline = FindProperty("_SkyColorAffectOutline"); + Properties.skyColorIntensity = FindProperty("_SkyColorIntensity"); Properties.fadeIn = FindProperty("_OutlineFadeIn"); Properties.fadeOut = FindProperty("_OutlineFadeOut"); @@ -57,9 +64,16 @@ namespace Misaki.HdrpToon.Editor editor.TexturePropertySingleLine(Styles.outlineWidthText, Properties.outlineWidthMap, Properties.outlineWidth); editor.TexturePropertySingleLine(Styles.outlineColorText, Properties.outlineColorMap, Properties.outlineColor); editor.ShaderProperty(Properties.albedoAffectOutline, Styles.albedoAffectOutlineText); + editor.ShaderProperty(Properties.skyColorAffectOutline, Styles.skyColorAffectOutlineText); + if (Properties.skyColorAffectOutline.GetBooleanValue()) + { + editor.ShaderProperty(Properties.skyColorIntensity, Styles.skyColorIntensityText); + } + + EditorGUILayout.Space(); editor.ShaderProperty(Properties.fadeIn, Styles.fadeInText); editor.ShaderProperty(Properties.fadeOut, Styles.fadeOutText); editor.ShaderProperty(Properties.useSmoothedNormal, Styles.useSmoothedNormalText); } } -} +} \ No newline at end of file diff --git a/Runtime/Models/ShaderKeywordEnum.cs b/Runtime/Models/ShaderKeywordEnum.cs index 0c66a13..399f73e 100644 --- a/Runtime/Models/ShaderKeywordEnum.cs +++ b/Runtime/Models/ShaderKeywordEnum.cs @@ -14,4 +14,19 @@ namespace Misaki.HdrpToon KKHair, Toon } + + internal enum IndirectDiffuseMode + { + Off, + IBL, + Matcap, + Ramp + } + + internal enum IndirectSpecularMode + { + Off, + IBL, + Matcap + } } \ No newline at end of file diff --git a/Runtime/Shaders/HDRPToon.shader b/Runtime/Shaders/HDRPToon.shader index ec611f1..95a92a8 100644 --- a/Runtime/Shaders/HDRPToon.shader +++ b/Runtime/Shaders/HDRPToon.shader @@ -223,11 +223,6 @@ Shader "HDRP/Toon" _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5 [ToggleUI] _SupportDecals("Support Decals", Float) = 1.0 - [ToggleUI] _ReceivesSSR("Receives SSR", Float) = 0.0 - [ToggleUI] _ReceivesSSAO("Receives SSAO", Float) = 1.0 - _AO_Factor("_AO_Factor", Range(0.0, 1.0)) = 1 - [ToggleUI] _ReceivesSSGI("Receives SSGI", Float) = 1.0 - _GI_Factor("_GI_Factor", Range(0.0, 1.0)) = 1 [ToggleUI] _AddPrecomputedVelocity("AddPrecomputedVelocity", Float) = 0.0 // ----------------------------------------------------------------------- @@ -287,7 +282,6 @@ Shader "HDRP/Toon" // Surface Inputs _NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map _NormalScale("_NormalScale", Range(0.0, 8.0)) = 1 - //_NormalMapOS("NormalMapOS", 2D) = "white" {} // Object space normal map - no good default value _Metallic("_Metallic", Range(0.0, 1.0)) = 0 _Smoothness("Smoothness", Range(0.0, 1.0)) = 0.0 @@ -300,12 +294,28 @@ Shader "HDRP/Toon" _AlphaRemapMax("AlphaRemapMax", Float) = 1.0 _AORemapMin("AORemapMin", Float) = 0.0 _AORemapMax("AORemapMax", Float) = 1.0 + [ToggleUI] _Use_SSSLut("Use SSSLut", Int) = 0 _SSSLutMap("SSSLutMap", 2D) = "white" {} - [ToggleUI, HideInInspector] _Use_SSSLut("Use SSSLut", Int) = 0 _SSSIntensity("SSSIntensity", Range(0.0, 3.0)) = 1.0 _HairBlendingMap("HairBlendingMap", 2D) = "black" {} + // Ambient + [KeywordEnum(Off, IBL, MatCap, Ramp)]_Indirect_Diffuse_Mode("_Indirect_Diffuse_Mode", Integer) = 1 + [KeywordEnum(Off, IBL, MatCap)]_Indirect_Specular_Mode("_Indirect_Specular_Mode", Integer) = 1 + + _IndirectDiffuseMatCapMap("IndirectDiffuseMatCapMap", 2D) = "black" {} + _IndirectDiffuseMatCapLod("IndirectDiffuseMatCapMapLOD", Range(-5, 5)) = 0.0 + + _IndirectDiffuseIntensity("Indirect Diffuse Intensity", Range(0, 5)) = 1.0 + _SSAOWeight("SSAO Weight", Range(0.0, 1.0)) = 1.0 + _SSGIWeight("SSGI Weight", Range(0.0, 1.0)) = 1.0 + + _IndirectSpecularMatCapMap("IndirectSpecularMatCapMap", 2D) = "black" {} + _IndirectSpecularMatCapLod("IndirectSpecularMatCapMapLOD", Range(-5, 5)) = 0.0 + + _IndirectSpecularIntensity("Indirect Specular Intensity", Range(0, 5)) = 1.0 + _SSRWeight("SSR Weight", Range(0.0, 1.0)) = 1.0 // Angel Rings _AngelRingColor("Angel Ring Color", Color) = (1, 1, 1, 1) @@ -319,12 +329,13 @@ Shader "HDRP/Toon" _OutlineWidthMap("Outline Tex", 2D) = "white" {} _OutlineColor("Outline Color", Color) = (0.5, 0.5, 0.5, 1) _OutlineColorMap("Outline Color Map", 2D) = "white" {} - [Toggle(_)] _AlbedoAffectOutline("AlbedoAffectOutline", Float) = 0 + [ToggleUI] _AlbedoAffectOutline("Albedo Affect Outline", Float) = 1 + [ToggleUI] _SkyColorAffectOutline("Sky Color Affect Outline", Float) = 1 + _SkyColorIntensity("Sky Color Intensity", Range(0, 5)) = 1 + _OutlineFadeIn("Outline Fade In", Float) = 50 _OutlineFadeOut("Outline Fade Out", Float) = 100 - [Toggle(_)] _UseSmoothedNormal("Use Smoothed Normal", Float) = 0 - //[HideInInspector]_Cutoff("Alpha cutoff", Range(0, 1)) = 0.5 - _BakedNormal("Baked Normal for Outline", 2D) = "white" {} + [ToggleUI] _UseSmoothedNormal("Use Smoothed Normal", Float) = 0 @@ -429,12 +440,8 @@ Shader "HDRP/Toon" //RimLightMask _Set_RimLightMask("Set_RimLightMask", 2D) = "white" {} _Tweak_RimLightMaskLevel("Tweak_RimLightMaskLevel", Range(-1, 1)) = 0 - // - [Enum(Off, 0, IBL, 1, Matcap, 2, Ramp, 3)]_Indirect_Diffuse_Mode("_Indirect_Diffuse_Mode", Integer) = 0 - [Enum(Off, 0, IBL, 1, Matcap, 2)]_Indirect_Specular_Mode("_Indirect_Specular_Mode", Integer) = 0 [Toggle(_)] _MatCap("MatCap", Float) = 0 - _MatCapMap("MatCapMap", 2D) = "black" {} //v.2.0.6 _BlurLevelMatcap("Blur Level of MatCap_Sampler", Range(0, 10)) = 0 _MatCapColor("MatCapColor", Color) = (1, 1, 1, 1) @@ -473,8 +480,6 @@ Shader "HDRP/Toon" [Toggle(_)] _Is_ViewCoord_Scroll("Is_ViewCoord_Scroll", Float) = 0 // //GI Intensity - _ID_Intensity("GI_Intensity", Range(0, 1)) = 1 - _IR_Intensity("GI_Intensity", Range(0, 1)) = 1 _Light_Intensity_Multiplier("Light_Intensity_Multiplier" , Range(0, 1)) = 0.25 //For VR Chat under No effective light objects _Unlit_Intensity("Unlit_Intensity", Range(0, 4)) = 0 @@ -1010,8 +1015,8 @@ Shader "HDRP/Toon" #pragma shader_feature_local_fragment _RECEIVE_SCREEN_SPACE_SHADOW_ON #pragma shader_feature_local_fragment _RECEIVE_HAIR_SHADOW_ON - #pragma shader_feature_local_fragment _INDIRECT_DIFFUSE_OFF _INDIRECT_DIFFUSE_IBL _INDIRECT_DIFFUSE_MATCAP _INDIRECT_DIFFUSE_RAMP - #pragma shader_feature_local_fragment _INDIRECT_SPECULAR_OFF _INDIRECT_SPECULAR_IBL _INDIRECT_SPECULAR_MATCAP + #pragma shader_feature_local_fragment _INDIRECT_DIFFUSE_MODE_OFF _INDIRECT_DIFFUSE_MODE_IBL _INDIRECT_DIFFUSE_MODE_MATCAP _INDIRECT_DIFFUSE_MODE_RAMP + #pragma shader_feature_local_fragment _INDIRECT_SPECULAR_MODE_OFF _INDIRECT_SPECULAR_MODE_IBL _INDIRECT_SPECULAR_MODE_MATCAP #pragma shader_feature_local_fragment _MASKMAP #pragma shader_feature_local_fragment _NORMAL_MAP diff --git a/Runtime/Shaders/Includes/Lighting/UtsLightEvaluation.hlsl b/Runtime/Shaders/Includes/Lighting/UtsLightEvaluation.hlsl index 60bc43e..9541383 100644 --- a/Runtime/Shaders/Includes/Lighting/UtsLightEvaluation.hlsl +++ b/Runtime/Shaders/Includes/Lighting/UtsLightEvaluation.hlsl @@ -102,15 +102,18 @@ IndirectLighting UtsEvaluateBSDF_ScreenSpaceReflection(PositionInputs posInput, IndirectLighting lighting; ZERO_INITIALIZE(IndirectLighting, lighting); - // TODO: this texture is sparse (mostly black). Can we avoid reading every texel? How about using Hi-S? - float4 ssrLighting = LOAD_TEXTURE2D_X(_SsrLightingTexture, posInput.positionSS); - InversePreExposeSsrLighting(ssrLighting); + if (_SSRWeight > 0.0) + { + // TODO: this texture is sparse (mostly black). Can we avoid reading every texel? How about using Hi-S? + float4 ssrLighting = LOAD_TEXTURE2D_X(_SsrLightingTexture, posInput.positionSS); + InversePreExposeSsrLighting(ssrLighting); - // Apply the weight on the ssr contribution (if required) - ApplyScreenSpaceReflectionWeight(ssrLighting); + // Apply the weight on the ssr contribution (if required) + ApplyScreenSpaceReflectionWeight(ssrLighting); - reflectionHierarchyWeight = ssrLighting.a; - lighting.specularReflected = ssrLighting.rgb * preLightData.specularFGD; + reflectionHierarchyWeight = ssrLighting.a; + lighting.specularReflected = lerp(lighting.specularReflected, ssrLighting.rgb * preLightData.specularFGD, _SSRWeight); + } return lighting; } @@ -125,7 +128,8 @@ void UtsEvaluateBSDF_BakeDiffuse(PositionInputs posInput, PreLightData preLightD #if !defined(_SURFACE_TYPE_TRANSPARENT) && !defined(SCREEN_SPACE_INDIRECT_DIFFUSE_DISABLED) if (_IndirectDiffuseMode != INDIRECTDIFFUSEMODE_OFF) { - builtinData.bakeDiffuseLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier(); + float3 ssgiLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier(); + builtinData.bakeDiffuseLighting = lerp(builtinData.bakeDiffuseLighting, ssgiLighting.rgb, _SSGIWeight); } else #endif @@ -168,7 +172,8 @@ void UtsEvaluateBSDF_MatCapDiffuse(float3 positionWS, float3 normalWS, inout Bui uv.x *= -1; uv = uv * 0.5 + 0.5; - builtinData.bakeDiffuseLighting = SAMPLE_TEXTURE2D_LOD(_MatCapMap, s_linear_clamp_sampler, uv, UNITY_SPECCUBE_LOD_STEPS).rgb * GetInverseCurrentExposureMultiplier(); + float lod = clamp(UNITY_SPECCUBE_LOD_STEPS + _IndirectDiffuseMatCapLod, 0.0, 10.0); + builtinData.bakeDiffuseLighting = SAMPLE_TEXTURE2D_LOD(_IndirectDiffuseMatCapMap, s_trilinear_clamp_sampler, uv, lod).rgb * GetInverseCurrentExposureMultiplier(); } IndirectLighting UtsEvaluateBSDF_MatCapSpecular(float3 positionWS, UtsBSDFData bsdfData, PreLightData preLightData) @@ -184,7 +189,8 @@ IndirectLighting UtsEvaluateBSDF_MatCapSpecular(float3 positionWS, UtsBSDFData b uv.x *= -1; uv = uv * 0.5 + 0.5; - lighting.specularReflected = SAMPLE_TEXTURE2D_LOD(_MatCapMap, s_linear_clamp_sampler, uv, PerceptualRoughnessToMipmapLevel(bsdfData.perceptualRoughness)).rgb; + float lod = clamp(PerceptualRoughnessToMipmapLevel(bsdfData.perceptualRoughness) + _IndirectSpecularMatCapLod, 0.0, 10.0); + lighting.specularReflected = SAMPLE_TEXTURE2D_LOD(_IndirectSpecularMatCapMap, s_trilinear_clamp_sampler, uv, lod).rgb; lighting.specularReflected *= preLightData.specularFGD * GetInverseCurrentExposureMultiplier(); return lighting; @@ -240,27 +246,37 @@ IndirectLighting UtsEvaluateBSDF_Env(LightLoopContext lightLoopContext, Position return lighting; } +void ApplyAmbientOcclusion(AmbientOcclusionFactor aoFactor, inout BuiltinData builtinData, inout AggregateLighting lighting) +{ + builtinData.bakeDiffuseLighting = APPLY_WEIGHT(builtinData.bakeDiffuseLighting, aoFactor.indirectAmbientOcclusion, _SSAOWeight); + lighting.indirect.specularReflected = APPLY_WEIGHT(lighting.indirect.specularReflected, aoFactor.indirectSpecularOcclusion, _SSAOWeight); + lighting.direct.diffuse = APPLY_WEIGHT(lighting.direct.diffuse, aoFactor.directAmbientOcclusion, _SSAOWeight); + lighting.direct.specular = APPLY_WEIGHT(lighting.direct.specular, aoFactor.directSpecularOcclusion, _SSAOWeight); +} + +void AdjustIndirectLighting(PreLightData preLightData, UtsBSDFData bsdfData, inout BuiltinData builtinData, inout AggregateLighting lighting) +{ + builtinData.bakeDiffuseLighting = ApplyCurrentExposureMultiplier(builtinData.bakeDiffuseLighting * bsdfData.diffuseColor * preLightData.diffuseFGD * _IndirectDiffuseIntensity); + lighting.indirect.specularReflected = ApplyCurrentExposureMultiplier(lighting.indirect.specularReflected * bsdfData.fresnel0 * _IndirectSpecularIntensity); +} + void UtsPostEvaluateBSDF(PositionInputs posInput, PreLightData preLightData, UtsBSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting, out LightLoopOutput lightLoopOutput) { +#if !defined(_INDIRECT_DIFFUSE_MODE_OFF) && !defined(_INDIRECT_SPECULAR_MODE_OFF) AmbientOcclusionFactor aoFactor; GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.ambientOcclusion, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor); - builtinData.bakeDiffuseLighting = APPLY_WEIGHT(builtinData.bakeDiffuseLighting, aoFactor.indirectAmbientOcclusion, _AO_Factor); - lighting.indirect.specularReflected = APPLY_WEIGHT(lighting.indirect.specularReflected, aoFactor.indirectSpecularOcclusion, _AO_Factor); - lighting.direct.diffuse = APPLY_WEIGHT(lighting.direct.diffuse, aoFactor.directAmbientOcclusion, _AO_Factor); - lighting.direct.specular = APPLY_WEIGHT(lighting.direct.specular, aoFactor.directSpecularOcclusion, _AO_Factor); - - builtinData.bakeDiffuseLighting = ApplyCurrentExposureMultiplier(builtinData.bakeDiffuseLighting * bsdfData.diffuseColor * preLightData.diffuseFGD * _ID_Intensity); - lighting.indirect.specularReflected = ApplyCurrentExposureMultiplier(lighting.indirect.specularReflected * bsdfData.fresnel0 * _IR_Intensity); + ApplyAmbientOcclusion(aoFactor, builtinData, lighting); +#endif + AdjustIndirectLighting(preLightData, bsdfData, builtinData, lighting); - lightLoopOutput.diffuseLighting = lighting.direct.diffuse + builtinData.bakeDiffuseLighting; + // In regular pbr, we need to multiple diffuse color here with direct diffuse lighting. However, in UTS, we have already multiplied it when evaluating the direct diffuse since we need to apply the shading color. + lightLoopOutput.diffuseLighting = lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor; lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected; // Rescale the GGX to account for the multiple scattering. lightLoopOutput.specularLighting *= 1.0 + bsdfData.fresnel0 * preLightData.energyCompensation; ApplyExposureAdjustment(lightLoopOutput.diffuseLighting); ApplyExposureAdjustment(lightLoopOutput.specularLighting); - - lightLoopOutput.diffuseLighting += builtinData.emissiveColor; } #endif \ No newline at end of file diff --git a/Runtime/Shaders/Includes/Lighting/UtsLightLoop.hlsl b/Runtime/Shaders/Includes/Lighting/UtsLightLoop.hlsl index 386ac63..d8ce557 100644 --- a/Runtime/Shaders/Includes/Lighting/UtsLightLoop.hlsl +++ b/Runtime/Shaders/Includes/Lighting/UtsLightLoop.hlsl @@ -247,9 +247,9 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs #endif float3 lightInReflDir = 0.0; - #ifdef _INDIRECT_DIFFUSE_OFF + #ifdef _INDIRECT_DIFFUSE_MODE_OFF - #elif _INDIRECT_DIFFUSE_IBL + #elif _INDIRECT_DIFFUSE_MODE_IBL bool replaceBakeDiffuseLighting = false; #if !defined(_SURFACE_TYPE_TRANSPARENT) // No SSGI/RTGI/Mixed effect on transparent if (_IndirectDiffuseMode != INDIRECTDIFFUSEMODE_OFF) @@ -273,18 +273,18 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs { UtsEvaluateBSDF_BakeDiffuse(posInput, preLightData, bsdfData, V, builtinData, lightInReflDir); } - #elif _INDIRECT_DIFFUSE_MATCAP + #elif _INDIRECT_DIFFUSE_MODE_MATCAP //builtinData.bakeDiffuseLighting = UtsEvaluateColor_MatCap(posInput.positionWS, bsdfData.normalWS, 0.0); UtsEvaluateBSDF_MatCapDiffuse(posInput.positionWS, bsdfData.normalWS, builtinData); - #elif _INDIRECT_DIFFUSE_RAMP + #elif _INDIRECT_DIFFUSE_MODE_RAMP UtsEvaluateBSDF_Ramp(posInput, bsdfData, builtinData); #endif if (featureFlags & LIGHTFEATUREFLAGS_ENV) { - #if _INDIRECT_SPECULAR_OFF + #if _INDIRECT_SPECULAR_MODE_OFF - #elif _INDIRECT_SPECULAR_IBL + #elif _INDIRECT_SPECULAR_MODE_IBL context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES; #if SCALARIZE_LIGHT_LOOP @@ -346,13 +346,13 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs } } - #elif _INDIRECT_SPECULAR_MATCAP + #elif _INDIRECT_SPECULAR_MODE_MATCAP IndirectLighting lighting = UtsEvaluateBSDF_MatCapSpecular(posInput.positionWS, bsdfData, preLightData); AccumulateIndirectLighting(lighting, aggregateLighting); #endif } - #if _INDIRECT_SPECULAR_IBL + #if _INDIRECT_SPECULAR_MODE_IBL // Only apply the sky IBL if the sky texture is available if ((featureFlags & LIGHTFEATUREFLAGS_SKY) && _EnvLightSkyEnabled) { diff --git a/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl b/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl index 245b566..1b706ed 100644 --- a/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl @@ -161,9 +161,6 @@ float4 _EmissiveColorMap_ST; float _TexWorldScaleEmissive; float4 _UVMappingMaskEmissive; -float _ID_Intensity; -float _IR_Intensity; - float4 _InvPrimScale; // Only XY are used // Wind @@ -202,12 +199,6 @@ float4 _SpecularColor; float _ToonSpecularStep; float _ToonSpecularFeather; -float _ReceivesSSR; -float _ReceivesSSAO; -float _AO_Factor; -float _ReceivesSSGI; -float _GIMultiplier; - float _NormalScale; float4 _DetailMap_ST; diff --git a/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl b/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl index bbb7cb6..3ac8247 100644 --- a/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl @@ -9,7 +9,8 @@ SAMPLER(sampler_SDFShadingMap); TEXTURE2D_ARRAY(_ShadingRampMap); -TEXTURE2D(_MatCapMap); +TEXTURE2D(_IndirectDiffuseMatCapMap); +TEXTURE2D(_IndirectSpecularMatCapMap); sampler _Set_1st_ShadePosition; sampler _Set_2nd_ShadePosition; @@ -27,7 +28,4 @@ TEXTURE2D(_OutlineWidthMap); SAMPLER(sampler_OutlineWidthMap); TEXTURE2D(_OutlineColorMap); -SAMPLER(sampler_OutlineColorMap); - -TEXTURE2D(_BakedNormalMap); -SAMPLER(sampler_BakedNormalMap); \ No newline at end of file +SAMPLER(sampler_OutlineColorMap); \ No newline at end of file diff --git a/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl b/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl index 196c8d7..a1b01bb 100644 --- a/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl @@ -25,6 +25,30 @@ float _SDFHighlightStrength; float _ShadowDistanceBias; float _ShadowNormalBias; +// Ambient +float _IndirectDiffuseMatCapLod; + +float _IndirectDiffuseIntensity; +float _SSAOWeight; +float _SSGIWeight; + +float _IndirectSpecularMatCapLod; + +float _IndirectSpecularIntensity; +float _SSRWeight; + +// Outline +float _OutlineWidth; +float4 _OutlineColor; +fixed _AlbedoAffectOutline; +fixed _SkyColorAffectOutline; +float _SkyColorIntensity; + +float _OutlineFadeIn; +float _OutlineFadeOut; +fixed _UseSmoothedNormal; + + float _utsTechnique; float4 _Color; @@ -170,13 +194,6 @@ float _AngelRingIntensity; float _AngelRingOffsetU; float _AngelRingOffsetV; -float _OutlineWidth; -float4 _OutlineColor; -fixed _AlbedoAffectOutline; -float _OutlineFadeIn; -float _OutlineFadeOut; -fixed _UseSmoothedNormal; - #if defined(_UTS_TOON_EV_PER_MODEL) // not in materials int _ToonLightHiCutFilter; diff --git a/Runtime/Shaders/Includes/ShaderPass/HDRPToonOutline.hlsl b/Runtime/Shaders/Includes/ShaderPass/HDRPToonOutline.hlsl index 74b5c4d..af11473 100644 --- a/Runtime/Shaders/Includes/ShaderPass/HDRPToonOutline.hlsl +++ b/Runtime/Shaders/Includes/ShaderPass/HDRPToonOutline.hlsl @@ -132,26 +132,23 @@ void Frag(PackedVaryingsToPS packedInput, float4 objPos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)); float4 uv0 = input.texCoord0; - // The following temporary definition of unity_AmbientEquator is for HDRP only. - //float4 unity_AmbientEquator = float4(0.05, 0.05, 0.05, 1.0); //Todo. - //v.2.0.9 - //float3 envLightSource_GradientEquator = unity_AmbientEquator.rgb > 0.05 ? unity_AmbientEquator.rgb : half3(0.05, 0.05, 0.05); float3 envLightSource_GradientEquator = ShadeSH9(float4(0, 1, 0, 0)); float3 envLightSource_SkyboxIntensity = max( - SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, 0.0, 0.0), input.texCoord1.xy, input.texCoord2.xy), - SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, -1.0, 0.0), input.texCoord1.xy, input.texCoord2.xy) + SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, 1.0, 0.0), input.texCoord1.xy, input.texCoord2.xy), + SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, -1.0, -0.0), input.texCoord1.xy, input.texCoord2.xy) ).rgb; float3 ambientSkyColor = envLightSource_SkyboxIntensity.rgb > 0.0 ? envLightSource_SkyboxIntensity : envLightSource_GradientEquator; - ambientSkyColor *= GetCurrentExposureMultiplier() * 5.0f; + ambientSkyColor *= GetCurrentExposureMultiplier() * _SkyColorIntensity; float3 baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, TRANSFORM_TEX(uv0, _BaseColorMap)).rgb; baseColor *= _BaseColor.rgb; float4 outlineColor = _OutlineColor; + outlineColor.rgb = lerp(outlineColor.rgb, outlineColor.rgb + ambientSkyColor, _SkyColorAffectOutline); #if _OUTLINE_COLOR_MAP outlineColor *= SAMPLE_TEXTURE2D(_OutlineColorMap, sampler_OutlineColorMap, TRANSFORM_TEX(uv0, _BaseColorMap)).rgb; #endif - outlineColor.rgb = lerp(outlineColor.rgb * ambientSkyColor, outlineColor.rgb * ambientSkyColor * baseColor, _AlbedoAffectOutline); + outlineColor.rgb = lerp(outlineColor.rgb, outlineColor.rgb * baseColor, _AlbedoAffectOutline); float3 volColor, volOpacity;