From 0f59fc5fa8a0d57e3c5780f49b6279738c0b55a8 Mon Sep 17 00:00:00 2001 From: yum Date: Sun, 26 May 2024 14:31:25 -0700 Subject: Overhaul PBR overlay Add second PBR overlay and fix masking behavior. Also elide keywords based on shader variant. This should improve performance. --- Editor/tooner.cs | 133 ++++++++++++++++++++++++----------------- globals.cginc | 139 ++++++++++++++++++++++++++++++++++-------- pbr.cginc | 15 +++-- tooner.shader | 149 ++++++++++++++++++++++++++++++++++++++-------- tooner_lighting.cginc | 94 ++++++++++++++++++----------- tooner_outline_pass.cginc | 27 ++++++++- 6 files changed, 410 insertions(+), 147 deletions(-) diff --git a/Editor/tooner.cs b/Editor/tooner.cs index 29d6a67..c4c9091 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -99,63 +99,78 @@ public class ToonerGUI : ShaderGUI { } void DoPBROverlay() { - MaterialProperty bc = FindProperty("_PBR_Overlay_Enable"); - bool enabled = bc.floatValue > 1E-6; - EditorGUI.BeginChangeCheck(); - enabled = EditorGUILayout.Toggle("Enable", enabled); - EditorGUI.EndChangeCheck(); - bc.floatValue = enabled ? 1.0f : 0.0f; - SetKeyword("_PBR_OVERLAY", enabled); + for (int i = 0; i < 2; i++) { + GUILayout.Label($"PBR overlay {i}", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; - if (enabled) { - bc = FindProperty("_PBR_Overlay_BaseColor"); - MaterialProperty bct = FindProperty("_PBR_Overlay_BaseColorTex"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Base color (RGBA)"), - bct, - bc); - if (bct.textureValue) { - editor.TextureScaleOffsetProperty(bct); - } - SetKeyword("_PBR_OVERLAY_BASECOLOR_MAP", bct.textureValue); + MaterialProperty bc = FindProperty($"_PBR_Overlay{i}_Enable"); + bool enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + SetKeyword($"_PBR_OVERLAY{i}", enabled); - bct = FindProperty("_PBR_Overlay_NormalTex"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Normal"), - bct, - FindProperty("_PBR_Overlay_Tex_NormalStr")); - if (bct.textureValue) { - editor.TextureScaleOffsetProperty(bct); - } - SetKeyword("_PBR_OVERLAY_NORMAL_MAP", bct.textureValue); + if (enabled) { + bc = FindProperty($"_PBR_Overlay{i}_BaseColor"); + MaterialProperty bct = FindProperty($"_PBR_Overlay{i}_BaseColorTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Base color (RGBA)"), + bct, + bc); + if (bct.textureValue) { + editor.TextureScaleOffsetProperty(bct); + } + SetKeyword($"_PBR_OVERLAY{i}_BASECOLOR_MAP", bct.textureValue); - bc = FindProperty("_PBR_Overlay_Metallic"); - bct = FindProperty("_PBR_Overlay_MetallicTex"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Metallic (RGBA)"), - bct, - bc); - if (bct.textureValue) { - editor.TextureScaleOffsetProperty(bct); - } - SetKeyword("_PBR_OVERLAY_METALLIC_MAP", bct.textureValue); + bct = FindProperty($"_PBR_Overlay{i}_NormalTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Normal"), + bct, + FindProperty($"_PBR_Overlay{i}_Tex_NormalStr")); + if (bct.textureValue) { + editor.TextureScaleOffsetProperty(bct); + } + SetKeyword($"_PBR_OVERLAY{i}_NORMAL_MAP", bct.textureValue); - bc = FindProperty("_PBR_Overlay_Roughness"); - bct = FindProperty("_PBR_Overlay_RoughnessTex"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Roughness (RGBA)"), - bct, - bc); - if (bct.textureValue) { - editor.TextureScaleOffsetProperty(bct); - } - SetKeyword("_PBR_OVERLAY_ROUGHNESS_MAP", bct.textureValue); + bc = FindProperty($"_PBR_Overlay{i}_Metallic"); + bct = FindProperty($"_PBR_Overlay{i}_MetallicTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Metallic (RGBA)"), + bct, + bc); + if (bct.textureValue) { + editor.TextureScaleOffsetProperty(bct); + } + SetKeyword($"_PBR_OVERLAY{i}_METALLIC_MAP", bct.textureValue); - bct = FindProperty("_PBR_Overlay_Mask"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Mask"), - bct); - SetKeyword("_PBR_OVERLAY_MASK", bct.textureValue); + bc = FindProperty($"_PBR_Overlay{i}_Roughness"); + bct = FindProperty($"_PBR_Overlay{i}_RoughnessTex"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Roughness (RGBA)"), + bct, + bc); + if (bct.textureValue) { + editor.TextureScaleOffsetProperty(bct); + } + SetKeyword($"_PBR_OVERLAY{i}_ROUGHNESS_MAP", bct.textureValue); + + bct = FindProperty($"_PBR_Overlay{i}_Mask"); + editor.TexturePropertySingleLine( + MakeLabel(bct, "Mask"), + bct); + SetKeyword($"_PBR_OVERLAY{i}_MASK", bct.textureValue); + + if (bct.textureValue) { + bc = FindProperty($"_PBR_Overlay{i}_Mask_Invert"); + enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Invert mask", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + } + } + EditorGUI.indentLevel -= 1; } } @@ -179,6 +194,17 @@ public class ToonerGUI : ShaderGUI { editor.RangeProperty( bc, "Max brightness"); + + bc = FindProperty("_Ambient_Occlusion"); + editor.TexturePropertySingleLine( + MakeLabel(bc, "Ambient occlusion"), + bc); + SetKeyword("_AMBIENT_OCCLUSION", bc.textureValue); + + if (bc.textureValue) { + bc = FindProperty("_Ambient_Occlusion_Strength"); + editor.RangeProperty(bc, "Ambient occlusion strength"); + } } void DoEmission() { @@ -773,10 +799,7 @@ public class ToonerGUI : ShaderGUI { DoRoughness(); EditorGUI.indentLevel -= 1; - GUILayout.Label("PBR overlay", EditorStyles.boldLabel); - EditorGUI.indentLevel += 1; DoPBROverlay(); - EditorGUI.indentLevel -= 1; GUILayout.Label("Lighting", EditorStyles.boldLabel); EditorGUI.indentLevel += 1; diff --git a/globals.cginc b/globals.cginc index 4d80955..6e56eac 100644 --- a/globals.cginc +++ b/globals.cginc @@ -3,12 +3,26 @@ #include "AutoLight.cginc" +SamplerState linear_repeat_s; + float4 _BaseColor; float _Metallic; float _Roughness; +float _Tex_NormalStr; +float _NormalStr; + +float _Min_Brightness; +float _Max_Brightness; + +float _Mesh_Normals_Mode; +float _Flatten_Mesh_Normals_Str; +float _Confabulate_Normals; + +#if defined(_BASECOLOR_MAP) texture2D _BaseColorTex; float4 _BaseColorTex_ST; +#endif texture2D _NormalTex; float4 _NormalTex_ST; texture2D _MetallicTex; @@ -16,43 +30,97 @@ float4 _MetallicTex_ST; texture2D _RoughnessTex; float4 _RoughnessTex_ST; -float4 _PBR_Overlay_BaseColor; -float _PBR_Overlay_Metallic; -float _PBR_Overlay_Roughness; -texture2D _PBR_Overlay_BaseColorTex; -float4 _PBR_Overlay_BaseColorTex_ST; -texture2D _PBR_Overlay_NormalTex; -float4 _PBR_Overlay_NormalTex_ST; -texture2D _PBR_Overlay_MetallicTex; -float4 _PBR_Overlay_MetallicTex_ST; -texture2D _PBR_Overlay_RoughnessTex; -float4 _PBR_Overlay_RoughnessTex_ST; -float _PBR_Overlay_Tex_NormalStr; -texture2D _PBR_Overlay_Mask; +#if defined(_PBR_OVERLAY0) +float4 _PBR_Overlay0_BaseColor; +float _PBR_Overlay0_Metallic; +float _PBR_Overlay0_Roughness; +texture2D _PBR_Overlay0_BaseColorTex; +float4 _PBR_Overlay0_BaseColorTex_ST; +texture2D _PBR_Overlay0_NormalTex; +float4 _PBR_Overlay0_NormalTex_ST; +texture2D _PBR_Overlay0_MetallicTex; +float4 _PBR_Overlay0_MetallicTex_ST; +texture2D _PBR_Overlay0_RoughnessTex; +float4 _PBR_Overlay0_RoughnessTex_ST; +float _PBR_Overlay0_Tex_NormalStr; +texture2D _PBR_Overlay0_Mask; +float _PBR_Overlay0_Mask_Invert; +#endif -texture2D _EmissionTex; -float _EmissionStrength; +#if defined(_PBR_OVERLAY1) +float4 _PBR_Overlay1_BaseColor; +float _PBR_Overlay1_Metallic; +float _PBR_Overlay1_Roughness; +texture2D _PBR_Overlay1_BaseColorTex; +float4 _PBR_Overlay1_BaseColorTex_ST; +texture2D _PBR_Overlay1_NormalTex; +float4 _PBR_Overlay1_NormalTex_ST; +texture2D _PBR_Overlay1_MetallicTex; +float4 _PBR_Overlay1_MetallicTex_ST; +texture2D _PBR_Overlay1_RoughnessTex; +float4 _PBR_Overlay1_RoughnessTex_ST; +float _PBR_Overlay1_Tex_NormalStr; +texture2D _PBR_Overlay1_Mask; +float _PBR_Overlay1_Mask_Invert; +#endif -SamplerState linear_repeat_s; +#if defined(_PBR_OVERLAY2) +float4 _PBR_Overlay2_BaseColor; +float _PBR_Overlay2_Metallic; +float _PBR_Overlay2_Roughness; +texture2D _PBR_Overlay2_BaseColorTex; +float4 _PBR_Overlay2_BaseColorTex_ST; +texture2D _PBR_Overlay2_NormalTex; +float4 _PBR_Overlay2_NormalTex_ST; +texture2D _PBR_Overlay2_MetallicTex; +float4 _PBR_Overlay2_MetallicTex_ST; +texture2D _PBR_Overlay2_RoughnessTex; +float4 _PBR_Overlay2_RoughnessTex_ST; +float _PBR_Overlay2_Tex_NormalStr; +texture2D _PBR_Overlay2_Mask; +float _PBR_Overlay2_Mask_Invert; +#endif -float _Tex_NormalStr; -float _NormalStr; +#if defined(_PBR_OVERLAY3) +float4 _PBR_Overlay3_BaseColor; +float _PBR_Overlay3_Metallic; +float _PBR_Overlay3_Roughness; +texture2D _PBR_Overlay3_BaseColorTex; +float4 _PBR_Overlay3_BaseColorTex_ST; +texture2D _PBR_Overlay3_NormalTex; +float4 _PBR_Overlay3_NormalTex_ST; +texture2D _PBR_Overlay3_MetallicTex; +float4 _PBR_Overlay3_MetallicTex_ST; +texture2D _PBR_Overlay3_RoughnessTex; +float4 _PBR_Overlay3_RoughnessTex_ST; +float _PBR_Overlay3_Tex_NormalStr; +texture2D _PBR_Overlay3_Mask; +float _PBR_Overlay3_Mask_Invert; +#endif -float _Min_Brightness; -float _Max_Brightness; +#if defined(_EMISSION) +texture2D _EmissionTex; +float _EmissionStrength; +#endif -float _Alpha_Cutoff; +#if defined(_AMBIENT_OCCLUSION) +texture2D _Ambient_Occlusion; +float _Ambient_Occlusion_Strength; +#endif -float _Mesh_Normals_Mode; -float _Flatten_Mesh_Normals_Str; -float _Confabulate_Normals; +#if defined(_RENDERING_CUTOUT) +float _Alpha_Cutoff; +#endif +#if defined(_OUTLINES) float _Outline_Width; float4 _Outline_Color; float _Outline_Emission_Strength; texture2D _Outline_Mask; float _Outline_Mask_Invert; +#endif +#if defined(_GLITTER) texture2D _Glitter_Mask; float _Glitter_Density; float _Glitter_Amount; @@ -61,19 +129,27 @@ float _Glitter_Seed; float _Glitter_Brightness; float _Glitter_Angle; float _Glitter_Power; +#endif +#if defined(_EXPLODE) float _Explode_Phase; +#endif +#if defined(_SCROLL) float _Scroll_Toggle; float _Scroll_Top; float _Scroll_Bottom; float _Scroll_Width; float _Scroll_Strength; float _Scroll_Speed; +#endif +#if defined(_TESSELLATION) float _Tess_Factor; float _Tess_Dist_Cutoff; +#endif +#if defined(_MATCAP0) float _Enable_Matcap0; texture2D _Matcap0; texture2D _Matcap0_Mask; @@ -81,7 +157,9 @@ float _Matcap0_Mask_Invert; float _Matcap0Str; float _Matcap0Mode; float _Matcap0Emission; +#endif +#if defined(_MATCAP1) float _Enable_Matcap1; texture2D _Matcap1; texture2D _Matcap1_Mask; @@ -89,7 +167,9 @@ float _Matcap1_Mask_Invert; float _Matcap1Str; float _Matcap1Mode; float _Matcap1Emission; +#endif +#if defined(_RIM_LIGHTING0) float _Rim_Lighting0_Enabled; float _Rim_Lighting0_Mode; float3 _Rim_Lighting0_Color; @@ -99,7 +179,9 @@ float _Rim_Lighting0_Center; float _Rim_Lighting0_Power; float _Rim_Lighting0_Strength; float _Rim_Lighting0_Emission; +#endif +#if defined(_RIM_LIGHTING1) float _Rim_Lighting1_Enabled; float _Rim_Lighting1_Mode; float3 _Rim_Lighting1_Color; @@ -109,27 +191,36 @@ float _Rim_Lighting1_Center; float _Rim_Lighting1_Power; float _Rim_Lighting1_Strength; float _Rim_Lighting1_Emission; +#endif +#if defined(_OKLAB) float _OKLAB_Enabled; texture2D _OKLAB_Mask; float _OKLAB_Lightness_Shift; float _OKLAB_Chroma_Shift; float _OKLAB_Hue_Shift; +#endif +#if defined(_CLONES) float _Clones_Enabled; float _Clones_Count; float _Clones_dx; float _Clones_Dist_Cutoff; +#endif +#if defined(_UVSCROLL) float _UVScroll_Enabled; texture2D _UVScroll_Mask; float _UVScroll_U_Speed; float _UVScroll_V_Speed; texture2D _UVScroll_Alpha; +#endif +#if defined(_LTCGI) float _LTCGI_Enabled; float4 _LTCGI_SpecularColor; float4 _LTCGI_DiffuseColor; +#endif #endif diff --git a/pbr.cginc b/pbr.cginc index e171f89..7f97e56 100644 --- a/pbr.cginc +++ b/pbr.cginc @@ -10,11 +10,11 @@ UNITY_DECLARE_TEXCUBE(_Cubemap); -UnityLight CreateDirectLight(float3 normal, v2f i) +UnityLight CreateDirectLight(float3 normal, float ao, v2f i) { UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos); UnityLight light; - light.color = _LightColor0.rgb * attenuation; + light.color = _LightColor0.rgb * attenuation * ao; #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT) light.dir = normalize((_WorldSpaceLightPos0 - i.worldPos).xyz); #else @@ -51,7 +51,7 @@ float3 BoxProjection ( } UnityIndirect CreateIndirectLight(float4 vertexLightColor, float3 view_dir, float3 normal, - float smoothness, float3 worldPos, float2 uv) { + float smoothness, float3 worldPos, float ao, float2 uv) { UnityIndirect indirect; indirect.diffuse = vertexLightColor; indirect.specular = 0; @@ -109,6 +109,8 @@ UnityIndirect CreateIndirectLight(float4 vertexLightColor, float3 view_dir, floa indirect.specular = probe0; #endif // FORWARD_BASE_PASS + indirect.diffuse *= ao; + return indirect; } @@ -117,7 +119,8 @@ float4 getLitColor( float4 albedo, float3 worldPos, float3 normal, - float metallic, float smoothness, float2 uv, v2f i) + float metallic, float smoothness, float2 uv, float ao, + v2f i) { float3 specular_tint; float one_minus_reflectivity; @@ -135,9 +138,9 @@ float4 getLitColor( normal = lerp(spherical_normal, flat_normal, flat); UnityIndirect indirect_light = CreateIndirectLight(vertexLightColor, - view_dir, normal, smoothness, worldPos, uv); + view_dir, normal, smoothness, worldPos, ao, uv); - UnityLight direct_light = CreateDirectLight(normal, i); + UnityLight direct_light = CreateDirectLight(normal, ao, i); if (flat) { float e = 0.8; indirect_light.diffuse += direct_light.color * e; diff --git a/tooner.shader b/tooner.shader index 29a2afb..a51cd54 100644 --- a/tooner.shader +++ b/tooner.shader @@ -11,16 +11,53 @@ Shader "yum_food/tooner" [NoScaleOffset] _MetallicTex("Metallic", 2D) = "white" {} [NoScaleOffset] _RoughnessTex("Roughness", 2D) = "black" {} - _PBR_Overlay_Enable("Enable PBR overlay", Float) = 0.0 - _PBR_Overlay_BaseColor("Base color", Color) = (0.8, 0.8, 0.8, 1) - _PBR_Overlay_Metallic("Metallic", Range(0, 1)) = 0 - _PBR_Overlay_Roughness("Roughness", Range(0, 1)) = 1 - [NoScaleOffset] _PBR_Overlay_BaseColorTex("Base color", 2D) = "white" {} - [NoScaleOffset] _PBR_Overlay_NormalTex("Normal", 2D) = "bump" {} - [NoScaleOffset] _PBR_Overlay_MetallicTex("Metallic", 2D) = "white" {} - [NoScaleOffset] _PBR_Overlay_RoughnessTex("Roughness", 2D) = "black" {} - [NoScaleOffset] _PBR_Overlay_Tex_NormalStr("Normal texture strength", Range(0, 10)) = 1 - [NoScaleOffset] _PBR_Overlay_Mask("Mask", 2D) = "white" {} + _PBR_Overlay0_Enable("Enable PBR overlay", Float) = 0.0 + _PBR_Overlay0_BaseColor("Base color", Color) = (0.8, 0.8, 0.8, 1) + _PBR_Overlay0_Metallic("Metallic", Range(0, 1)) = 0 + _PBR_Overlay0_Roughness("Roughness", Range(0, 1)) = 1 + _PBR_Overlay0_BaseColorTex("Base color", 2D) = "white" {} + _PBR_Overlay0_NormalTex("Normal", 2D) = "bump" {} + _PBR_Overlay0_MetallicTex("Metallic", 2D) = "white" {} + _PBR_Overlay0_RoughnessTex("Roughness", 2D) = "black" {} + _PBR_Overlay0_Tex_NormalStr("Normal texture strength", Range(0, 10)) = 1 + _PBR_Overlay0_Mask("Mask", 2D) = "white" {} + _PBR_Overlay0_Mask_Invert("Mask invert", Float) = 0.0 + + _PBR_Overlay1_Enable("Enable PBR overlay", Float) = 0.0 + _PBR_Overlay1_BaseColor("Base color", Color) = (0.8, 0.8, 0.8, 1) + _PBR_Overlay1_Metallic("Metallic", Range(0, 1)) = 0 + _PBR_Overlay1_Roughness("Roughness", Range(0, 1)) = 1 + _PBR_Overlay1_BaseColorTex("Base color", 2D) = "white" {} + _PBR_Overlay1_NormalTex("Normal", 2D) = "bump" {} + _PBR_Overlay1_MetallicTex("Metallic", 2D) = "white" {} + _PBR_Overlay1_RoughnessTex("Roughness", 2D) = "black" {} + _PBR_Overlay1_Tex_NormalStr("Normal texture strength", Range(0, 10)) = 1 + _PBR_Overlay1_Mask("Mask", 2D) = "white" {} + _PBR_Overlay1_Mask_Invert("Mask invert", Float) = 0.0 + + _PBR_Overlay2_Enable("Enable PBR overlay", Float) = 0.0 + _PBR_Overlay2_BaseColor("Base color", Color) = (0.8, 0.8, 0.8, 1) + _PBR_Overlay2_Metallic("Metallic", Range(0, 1)) = 0 + _PBR_Overlay2_Roughness("Roughness", Range(0, 1)) = 1 + _PBR_Overlay2_BaseColorTex("Base color", 2D) = "white" {} + _PBR_Overlay2_NormalTex("Normal", 2D) = "bump" {} + _PBR_Overlay2_MetallicTex("Metallic", 2D) = "white" {} + _PBR_Overlay2_RoughnessTex("Roughness", 2D) = "black" {} + _PBR_Overlay2_Tex_NormalStr("Normal texture strength", Range(0, 10)) = 1 + _PBR_Overlay2_Mask("Mask", 2D) = "white" {} + _PBR_Overlay2_Mask_Invert("Mask invert", Float) = 0.0 + + _PBR_Overlay3_Enable("Enable PBR overlay", Float) = 0.0 + _PBR_Overlay3_BaseColor("Base color", Color) = (0.8, 0.8, 0.8, 1) + _PBR_Overlay3_Metallic("Metallic", Range(0, 1)) = 0 + _PBR_Overlay3_Roughness("Roughness", Range(0, 1)) = 1 + _PBR_Overlay3_BaseColorTex("Base color", 2D) = "white" {} + _PBR_Overlay3_NormalTex("Normal", 2D) = "bump" {} + _PBR_Overlay3_MetallicTex("Metallic", 2D) = "white" {} + _PBR_Overlay3_RoughnessTex("Roughness", 2D) = "black" {} + _PBR_Overlay3_Tex_NormalStr("Normal texture strength", Range(0, 10)) = 1 + _PBR_Overlay3_Mask("Mask", 2D) = "white" {} + _PBR_Overlay3_Mask_Invert("Mask invert", Float) = 0.0 [NoScaleOffset] _EmissionTex("Emission map", 2D) = "black" {} _EmissionStrength("Emission strength", Range(0, 2)) = 0 @@ -32,6 +69,8 @@ Shader "yum_food/tooner" _Max_Brightness("Max brightness", Range(0, 1.5)) = 1 _Mesh_Normal_Strength("Mesh normal strength", Range(0, 10)) = 1 _NormalStr("Normal strength", Range(0, 10)) = 1 + _Ambient_Occlusion("Ambient occlusion", 2D) = "white" {} + _Ambient_Occlusion_Strength("Ambient occlusion", Range(0,1)) = 1 _Shading_Mode("Shading mode", Range(0, 1)) = 0 _Mesh_Normals_Mode("Normals mode", Float) = 0.0 @@ -184,15 +223,35 @@ Shader "yum_food/tooner" #pragma shader_feature_local _ _RIM_LIGHTING1_MASK #pragma shader_feature_local _ _OKLAB #pragma shader_feature_local _ _CLONES - #pragma shader_feature_local _ _PBR_OVERLAY - #pragma shader_feature_local _ _PBR_OVERLAY_BASECOLOR_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_NORMAL_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_ROUGHNESS_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0 + #pragma shader_feature_local _ _PBR_OVERLAY0_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_MASK + #pragma shader_feature_local _ _PBR_OVERLAY1 + #pragma shader_feature_local _ _PBR_OVERLAY1_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_MASK + #pragma shader_feature_local _ _PBR_OVERLAY2 + #pragma shader_feature_local _ _PBR_OVERLAY2_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_MASK + #pragma shader_feature_local _ _PBR_OVERLAY3 + #pragma shader_feature_local _ _PBR_OVERLAY3_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_MASK #pragma shader_feature_local _ _LTCGI #pragma shader_feature_local _ _TESSELLATION #pragma shader_feature_local _ _MATCAP0_DISTORTION0 #pragma shader_feature_local _ _MATCAP1_DISTORTION0 + #pragma shader_feature_local _ _AMBIENT_OCCLUSION #pragma vertex vert //#pragma vertex hull_vertex @@ -248,15 +307,35 @@ Shader "yum_food/tooner" #pragma shader_feature_local _ _RIM_LIGHTING1_MASK #pragma shader_feature_local _ _OKLAB #pragma shader_feature_local _ _CLONES - #pragma shader_feature_local _ _PBR_OVERLAY - #pragma shader_feature_local _ _PBR_OVERLAY_BASECOLOR_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_NORMAL_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_ROUGHNESS_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0 + #pragma shader_feature_local _ _PBR_OVERLAY0_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_MASK + #pragma shader_feature_local _ _PBR_OVERLAY1 + #pragma shader_feature_local _ _PBR_OVERLAY1_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_MASK + #pragma shader_feature_local _ _PBR_OVERLAY2 + #pragma shader_feature_local _ _PBR_OVERLAY2_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_MASK + #pragma shader_feature_local _ _PBR_OVERLAY3 + #pragma shader_feature_local _ _PBR_OVERLAY3_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_MASK #pragma shader_feature_local _ _LTCGI #pragma shader_feature_local _ _TESSELLATION #pragma shader_feature_local _ _MATCAP0_DISTORTION0 #pragma shader_feature_local _ _MATCAP1_DISTORTION0 + #pragma shader_feature_local _ _AMBIENT_OCCLUSION #pragma vertex vert //#pragma vertex hull_vertex @@ -294,13 +373,33 @@ Shader "yum_food/tooner" #pragma shader_feature_local _ _RIM_LIGHTING1_MASK #pragma shader_feature_local _ _OKLAB #pragma shader_feature_local _ _CLONES - #pragma shader_feature_local _ _PBR_OVERLAY - #pragma shader_feature_local _ _PBR_OVERLAY_BASECOLOR_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_NORMAL_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_ROUGHNESS_MAP - #pragma shader_feature_local _ _PBR_OVERLAY_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0 + #pragma shader_feature_local _ _PBR_OVERLAY0_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY0_MASK + #pragma shader_feature_local _ _PBR_OVERLAY1 + #pragma shader_feature_local _ _PBR_OVERLAY1_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY1_MASK + #pragma shader_feature_local _ _PBR_OVERLAY2 + #pragma shader_feature_local _ _PBR_OVERLAY2_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY2_MASK + #pragma shader_feature_local _ _PBR_OVERLAY3 + #pragma shader_feature_local _ _PBR_OVERLAY3_BASECOLOR_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_NORMAL_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_ROUGHNESS_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_METALLIC_MAP + #pragma shader_feature_local _ _PBR_OVERLAY3_MASK #pragma shader_feature_local _ _LTCGI #pragma shader_feature_local _ _TESSELLATION + #pragma shader_feature_local _ _AMBIENT_OCCLUSION #pragma vertex vert //#pragma vertex hull_vertex diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc index baf3080..f62b8d2 100644 --- a/tooner_lighting.cginc +++ b/tooner_lighting.cginc @@ -154,11 +154,15 @@ tess_factors patch_constant(InputPatch patch) { tess_factors f; +#if defined(_TESSELLATION) float3 worldPos = mul(unity_ObjectToWorld, patch[0].position); float factor = _Tess_Factor; if (_Tess_Dist_Cutoff > 0 && length(_WorldSpaceCameraPos - worldPos) > _Tess_Dist_Cutoff) { factor = 1; } +#else + float factor = 1; +#endif f.edge[0] = factor; f.edge[1] = factor; @@ -387,6 +391,43 @@ float2 matcap_distortion0(float2 matcap_uv) { #define UV_SCOFF(uv, tex_st) (uv) * (tex_st).xy + (tex_st).zw +void getOverlayAlbedo(inout float4 ov_albedo, + inout float ov_mask, + v2f i, float iddx, float iddy) +{ +#if defined(_PBR_OVERLAY0) +#if defined(_PBR_OVERLAY0_BASECOLOR_MAP) + ov_albedo = _PBR_Overlay0_BaseColorTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay0_BaseColorTex_ST), iddx, iddy); + ov_albedo *= _PBR_Overlay0_BaseColor; +#else + ov_albedo = _PBR_Overlay0_BaseColor; +#endif // _PBR_OVERLAY0_BASECOLOR_MAP + +#if defined(_PBR_OVERLAY0_MASK) + ov_mask = _PBR_Overlay0_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); + ov_mask = ((bool) round(_PBR_Overlay0_Mask_Invert)) ? 1.0 - ov_mask : ov_mask; +#else + ov_mask = 1; +#endif + ov_albedo.a *= ov_mask; +#endif // _PBR_OVERLAY0 +} + +void applyOverlayNormal(inout float3 raw_normal, float ov_mask, v2f i, float iddx, float iddy) +{ +#if defined(_PBR_OVERLAY0) && defined(_PBR_OVERLAY0_NORMAL_MAP) + // Use UVs to smoothly blend between fully detailed normals when close up and + // flat normals when far away. If we don't do this, then we see moire effects + // on e.g. striped normal maps. + //float3 raw_normal = UnpackScaleNormal(_PBR_Overlay0_NormalTex.SampleGrad(linear_repeat_s, i.uv, iddx/2, iddy/2), _PBR_Overlay0_Tex_NormalStr); + float3 raw_normal_2 = UnpackScaleNormal(_PBR_Overlay0_NormalTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay0_NormalTex_ST), iddx/2, iddy/2), _PBR_Overlay0_Tex_NormalStr * ov_mask); + + raw_normal = BlendNormals( + raw_normal, + raw_normal_2); +#endif // _PBR_OVERLAY0 && _PBR_OVERLAY0_NORMAL_MAP +} + float4 effect(inout v2f i) { float iddx = ddx(i.uv.x) / 4; @@ -423,22 +464,9 @@ float4 effect(inout v2f i) albedo.a = 1; #endif -#if defined(_PBR_OVERLAY) -#if defined(_PBR_OVERLAY_BASECOLOR_MAP) - float4 ov_albedo = _PBR_Overlay_BaseColorTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay_BaseColorTex_ST), iddx, iddy); - ov_albedo *= _PBR_Overlay_BaseColor; -#else - float4 ov_albedo = _BaseColor; -#endif // _PBR_OVERLAY_BASECOLOR_MAP - -#if defined(_PBR_OVERLAY_MASK) - float ov_mask = _PBR_Overlay_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); -#else - float ov_mask = 1; -#endif - ov_albedo.a *= ov_mask; - -#endif // _PBR_OVERLAY + float4 ov_albedo = 0; + float ov_mask = 0; + getOverlayAlbedo(ov_albedo, ov_mask, i, iddx, iddy); #if defined(_NORMAL_MAP) // Use UVs to smoothly blend between fully detailed normals when close up and @@ -446,25 +474,15 @@ float4 effect(inout v2f i) // on e.g. striped normal maps. float fw = clamp(fwidth(i.uv), .001, 1) * 1200; float3 raw_normal = UnpackScaleNormal(_NormalTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _NormalTex_ST), iddx/2, iddy/2), _Tex_NormalStr); - raw_normal = normalize(raw_normal); raw_normal = BlendNormals( (1/fw) * raw_normal, fw * float3(0, 0, 1)); +#else + float3 raw_normal = UnpackNormal(float4(0.5, 0.5, 1, 1)); +#endif // _NORMAL_MAP -#if defined(_PBR_OVERLAY) && defined(_PBR_OVERLAY_NORMAL_MAP) - { - // Use UVs to smoothly blend between fully detailed normals when close up and - // flat normals when far away. If we don't do this, then we see moire effects - // on e.g. striped normal maps. - //float3 raw_normal = UnpackScaleNormal(_PBR_Overlay_NormalTex.SampleGrad(linear_repeat_s, i.uv, iddx/2, iddy/2), _PBR_Overlay_Tex_NormalStr); - float3 raw_normal_2 = UnpackScaleNormal(_PBR_Overlay_NormalTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay_NormalTex_ST), iddx/2, iddy/2), _PBR_Overlay_Tex_NormalStr * ov_albedo.a); - - raw_normal = BlendNormals( - raw_normal, - raw_normal_2); - } -#endif // _PBR_OVERLAY && _PBR_OVERLAY_NORMAL_MAP + applyOverlayNormal(raw_normal, ov_mask, i, iddx, iddy); float3 binormal = CreateBinormal(i.normal, i.tangent.xyz, i.tangent.w); float3 normal = normalize( @@ -472,9 +490,6 @@ float4 effect(inout v2f i) raw_normal.y * binormal + raw_normal.z * i.normal ); -#else // !_NORMAL_MAP - float3 normal = i.normal; -#endif // _NORMAL_MAP #if defined(_METALLIC_MAP) float metallic = _MetallicTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _MetallicTex_ST), iddx, iddy); @@ -492,9 +507,9 @@ float4 effect(inout v2f i) float4 vertex_light_color = 0; #endif -#if defined(_PBR_OVERLAY) +#if defined(_PBR_OVERLAY0) albedo.rgb = lerp(albedo.rgb, ov_albedo.rgb, ov_albedo.a); -#endif // _PBR_OVERLAY +#endif // _PBR_OVERLAY0 #if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) float3 matcap_emission = 0; @@ -725,8 +740,15 @@ float4 effect(inout v2f i) } #endif +#if defined(_AMBIENT_OCCLUSION) + float ao = _Ambient_Occlusion.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); + ao = 1 - (1 - ao) * _Ambient_Occlusion_Strength; +#else + float ao = 1; +#endif + float4 lit = getLitColor(vertex_light_color, albedo, i.worldPos, normal, - metallic, 1.0 - roughness, i.uv, i); + metallic, 1.0 - roughness, i.uv, ao, i); float4 result = lit; #if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1) diff --git a/tooner_outline_pass.cginc b/tooner_outline_pass.cginc index 35e22b5..1f60bdf 100644 --- a/tooner_outline_pass.cginc +++ b/tooner_outline_pass.cginc @@ -25,6 +25,7 @@ struct tess_factors { v2f vert(appdata v) { +#if defined(_OUTLINES) float outline_mask = _Outline_Mask.SampleLevel(linear_repeat_s, v.uv0.xy, /*lod=*/1); outline_mask = _Outline_Mask_Invert > 1E-6 ? 1 - outline_mask : outline_mask; @@ -70,10 +71,23 @@ v2f vert(appdata v) #endif return o; +#else + v2f o; + o.worldPos = 0; + o.objPos = 0; + o.clipPos = 0; + o.normal = 0; + o.uv = 0; +#if defined(LIGHTMAP_ON) + o.lmuv = 0; +#endif + return o; +#endif // _OUTLINES } tess_data hull_vertex(appdata v) { +#if defined(_OUTLINES) float outline_mask = _Outline_Mask.SampleLevel(linear_repeat_s, v.uv0.xy, /*lod=*/3); outline_mask = _Outline_Mask_Invert > 1E-6 ? 1 - outline_mask : outline_mask; @@ -102,17 +116,22 @@ tess_data hull_vertex(appdata v) o.uv = v.uv0.xy; return o; +#endif // _OUTLINES } tess_factors patch_constant(InputPatch patch) { tess_factors f; +#if defined(_TESSELLATION) float3 worldPos = mul(unity_ObjectToWorld, patch[0].vertex); float factor = _Tess_Factor; if (_Tess_Dist_Cutoff > 0 && length(_WorldSpaceCameraPos - worldPos) > _Tess_Dist_Cutoff) { factor = 1; } +#else + float factor = 1; +#endif f.edge[0] = factor; f.edge[1] = factor; @@ -302,6 +321,7 @@ void geom(triangle v2f tri_in[3], fixed4 frag (v2f i) : SV_Target { +#if defined(_OUTLINES) float iddx = ddx(i.uv.x) / 4; float iddy = ddx(i.uv.y) / 4; #if defined(_BASECOLOR_MAP) @@ -321,9 +341,10 @@ fixed4 frag (v2f i) : SV_Target //float3 flat_normal = normalize(UnpackNormal(float4(128, 128, 255, 255)/255)); float3 flat_normal = normalize(_WorldSpaceCameraPos - i.worldPos); float4 vertex_light_color = 0; + float ao = 1; float4 result = getLitColor( vertex_light_color, - _Outline_Color, i.worldPos, flat_normal, 0, 0, i.uv, i); + _Outline_Color, i.worldPos, flat_normal, 0, 0, i.uv, ao, i); result += _Outline_Color * _Outline_Emission_Strength; @@ -338,6 +359,10 @@ fixed4 frag (v2f i) : SV_Target #endif return result; +#else + discard; + return 0; +#endif // _OUTLINES } #endif // __TOONER_OUTLINE_PASS -- cgit v1.2.3