diff options
| author | yum <yum.food.vr@gmail.com> | 2024-08-02 17:03:03 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2024-08-02 17:03:03 -0700 |
| commit | 674df2324fa891e23e982a95e46d8fda3378c0c3 (patch) | |
| tree | 6197a6497b98e181c5051ac172dbfc5d9e64f128 | |
| parent | fa44a3879806176d48d0c16a960a2c4fe6198dd6 (diff) | |
Implement overlay roughness
| -rw-r--r-- | Editor/tooner.cs | 30 | ||||
| -rw-r--r-- | feature_macros.cginc | 4 | ||||
| -rw-r--r-- | globals.cginc | 24 | ||||
| -rw-r--r-- | tooner.shader | 4 | ||||
| -rw-r--r-- | tooner_lighting.cginc | 68 |
5 files changed, 109 insertions, 21 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs index 9efdd21..166241d 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -242,16 +242,28 @@ public class ToonerGUI : ShaderGUI { } SetKeyword($"_PBR_OVERLAY{i}_METALLIC_MAP", 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); + bc = FindProperty($"_PBR_Overlay{i}_Roughness_Enable"); + enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + enabled = EditorGUILayout.Toggle("Enable roughness", enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = enabled ? 1.0f : 0.0f; + SetKeyword($"_PBR_OVERLAY{i}_ROUGHNESS", enabled); + + if (enabled) { + EditorGUI.indentLevel += 1; + 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); + EditorGUI.indentLevel -= 1; } - SetKeyword($"_PBR_OVERLAY{i}_ROUGHNESS_MAP", bct.textureValue); bct = FindProperty($"_PBR_Overlay{i}_Mask"); editor.TexturePropertySingleLine( diff --git a/feature_macros.cginc b/feature_macros.cginc index f252524..07b0a3c 100644 --- a/feature_macros.cginc +++ b/feature_macros.cginc @@ -37,6 +37,7 @@ #pragma shader_feature_local _ _PBR_OVERLAY0_BASECOLOR_MAP #pragma shader_feature_local _ _PBR_OVERLAY0_EMISSION_MAP #pragma shader_feature_local _ _PBR_OVERLAY0_NORMAL_MAP +#pragma shader_feature_local _ _PBR_OVERLAY0_ROUGHNESS #pragma shader_feature_local _ _PBR_OVERLAY0_ROUGHNESS_MAP #pragma shader_feature_local _ _PBR_OVERLAY0_METALLIC_MAP #pragma shader_feature_local _ _PBR_OVERLAY0_MASK @@ -48,6 +49,7 @@ #pragma shader_feature_local _ _PBR_OVERLAY1_BASECOLOR_MAP #pragma shader_feature_local _ _PBR_OVERLAY1_EMISSION_MAP #pragma shader_feature_local _ _PBR_OVERLAY1_NORMAL_MAP +#pragma shader_feature_local _ _PBR_OVERLAY1_ROUGHNESS #pragma shader_feature_local _ _PBR_OVERLAY1_ROUGHNESS_MAP #pragma shader_feature_local _ _PBR_OVERLAY1_METALLIC_MAP #pragma shader_feature_local _ _PBR_OVERLAY1_MASK @@ -59,6 +61,7 @@ #pragma shader_feature_local _ _PBR_OVERLAY2_BASECOLOR_MAP #pragma shader_feature_local _ _PBR_OVERLAY2_EMISSION_MAP #pragma shader_feature_local _ _PBR_OVERLAY2_NORMAL_MAP +#pragma shader_feature_local _ _PBR_OVERLAY2_ROUGHNESS #pragma shader_feature_local _ _PBR_OVERLAY2_ROUGHNESS_MAP #pragma shader_feature_local _ _PBR_OVERLAY2_METALLIC_MAP #pragma shader_feature_local _ _PBR_OVERLAY2_MASK @@ -70,6 +73,7 @@ #pragma shader_feature_local _ _PBR_OVERLAY3_BASECOLOR_MAP #pragma shader_feature_local _ _PBR_OVERLAY3_EMISSION_MAP #pragma shader_feature_local _ _PBR_OVERLAY3_NORMAL_MAP +#pragma shader_feature_local _ _PBR_OVERLAY3_ROUGHNESS #pragma shader_feature_local _ _PBR_OVERLAY3_ROUGHNESS_MAP #pragma shader_feature_local _ _PBR_OVERLAY3_METALLIC_MAP #pragma shader_feature_local _ _PBR_OVERLAY3_MASK diff --git a/globals.cginc b/globals.cginc index 5cb77e9..677cc8a 100644 --- a/globals.cginc +++ b/globals.cginc @@ -84,7 +84,11 @@ float4 _RoughnessTex_ST; #if defined(_PBR_OVERLAY0) float4 _PBR_Overlay0_BaseColor; float _PBR_Overlay0_Metallic; +#if defined(_PBR_OVERLAY0_ROUGHNESS) float _PBR_Overlay0_Roughness; +texture2D _PBR_Overlay0_RoughnessTex; +float4 _PBR_Overlay0_RoughnessTex_ST; +#endif texture2D _PBR_Overlay0_BaseColorTex; float4 _PBR_Overlay0_BaseColorTex_ST; float4 _PBR_Overlay0_Emission; @@ -94,8 +98,6 @@ 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; @@ -108,7 +110,11 @@ float _PBR_Overlay0_Alpha_Multiplier; #if defined(_PBR_OVERLAY1) float4 _PBR_Overlay1_BaseColor; float _PBR_Overlay1_Metallic; +#if defined(_PBR_OVERLAY1_ROUGHNESS) float _PBR_Overlay1_Roughness; +texture2D _PBR_Overlay1_RoughnessTex; +float4 _PBR_Overlay1_RoughnessTex_ST; +#endif texture2D _PBR_Overlay1_BaseColorTex; float4 _PBR_Overlay1_BaseColorTex_ST; float4 _PBR_Overlay1_Emission; @@ -118,8 +124,6 @@ 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; @@ -132,7 +136,11 @@ float _PBR_Overlay1_Alpha_Multiplier; #if defined(_PBR_OVERLAY2) float4 _PBR_Overlay2_BaseColor; float _PBR_Overlay2_Metallic; +#if defined(_PBR_OVERLAY2_ROUGHNESS) float _PBR_Overlay2_Roughness; +texture2D _PBR_Overlay2_RoughnessTex; +float4 _PBR_Overlay2_RoughnessTex_ST; +#endif texture2D _PBR_Overlay2_BaseColorTex; float4 _PBR_Overlay2_BaseColorTex_ST; float4 _PBR_Overlay2_Emission; @@ -142,8 +150,6 @@ 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; @@ -156,7 +162,11 @@ float _PBR_Overlay2_Alpha_Multiplier; #if defined(_PBR_OVERLAY3) float4 _PBR_Overlay3_BaseColor; float _PBR_Overlay3_Metallic; +#if defined(_PBR_OVERLAY3_ROUGHNESS) float _PBR_Overlay3_Roughness; +texture2D _PBR_Overlay3_RoughnessTex; +float4 _PBR_Overlay3_RoughnessTex_ST; +#endif texture2D _PBR_Overlay3_BaseColorTex; float4 _PBR_Overlay3_BaseColorTex_ST; float4 _PBR_Overlay3_Emission; @@ -166,8 +176,6 @@ 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; diff --git a/tooner.shader b/tooner.shader index 5ae08ed..ec515b3 100644 --- a/tooner.shader +++ b/tooner.shader @@ -22,6 +22,7 @@ Shader "yum_food/tooner" _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_Enable("Roughness enable", Float) = 0 _PBR_Overlay0_Roughness("Roughness", Range(0, 1)) = 1 _PBR_Overlay0_BaseColorTex("Base color", 2D) = "white" {} _PBR_Overlay0_Emission("Emission", Color) = (1, 1, 1, 1) @@ -41,6 +42,7 @@ Shader "yum_food/tooner" _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_Enable("Roughness enable", Float) = 0 _PBR_Overlay1_Roughness("Roughness", Range(0, 1)) = 1 _PBR_Overlay1_BaseColorTex("Base color", 2D) = "white" {} _PBR_Overlay1_Emission("Emission", Color) = (1, 1, 1, 1) @@ -60,6 +62,7 @@ Shader "yum_food/tooner" _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_Enable("Roughness enable", Float) = 0 _PBR_Overlay2_Roughness("Roughness", Range(0, 1)) = 1 _PBR_Overlay2_BaseColorTex("Base color", 2D) = "white" {} _PBR_Overlay2_Emission("Emission", Color) = (1, 1, 1, 1) @@ -79,6 +82,7 @@ Shader "yum_food/tooner" _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_Enable("Roughness enable", Float) = 0 _PBR_Overlay3_Roughness("Roughness", Range(0, 1)) = 1 _PBR_Overlay3_BaseColorTex("Base color", 2D) = "white" {} _PBR_Overlay3_Emission("Emission", Color) = (1, 1, 1, 1) diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc index 8ea356c..00594ef 100644 --- a/tooner_lighting.cginc +++ b/tooner_lighting.cginc @@ -525,23 +525,35 @@ float2 matcap_distortion0(float2 matcap_uv) { struct PbrOverlay { #if defined(_PBR_OVERLAY0) float4 ov0_albedo; +#if defined(_PBR_OVERLAY0_ROUGHNESS) + float ov0_roughness; +#endif float ov0_mask; #endif #if defined(_PBR_OVERLAY1) float4 ov1_albedo; +#if defined(_PBR_OVERLAY1_ROUGHNESS) + float ov1_roughness; +#endif float ov1_mask; #endif #if defined(_PBR_OVERLAY2) float4 ov2_albedo; +#if defined(_PBR_OVERLAY2_ROUGHNESS) + float ov2_roughness; +#endif float ov2_mask; #endif #if defined(_PBR_OVERLAY3) float4 ov3_albedo; +#if defined(_PBR_OVERLAY3_ROUGHNESS) + float ov3_roughness; +#endif float ov3_mask; #endif }; -void getOverlayAlbedo(inout PbrOverlay ov, +void getOverlayAlbedoRoughness(inout PbrOverlay ov, v2f i, float iddx, float iddy) { #if defined(_PBR_OVERLAY0) @@ -552,6 +564,15 @@ void getOverlayAlbedo(inout PbrOverlay ov, ov.ov0_albedo = _PBR_Overlay0_BaseColor; #endif // _PBR_OVERLAY0_BASECOLOR_MAP +#if defined(_PBR_OVERLAY0_ROUGHNESS) +#if defined(_PBR_OVERLAY0_ROUGHNESS_MAP) + ov.ov0_roughness = _PBR_Overlay0_RoughnessTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay0_RoughnessTex_ST), iddx * _PBR_Overlay0_RoughnessTex_ST.x, iddy * _PBR_Overlay0_RoughnessTex_ST.y); + ov.ov0_roughness *= _PBR_Overlay0_Roughness; +#else + ov.ov0_roughness = _PBR_Overlay0_Roughness; +#endif // _PBR_OVERLAY0_ROUGHNESS_MAP +#endif + #if defined(_PBR_OVERLAY0_MASK) ov.ov0_mask = _PBR_Overlay0_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); ov.ov0_mask = ((bool) round(_PBR_Overlay0_Mask_Invert)) ? 1.0 - ov.ov0_mask : ov.ov0_mask; @@ -569,6 +590,15 @@ void getOverlayAlbedo(inout PbrOverlay ov, ov.ov1_albedo = _PBR_Overlay1_BaseColor; #endif // _PBR_OVERLAY1_BASECOLOR_MAP +#if defined(_PBR_OVERLAY1_ROUGHNESS) +#if defined(_PBR_OVERLAY1_ROUGHNESS_MAP) + ov.ov0_roughness = _PBR_Overlay1_RoughnessTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay1_RoughnessTex_ST), iddx * _PBR_Overlay1_RoughnessTex_ST.x, iddy * _PBR_Overlay1_RoughnessTex_ST.y); + ov.ov0_roughness *= _PBR_Overlay1_Roughness; +#else + ov.ov0_roughness = _PBR_Overlay1_Roughness; +#endif // _PBR_OVERLAY1_ROUGHNESS_MAP +#endif + #if defined(_PBR_OVERLAY1_MASK) ov.ov1_mask = _PBR_Overlay1_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); ov.ov1_mask = ((bool) round(_PBR_Overlay1_Mask_Invert)) ? 1.0 - ov.ov1_mask : ov.ov1_mask; @@ -586,6 +616,15 @@ void getOverlayAlbedo(inout PbrOverlay ov, ov.ov2_albedo = _PBR_Overlay2_BaseColor; #endif // _PBR_OVERLAY2_BASECOLOR_MAP +#if defined(_PBR_OVERLAY2_ROUGHNESS) +#if defined(_PBR_OVERLAY2_ROUGHNESS_MAP) + ov.ov0_roughness = _PBR_Overlay2_RoughnessTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay2_RoughnessTex_ST), iddx * _PBR_Overlay2_RoughnessTex_ST.x, iddy * _PBR_Overlay2_RoughnessTex_ST.y); + ov.ov0_roughness *= _PBR_Overlay2_Roughness; +#else + ov.ov0_roughness = _PBR_Overlay2_Roughness; +#endif // _PBR_OVERLAY2_ROUGHNESS_MAP +#endif + #if defined(_PBR_OVERLAY2_MASK) ov.ov2_mask = _PBR_Overlay2_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); ov.ov2_mask = ((bool) round(_PBR_Overlay2_Mask_Invert)) ? 1.0 - ov.ov2_mask : ov.ov2_mask; @@ -603,6 +642,15 @@ void getOverlayAlbedo(inout PbrOverlay ov, ov.ov3_albedo = _PBR_Overlay3_BaseColor; #endif // _PBR_OVERLAY3_BASECOLOR_MAP +#if defined(_PBR_OVERLAY3_ROUGHNESS) +#if defined(_PBR_OVERLAY3_ROUGHNESS_MAP) + ov.ov0_roughness = _PBR_Overlay3_RoughnessTex.SampleGrad(linear_repeat_s, UV_SCOFF(i.uv, _PBR_Overlay3_RoughnessTex_ST), iddx * _PBR_Overlay3_RoughnessTex_ST.x, iddy * _PBR_Overlay3_RoughnessTex_ST.y); + ov.ov0_roughness *= _PBR_Overlay3_Roughness; +#else + ov.ov0_roughness = _PBR_Overlay3_Roughness; +#endif // _PBR_OVERLAY3_ROUGHNESS_MAP +#endif + #if defined(_PBR_OVERLAY3_MASK) ov.ov3_mask = _PBR_Overlay3_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy); ov.ov3_mask = ((bool) round(_PBR_Overlay3_Mask_Invert)) ? 1.0 - ov.ov3_mask : ov.ov3_mask; @@ -683,7 +731,7 @@ void applyDecalAlbedo(inout float4 albedo, #endif // _DECAL3 } -void mixOverlayAlbedo(inout float4 albedo, PbrOverlay ov) { +void mixOverlayAlbedoRoughness(inout float4 albedo, inout float roughness, PbrOverlay ov) { // Calculate alpha masks before we start mutating alpha. #if defined(_PBR_OVERLAY0) float a0 = saturate(ov.ov0_albedo.a * _PBR_Overlay0_Alpha_Multiplier); @@ -721,6 +769,9 @@ void mixOverlayAlbedo(inout float4 albedo, PbrOverlay ov) { #if defined(_PBR_OVERLAY0) #if defined(_PBR_OVERLAY0_MIX_ALPHA_BLEND) albedo.rgb = lerp(albedo.rgb, ov.ov0_albedo.rgb, a0); +#if defined(_PBR_OVERLAY0_ROUGHNESS) + roughness = lerp(roughness, ov.ov0_roughness, a0); +#endif albedo.a = max(albedo.a, a0); #elif defined(_PBR_OVERLAY0_MIX_ADD) albedo.rgb += ov.ov0_albedo; @@ -734,6 +785,9 @@ void mixOverlayAlbedo(inout float4 albedo, PbrOverlay ov) { #if defined(_PBR_OVERLAY1) #if defined(_PBR_OVERLAY1_MIX_ALPHA_BLEND) albedo.rgb = lerp(albedo.rgb, ov.ov1_albedo.rgb, a1); +#if defined(_PBR_OVERLAY0_ROUGHNESS) + roughness = lerp(roughness, ov.ov1_roughness, a1); +#endif albedo.a = max(albedo.a, a1); #elif defined(_PBR_OVERLAY1_MIX_ADD) albedo.rgb += ov.ov1_albedo; @@ -747,6 +801,9 @@ void mixOverlayAlbedo(inout float4 albedo, PbrOverlay ov) { #if defined(_PBR_OVERLAY2) #if defined(_PBR_OVERLAY2_MIX_ALPHA_BLEND) albedo.rgb = lerp(albedo.rgb, ov.ov2_albedo.rgb, ov.ov2_albedo.a); +#if defined(_PBR_OVERLAY0_ROUGHNESS) + roughness = lerp(roughness, ov.ov2_roughness, a2); +#endif albedo.a = max(albedo.a, a2); #elif defined(_PBR_OVERLAY2_MIX_ADD) albedo.rgb += ov.ov2_albedo; @@ -760,6 +817,9 @@ void mixOverlayAlbedo(inout float4 albedo, PbrOverlay ov) { #if defined(_PBR_OVERLAY3) #if defined(_PBR_OVERLAY3_MIX_ALPHA_BLEND) albedo.rgb = lerp(albedo.rgb, ov.ov3_albedo.rgb, a3); +#if defined(_PBR_OVERLAY0_ROUGHNESS) + roughness = lerp(roughness, ov.ov3_roughness, a3); +#endif albedo.a = max(albedo.a, a3); #elif defined(_PBR_OVERLAY3_MIX_ADD) albedo.rgb += ov.ov3_albedo; @@ -966,7 +1026,7 @@ float4 effect(inout v2f i) #endif PbrOverlay ov; - getOverlayAlbedo(ov, i, iddx, iddy); + getOverlayAlbedoRoughness(ov, i, iddx, iddy); #if defined(_NORMAL_MAP) // Use UVs to smoothly blend between fully detailed normals when close up and @@ -1023,7 +1083,7 @@ float4 effect(inout v2f i) } #endif - mixOverlayAlbedo(albedo, ov); + mixOverlayAlbedoRoughness(albedo, roughness, ov); #if defined(_DECAL0) || defined(_DECAL1) || defined(_DECAL2) || defined(_DECAL3) float decal_emission = 0; applyDecalAlbedo(albedo, decal_emission, i); |
