From c90f76374bad23a7a82302c09a52384b1f916f1e Mon Sep 17 00:00:00 2001 From: Andreas Atteneder Date: Mon, 20 Dec 2021 11:01:10 +0100 Subject: [PATCH] refactor: Moved HDRP specifics into dedicated file. --- .../HighDefinitionRPMaterialGenerator.cs | 69 +++++++++++++++++++ .../Material/ShaderGraphMaterialGenerator.cs | 64 +++++------------ 2 files changed, 85 insertions(+), 48 deletions(-) diff --git a/Runtime/Scripts/Material/HighDefinitionRPMaterialGenerator.cs b/Runtime/Scripts/Material/HighDefinitionRPMaterialGenerator.cs index 211a9f17..c68e6a7b 100644 --- a/Runtime/Scripts/Material/HighDefinitionRPMaterialGenerator.cs +++ b/Runtime/Scripts/Material/HighDefinitionRPMaterialGenerator.cs @@ -17,11 +17,80 @@ using System; using UnityEngine; +using UnityEngine.Rendering; namespace GLTFast.Materials { public class HighDefinitionRPMaterialGenerator : ShaderGraphMaterialGenerator { + static readonly int k_AlphaCutoffEnable = Shader.PropertyToID("_AlphaCutoffEnable"); + static readonly int k_ZTestGBufferPropId = Shader.PropertyToID("_ZTestGBuffer"); + +#if USING_HDRP_10_OR_NEWER + // const string KW_DISABLE_DECALS = "_DISABLE_DECALS"; + const string KW_DISABLE_SSR_TRANSPARENT = "_DISABLE_SSR_TRANSPARENT"; + const string KW_DOUBLESIDED_ON = "_DOUBLESIDED_ON"; + const string KW_ENABLE_FOG_ON_TRANSPARENT = "_ENABLE_FOG_ON_TRANSPARENT"; + const string KW_SURFACE_TYPE_TRANSPARENT = "_SURFACE_TYPE_TRANSPARENT"; + + const string k_ShaderPassTransparentDepthPrepass = "TransparentDepthPrepass"; + const string k_ShaderPassTransparentDepthPostpass = "TransparentDepthPostpass"; + const string k_ShaderPassTransparentBackface = "TransparentBackface"; + const string k_ShaderPassRayTracingPrepass = "RayTracingPrepass"; + + static readonly int k_DoubleSidedEnablePropId = Shader.PropertyToID("_DoubleSidedEnable"); + static readonly int k_DoubleSidedNormalModePropId = Shader.PropertyToID("_DoubleSidedNormalMode"); + static readonly int k_DoubleSidedConstantsPropId = Shader.PropertyToID("_DoubleSidedConstants"); + static readonly int k_AlphaDstBlendPropId = Shader.PropertyToID("_AlphaDstBlend"); + static readonly int k_CullModeForwardPropId = Shader.PropertyToID("_CullModeForward"); +#endif + +#if USING_HDRP_10_OR_NEWER + protected override void SetDoubleSided(Schema.Material gltfMaterial, Material material) { + base.SetDoubleSided(gltfMaterial,material); + + material.EnableKeyword(KW_DOUBLESIDED_ON); + material.SetFloat(k_DoubleSidedEnablePropId, 1); + + // UnityEditor.Rendering.HighDefinition.DoubleSidedNormalMode.Flip + material.SetFloat(k_DoubleSidedNormalModePropId, 0); + material.SetVector(k_DoubleSidedConstantsPropId, new Vector4(-1,-1,-1,0)); + + material.SetFloat("_CullMode", (int)CullMode.Off); + } +#endif + + protected override void SetAlphaModeMask(Schema.Material gltfMaterial, Material material) { + base.SetAlphaModeMask(gltfMaterial,material); + material.EnableKeyword(KW_ALPHATEST_ON); + material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_CUTOUT); + material.SetFloat(k_AlphaCutoffEnable, 1); + material.renderQueue = (int) RenderQueue.AlphaTest; + material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3 + material.SetOverrideTag("MotionVector","User"); + material.SetShaderPassEnabled("MOTIONVECTORS",false); + } + +#if USING_HDRP_10_OR_NEWER + protected override void SetShaderModeBlend(Schema.Material gltfMaterial, Material material) { + material.EnableKeyword(KW_ALPHATEST_ON); + material.EnableKeyword(KW_SURFACE_TYPE_TRANSPARENT); + // material.EnableKeyword(KW_DISABLE_DECALS); + material.EnableKeyword(KW_DISABLE_SSR_TRANSPARENT); + material.EnableKeyword(KW_ENABLE_FOG_ON_TRANSPARENT); + material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_TRANSPARENT); + material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPrepass, false); + material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPostpass, false); + material.SetShaderPassEnabled(k_ShaderPassTransparentBackface, false); + material.SetShaderPassEnabled(k_ShaderPassRayTracingPrepass, false); + material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3 + material.SetFloat(k_AlphaDstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);//10 + material.SetFloat(dstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);//10 + material.SetFloat(srcBlendPropId, (int) BlendMode.SrcAlpha);//5 + material.SetFloat(cullModePropId, (int)CullMode.Off); + material.SetFloat(k_CullModeForwardPropId, (int)CullMode.Off); + } +#endif } } #endif // USING_URP diff --git a/Runtime/Scripts/Material/ShaderGraphMaterialGenerator.cs b/Runtime/Scripts/Material/ShaderGraphMaterialGenerator.cs index 1e6200d0..0a2f414b 100644 --- a/Runtime/Scripts/Material/ShaderGraphMaterialGenerator.cs +++ b/Runtime/Scripts/Material/ShaderGraphMaterialGenerator.cs @@ -92,26 +92,6 @@ protected enum UnlitShaderFeatures { protected static readonly int transmissionTextureScaleTransformPropId = Shader.PropertyToID("_TransmittanceColorMap_ST"); protected static readonly int transmissionTextureRotationPropId = Shader.PropertyToID("_TransmittanceColorMapRotation"); protected static readonly int transmissionTextureUVChannelPropId = Shader.PropertyToID("_TransmittanceColorMapUVChannel"); - -#if USING_HDRP_10_OR_NEWER - // const string KW_DISABLE_DECALS = "_DISABLE_DECALS"; - const string KW_DISABLE_SSR_TRANSPARENT = "_DISABLE_SSR_TRANSPARENT"; - const string KW_DOUBLESIDED_ON = "_DOUBLESIDED_ON"; - const string KW_ENABLE_FOG_ON_TRANSPARENT = "_ENABLE_FOG_ON_TRANSPARENT"; - const string KW_SURFACE_TYPE_TRANSPARENT = "_SURFACE_TYPE_TRANSPARENT"; - - const string k_ShaderPassTransparentDepthPrepass = "TransparentDepthPrepass"; - const string k_ShaderPassTransparentDepthPostpass = "TransparentDepthPostpass"; - const string k_ShaderPassTransparentBackface = "TransparentBackface"; - const string k_ShaderPassRayTracingPrepass = "RayTracingPrepass"; - - static readonly int k_DoubleSidedEnablePropId = Shader.PropertyToID("_DoubleSidedEnable"); - static readonly int k_DoubleSidedNormalModePropId = Shader.PropertyToID("_DoubleSidedNormalMode"); - static readonly int k_DoubleSidedConstantsPropId = Shader.PropertyToID("_DoubleSidedConstants"); - static readonly int k_ZTestGBufferPropId = Shader.PropertyToID("_ZTestGBuffer"); - static readonly int k_AlphaDstBlendPropId = Shader.PropertyToID("_AlphaDstBlend"); - static readonly int k_CullModeForwardPropId = Shader.PropertyToID("_CullModeForward"); -#endif static Dictionary metallicShaders = new Dictionary(); static Dictionary specularShaders = new Dictionary(); @@ -367,7 +347,7 @@ public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfRea } if (gltfMaterial.alphaModeEnum == AlphaMode.MASK) { - material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); + SetAlphaModeMask(gltfMaterial, material); } else { material.SetFloat(cutoffPropId, 0); // double sided opaque would make errors in HDRP 7.3 otherwise @@ -386,43 +366,21 @@ public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfRea material.renderQueue = (int) renderQueue.Value; -#if USING_HDRP_10_OR_NEWER if (gltfMaterial.doubleSided) { - material.EnableKeyword(KW_DOUBLESIDED_ON); - material.SetFloat(k_DoubleSidedEnablePropId, 1); - - // UnityEditor.Rendering.HighDefinition.DoubleSidedNormalMode.Flip - material.SetFloat(k_DoubleSidedNormalModePropId, 0); - material.SetVector(k_DoubleSidedConstantsPropId, new Vector4(-1,-1,-1,0)); - - material.SetFloat("_CullMode", (int)CullMode.Off); + SetDoubleSided(gltfMaterial, material); } - + switch (shaderMode) { case ShaderMode.Opaque: + SetShaderModeOpaque(gltfMaterial, material); break; case ShaderMode.Blend: - material.EnableKeyword(KW_ALPHATEST_ON); - material.EnableKeyword(KW_SURFACE_TYPE_TRANSPARENT); - // material.EnableKeyword(KW_DISABLE_DECALS); - material.EnableKeyword(KW_DISABLE_SSR_TRANSPARENT); - material.EnableKeyword(KW_ENABLE_FOG_ON_TRANSPARENT); - material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_TRANSPARENT); - material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPrepass, false); - material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPostpass, false); - material.SetShaderPassEnabled(k_ShaderPassTransparentBackface, false); - material.SetShaderPassEnabled(k_ShaderPassRayTracingPrepass, false); - material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3 - material.SetFloat(k_AlphaDstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);//10 - material.SetFloat(dstBlendPropId, (int)BlendMode.OneMinusSrcAlpha);//10 - material.SetFloat(srcBlendPropId, (int) BlendMode.SrcAlpha);//5 - material.SetFloat(cullModePropId, (int)CullMode.Off); - material.SetFloat(k_CullModeForwardPropId, (int)CullMode.Off); + SetShaderModeBlend(gltfMaterial, material); break; case ShaderMode.Premultiply: + SetShaderModePremultiply(gltfMaterial, material); break; } -#endif material.SetVector(baseColorPropId, baseColorLinear); @@ -434,6 +392,16 @@ public override Material GenerateMaterial(Schema.Material gltfMaterial, IGltfRea return material; } + protected virtual void SetDoubleSided(Schema.Material gltfMaterial, Material material) { } + + protected virtual void SetAlphaModeMask(Schema.Material gltfMaterial, Material material) { + material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); + } + + protected virtual void SetShaderModeOpaque(Schema.Material gltfMaterial, Material material) { } + protected virtual void SetShaderModeBlend(Schema.Material gltfMaterial, Material material) { } + protected virtual void SetShaderModePremultiply(Schema.Material gltfMaterial, Material material) { } + protected virtual RenderQueue? ApplyTransmission( ref Color baseColorLinear, IGltfReadable gltf,