From 66e3e3b84ca2736fcfb4d3782e21df9c04bc8c67 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 14 Jun 2021 20:05:12 +0900 Subject: [PATCH] refactoring about alpha --- .../VRM10/vrmc_materials_mtoon_define.hlsl | 20 +++++++++++++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 13 ++------- .../vrmc_materials_mtoon_geometry_alpha.hlsl | 29 +++++++++++++++++++ ...c_materials_mtoon_geometry_alpha.hlsl.meta | 3 ++ 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index faf6f021a0..cb983736f7 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -41,6 +41,26 @@ inline bool MToon_IsOutlinePass() #endif } +// Compile-time constant +inline bool MToon_IsAlphaTestOn() +{ +#if defined(_ALPHATEST_ON) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsAlphaBlendOn() +{ +#if defined(_ALPHABLEND_ON) + return true; +#else + return false; +#endif +} + // Compile-time constant inline bool MToon_IsNormalMapOn() { diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl index 9064d1f315..0e08f4b502 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl @@ -7,6 +7,7 @@ #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" #include "./vrmc_materials_mtoon_geometry_uv.hlsl" +#include "./vrmc_materials_mtoon_geometry_alpha.hlsl" #include "./vrmc_materials_mtoon_geometry_normal.hlsl" #include "./vrmc_materials_mtoon_lighting_unity.hlsl" #include "./vrmc_materials_mtoon_lighting_mtoon.hlsl" @@ -27,17 +28,7 @@ half4 MToonFragment(const FragmentInput fragmentInput) : SV_Target const half4 litColor = UNITY_SAMPLE_TEX2D(_MainTex, uv) * _Color; // Alpha Test -#if defined(_ALPHATEST_ON) - const half rawAlpha = litColor.a; - const half tmpAlpha = (rawAlpha - _Cutoff) / max(fwidth(rawAlpha), 0.00001) + 0.5; // Alpha to Coverage - clip(tmpAlpha - _Cutoff); - const half alpha = 1.0; -#elif defined(_ALPHABLEND_ON) - const half alpha = litColor.a; - clip(alpha - EPS_COL); -#else - const half alpha = 1.0; -#endif + const half alpha = GetMToonGeometry_Alpha(litColor); // Get Normal const float3 normalWS = GetMToonGeometry_Normal(input, fragmentInput.facing, uv); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl new file mode 100644 index 0000000000..99ac426781 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl @@ -0,0 +1,29 @@ +#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_ALPHA_INCLUDED +#define VRMC_MATERIALS_MTOON_GEOMETRY_ALPHA_INCLUDED + +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" + +inline half GetMToonGeometry_Alpha(half4 litColor) +{ + if (MToon_IsAlphaTestOn()) + { + const half rawAlpha = litColor.a; + const half tmpAlpha = (rawAlpha - _Cutoff) / max(fwidth(rawAlpha), 0.00001) + 0.5; // Alpha to Coverage + clip(tmpAlpha - _Cutoff); + return 1.0; + } + else if (MToon_IsAlphaBlendOn()) + { + const half alpha = litColor.a; + clip(alpha - EPS_COL); + return alpha; + } + else + { + return 1.0; + } +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl.meta new file mode 100644 index 0000000000..4fc9913aff --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_alpha.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 87aa2d8d38194c35a6531b8f5e3da7aa +timeCreated: 1623668534 \ No newline at end of file