// Not used currently:
        // TODO: Script like this must also work with embed material in scene (i.e we need to catch
        // .unity scene and load material and patch in memory. And it must work with perforce
        // i.e automatically checkout all those files).
        static void SpecularOcclusionMode(Material material, HDShaderUtils.ShaderID id)
        {
            switch (id)
            {
            case HDShaderUtils.ShaderID.Lit:
            case HDShaderUtils.ShaderID.LayeredLit:
            case HDShaderUtils.ShaderID.LitTesselation:
            case HDShaderUtils.ShaderID.LayeredLitTesselation:
                var serializedObject  = new SerializedObject(material);
                var specOcclusionMode = 1;
                if (FindProperty(serializedObject, "_EnableSpecularOcclusion", SerializedType.Boolean).property != null)
                {
                    var enableSpecOcclusion = GetSerializedBoolean(serializedObject, "_EnableSpecularOcclusion");
                    if (enableSpecOcclusion)
                    {
                        specOcclusionMode = 2;
                    }
                    RemoveSerializedBoolean(serializedObject, "_EnableSpecularOcclusion");
                    serializedObject.ApplyModifiedProperties();
                }
                material.SetInt("_SpecularOcclusionMode", specOcclusionMode);

                HDShaderUtils.ResetMaterialKeywords(material);
                break;
            }
        }
示例#2
0
        static void MigrateDecalRenderQueue(Material material, HDShaderUtils.ShaderID id)
        {
            const string kSupportDecals = "_SupportDecals";

            // Take the opportunity to remove _SupportDecals from Unlit as it is not suppose to be here
            if (HDShaderUtils.IsUnlitHDRPShader(material.shader))
            {
                var serializedMaterial = new SerializedObject(material);
                if (TryFindProperty(serializedMaterial, kSupportDecals, SerializedType.Integer, out var property, out _, out _))
                {
                    RemoveSerializedInt(serializedMaterial, kSupportDecals);
                    serializedMaterial.ApplyModifiedProperties();
                }
            }

            if (material.HasProperty(kSupportDecals))
            {
                bool supportDecal = material.GetFloat(kSupportDecals) > 0.0f;

                if (supportDecal)
                {
                    // Update material render queue to be in Decal render queue based on the value of decal property (see HDRenderQueue.cs)
                    if (material.renderQueue == ((int)UnityEngine.Rendering.RenderQueue.Geometry))
                    {
                        material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry + 225;
                    }
                    else if (material.renderQueue == ((int)UnityEngine.Rendering.RenderQueue.AlphaTest))
                    {
                        material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest + 25;
                    }
                }
            }

            HDShaderUtils.ResetMaterialKeywords(material);
        }
示例#3
0
        static void MetallicRemapping(Material material, HDShaderUtils.ShaderID id)
        {
            const string kMetallicRemapMax = "_MetallicRemapMax";

            // Lit shaders now have metallic remapping for the mask map
            if (id == HDShaderUtils.ShaderID.Lit || id == HDShaderUtils.ShaderID.LitTesselation ||
                id == HDShaderUtils.ShaderID.LayeredLit || id == HDShaderUtils.ShaderID.LayeredLitTesselation)
            {
                const string kMetallic = "_Metallic";
                if (material.HasProperty(kMetallic) && material.HasProperty(kMetallicRemapMax))
                {
                    var metallic = material.GetFloat(kMetallic);
                    material.SetFloat(kMetallicRemapMax, metallic);
                }
            }
            else if (id == HDShaderUtils.ShaderID.Decal)
            {
                HDShaderUtils.ResetMaterialKeywords(material);
                var serializedMaterial = new SerializedObject(material);

                const string kMetallicScale = "_MetallicScale";
                float        metallicScale  = 1.0f;
                if (TryFindProperty(serializedMaterial, kMetallicScale, SerializedType.Float, out var propertyMetallicScale, out _, out _))
                {
                    metallicScale = propertyMetallicScale.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kMetallicScale);
                }

                serializedMaterial.ApplyModifiedProperties();

                material.SetFloat(kMetallicRemapMax, metallicScale);
            }
        }
示例#4
0
        static void FixIncorrectEmissiveColorSpace(Material material, HDShaderUtils.ShaderID id)
        {
            // kEmissiveColorLDR wasn't correctly converted to linear color space.
            // so here we adjust the value of kEmissiveColorLDR to compensate. But only if not using a HDR Color
            const string kUseEmissiveIntensity = "_UseEmissiveIntensity";

            if (material.HasProperty(kUseEmissiveIntensity) && material.GetInt(kUseEmissiveIntensity) == 1)
            {
                const string kEmissiveColorLDR  = "_EmissiveColorLDR";
                const string kEmissiveColor     = "_EmissiveColor";
                const string kEmissiveIntensity = "_EmissiveIntensity";

                if (material.HasProperty(kEmissiveColorLDR) && material.HasProperty(kEmissiveIntensity) && material.HasProperty(kEmissiveColor))
                {
                    // Important:  The color picker for kEmissiveColorLDR is LDR and in sRGB color space but Unity don't perform any color space conversion in the color
                    // picker BUT only when sending the color data to the shader... So as we are doing our own calculation here in C#, we must do the conversion ourselves.
                    Color emissiveColorLDR     = material.GetColor(kEmissiveColorLDR);
                    Color emissiveColorLDRsRGB = new Color(Mathf.LinearToGammaSpace(emissiveColorLDR.r), Mathf.LinearToGammaSpace(emissiveColorLDR.g), Mathf.LinearToGammaSpace(emissiveColorLDR.b));
                    material.SetColor(kEmissiveColorLDR, emissiveColorLDRsRGB);
                }

                // Reset the value of kEmissiveColor
                material.UpdateEmissiveColorFromIntensityAndEmissiveColorLDR();
            }
        }
示例#5
0
        static void MoreMaterialSurfaceOptionFromShaderGraph(Material material, HDShaderUtils.ShaderID id)
        {
            if (material.shader.IsShaderGraph())
            {
                // Synchronize properties we exposed from SG to the material
                ResetFloatProperty(kReceivesSSR);
                ResetFloatProperty(kReceivesSSRTransparent);
                ResetFloatProperty(kEnableDecals);
                ResetFloatProperty(kEnableBlendModePreserveSpecularLighting);
                ResetFloatProperty(kTransparentWritingMotionVec);
                ResetFloatProperty(kAddPrecomputedVelocity);
                ResetFloatProperty(kDepthOffsetEnable);
            }

            void ResetFloatProperty(string propName)
            {
                int propIndex = material.shader.FindPropertyIndex(propName);

                if (propIndex == -1)
                {
                    return;
                }
                float defaultValue = material.shader.GetPropertyDefaultFloatValue(propIndex);

                material.SetFloat(propName, defaultValue);
            }

            HDShaderUtils.ResetMaterialKeywords(material);
        }
示例#6
0
 static void AlphaToMaskUIFix(Material material, HDShaderUtils.ShaderID id)
 {
     if (material.HasProperty(kAlphaToMask) && material.HasProperty(kAlphaToMaskInspector))
     {
         material.SetFloat(kAlphaToMaskInspector, material.GetFloat(kAlphaToMask));
         HDShaderUtils.ResetMaterialKeywords(material);
     }
 }
示例#7
0
 static void ForceForwardEmissiveForDeferred(Material material, HDShaderUtils.ShaderID id)
 {
     // Force Forward emissive for deferred pass is only setup for Lit shader
     if (id == HDShaderUtils.ShaderID.SG_Lit || id == HDShaderUtils.ShaderID.Lit || id == HDShaderUtils.ShaderID.LitTesselation ||
         id == HDShaderUtils.ShaderID.LayeredLit || id == HDShaderUtils.ShaderID.LayeredLitTesselation)
     {
         HDShaderUtils.ResetMaterialKeywords(material);
     }
 }
        //example migration method, remove it after first real migration
        //static void EmissiveIntensityToColor(Material material, ShaderID id)
        //{
        //    switch(id)
        //    {
        //        case ShaderID.Lit:
        //        case ShaderID.LitTesselation:
        //            var emissiveIntensity = material.GetFloat("_EmissiveIntensity");
        //            var emissiveColor = Color.black;
        //            if (material.HasProperty("_EmissiveColor"))
        //                emissiveColor = material.GetColor("_EmissiveColor");
        //            emissiveColor *= emissiveIntensity;
        //            emissiveColor.a = 1.0f;
        //            material.SetColor("_EmissiveColor", emissiveColor);
        //            material.SetColor("_EmissionColor", Color.white);
        //            break;
        //    }
        //}
        //
        //static void Serialization_API_Usage(Material material, ShaderID id)
        //{
        //    switch(id)
        //    {
        //        case ShaderID.Unlit:
        //            var serializedObject = new SerializedObject(material);
        //            AddSerializedInt(serializedObject, "former", 42);
        //            RenameSerializedScalar(serializedObject, "former", "new");
        //            Debug.Log(GetSerializedInt(serializedObject, "new"));
        //            RemoveSerializedInt(serializedObject, "new");
        //            serializedObject.ApplyModifiedProperties();
        //            break;
        //    }
        //}

        static void ZWriteForTransparent(Material material, HDShaderUtils.ShaderID id)
        {
            // For transparent materials, the ZWrite property that is now used is _TransparentZWrite.
            if (material.GetSurfaceType() == SurfaceType.Transparent)
            {
                material.SetFloat(kTransparentZWrite, material.GetZWrite() ? 1.0f : 0.0f);
            }

            HDShaderUtils.ResetMaterialKeywords(material);
        }
示例#9
0
 static void ExposeRefraction(Material material, HDShaderUtils.ShaderID id)
 {
     // Lit SG now have a shader feature for refraction instead of an hardcoded material
     if (id == HDShaderUtils.ShaderID.SG_Lit)
     {
         // Sync the default refraction model from the shader graph to the shader
         // We need to do this because the material may already have a refraction model information (from the Lit)
         // In order to not break the rendering of the material, we patch the refraction model:
         if (material.HasProperty(kRefractionModel))
         {
             var refractionModel = material.shader.GetPropertyDefaultFloatValue(material.shader.FindPropertyIndex(kRefractionModel));
             material.SetFloat(kRefractionModel, refractionModel);
         }
         HDShaderUtils.ResetMaterialKeywords(material);
     }
 }
示例#10
0
        static void ShaderGraphStack(Material material, HDShaderUtils.ShaderID id)
        {
            Shader shader = material.shader;

            if (shader.IsShaderGraph())
            {
                if (shader.TryGetMetadataOfType <HDMetadata>(out var obj))
                {
                    // Material coming from old cross pipeline shader (Unlit and PBR) are not synchronize correctly with their
                    // shader graph. This code below ensure it is
                    if (obj.migrateFromOldCrossPipelineSG) // come from PBR or Unlit cross pipeline SG?
                    {
                        var defaultProperties = new Material(material.shader);

                        foreach (var floatToSync in s_ShadergraphStackFloatPropertiesToSynchronize)
                        {
                            if (material.HasProperty(floatToSync))
                            {
                                material.SetFloat(floatToSync, defaultProperties.GetFloat(floatToSync));
                            }
                        }

                        defaultProperties = null;

                        // Postprocess now that material is correctly sync
                        bool isTransparent = material.HasProperty("_SurfaceType") && material.GetFloat("_SurfaceType") > 0.0f;
                        bool alphaTest     = material.HasProperty("_AlphaCutoffEnable") && material.GetFloat("_AlphaCutoffEnable") > 0.0f;

                        material.renderQueue = isTransparent ? (int)HDRenderQueue.Priority.Transparent :
                                               alphaTest ? (int)HDRenderQueue.Priority.OpaqueAlphaTest : (int)HDRenderQueue.Priority.Opaque;

                        material.SetFloat("_RenderQueueType", isTransparent ? (float)HDRenderQueue.RenderQueueType.Transparent : (float)HDRenderQueue.RenderQueueType.Opaque);
                    }
                }
            }

            HDShaderUtils.ResetMaterialKeywords(material);
        }
示例#11
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (var asset in importedAssets)
            {
                if (!asset.ToLowerInvariant().EndsWith(".mat"))
                {
                    continue;
                }

                var material = (Material)AssetDatabase.LoadAssetAtPath(asset, typeof(Material));
                if (!HDShaderUtils.IsHDRPShader(material.shader, upgradable: true))
                {
                    continue;
                }

                HDShaderUtils.ShaderID id  = HDShaderUtils.GetShaderEnumFromShader(material.shader);
                var          latestVersion = k_Migrations.Length;
                var          wasUpgraded   = false;
                var          assetVersions = AssetDatabase.LoadAllAssetsAtPath(asset);
                AssetVersion assetVersion  = null;
                foreach (var subAsset in assetVersions)
                {
                    if (subAsset.GetType() == typeof(AssetVersion))
                    {
                        assetVersion = subAsset as AssetVersion;
                        break;
                    }
                }

                //subasset not found
                if (!assetVersion)
                {
                    wasUpgraded            = true;
                    assetVersion           = ScriptableObject.CreateInstance <AssetVersion>();
                    assetVersion.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
                    if (s_CreatedAssets.Contains(asset))
                    {
                        //just created
                        assetVersion.version = latestVersion;
                        s_CreatedAssets.Remove(asset);

                        //[TODO: remove comment once fixed]
                        //due to FB 1175514, this not work. It is being fixed though.
                        //delayed call of the following work in some case and cause infinite loop in other cases.
                        AssetDatabase.AddObjectToAsset(assetVersion, asset);
                    }
                    else
                    {
                        //asset exist prior migration
                        assetVersion.version = 0;
                        AssetDatabase.AddObjectToAsset(assetVersion, asset);
                    }
                }

                //upgrade
                while (assetVersion.version < latestVersion)
                {
                    k_Migrations[assetVersion.version](material, id);
                    assetVersion.version++;
                    wasUpgraded = true;
                }

                if (wasUpgraded)
                {
                    EditorUtility.SetDirty(assetVersion);
                }
            }
        }
示例#12
0
        static void RenderQueueUpgrade(Material material, HDShaderUtils.ShaderID id)
        {
            // In order for the ray tracing keyword to be taken into account, we need to make it dirty so that the parameter is created first
            HDShaderUtils.ResetMaterialKeywords(material);

            // Replace previous ray tracing render queue for opaque to regular opaque with raytracing
            if (material.renderQueue == ((int)UnityEngine.Rendering.RenderQueue.GeometryLast + 20))
            {
                material.renderQueue = (int)HDRenderQueue.Priority.Opaque;
                material.SetFloat(kRayTracing, 1.0f);
            }
            // Replace previous ray tracing render queue for transparent to regular transparent with raytracing
            else if (material.renderQueue == 3900)
            {
                material.renderQueue = (int)HDRenderQueue.Priority.Transparent;
                material.SetFloat(kRayTracing, 1.0f);
            }

            // For shader graphs, there is an additional pass we need to do
            if (material.HasProperty("_RenderQueueType"))
            {
                int renderQueueType = (int)material.GetFloat("_RenderQueueType");
                switch (renderQueueType)
                {
                // This was ray tracing opaque, should go back to opaque
                case 3:
                {
                    renderQueueType = 1;
                }
                break;

                // If it was in the transparent range, reduce it by 1
                case 4:
                case 5:
                case 6:
                case 7:
                {
                    renderQueueType = renderQueueType - 1;
                }
                break;

                // If it was in the ray tracing transparent, should go back to transparent
                case 8:
                {
                    renderQueueType = renderQueueType - 4;
                }
                break;

                // If it was in overlay should be reduced by 2
                case 10:
                {
                    renderQueueType = renderQueueType - 2;
                }
                break;

                // background, opaque and AfterPostProcessOpaque are not impacted
                default:
                    break;
                }


                // Push it back to the material
                material.SetFloat("_RenderQueueType", (float)renderQueueType);
            }

            HDShaderUtils.ResetMaterialKeywords(material);
        }
示例#13
0
 static void StencilRefactor(Material material, HDShaderUtils.ShaderID id)
 {
     HDShaderUtils.ResetMaterialKeywords(material);
 }
示例#14
0
        static void ExposedDecalInputsFromShaderGraph(Material material, HDShaderUtils.ShaderID id)
        {
            if (id == HDShaderUtils.ShaderID.Decal)
            {
                // In order for the new properties (kAffectsAlbedo...) to be taken into account, we need to make it dirty so that the parameter is created first
                HDShaderUtils.ResetMaterialKeywords(material);

                var serializedMaterial = new SerializedObject(material);

                // Note: the property must not exist in the .shader for RemoveSerializedFloat to work (otherwise it will be re-added)
                const string kAlbedoMode = "_AlbedoMode";
                float        albedoMode  = 1.0f;
                if (TryFindProperty(serializedMaterial, kAlbedoMode, SerializedType.Float, out var propertyAlbedoMode, out _, out _))
                {
                    albedoMode = propertyAlbedoMode.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kAlbedoMode);
                }

                // For normal map we don't remove the property _NormalMap but just check if there is a texture assign and then enable _AffectNormal
                const string kNormalMap = "_NormalMap";
                float        normalMap  = 0.0f;
                if (TryFindProperty(serializedMaterial, kNormalMap, SerializedType.Texture, out var propertyNormalTexture, out _, out _))
                {
                    normalMap = propertyNormalTexture.FindPropertyRelative("m_Texture").objectReferenceValue != null ? 1.0f : 0.0f;
                }

                // For normal map we don't remove the property _NormalMap but just check if there is a texture assign and then enable _AffectNormal
                const string kMaskMap = "_MaskMap";
                float        maskMap  = 0.0f;
                if (TryFindProperty(serializedMaterial, kMaskMap, SerializedType.Texture, out var propertyMaskMapTexture, out _, out _))
                {
                    maskMap = propertyMaskMapTexture.FindPropertyRelative("m_Texture").objectReferenceValue != null ? 1.0f : 0.0f;
                }

                const string kMaskmapMetal = "_MaskmapMetal";
                float        maskMapMetal  = 0.0f;
                if (TryFindProperty(serializedMaterial, kMaskmapMetal, SerializedType.Float, out var propertyMaskMapMetal, out _, out _))
                {
                    maskMapMetal = propertyMaskMapMetal.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kMaskmapMetal);
                }

                const string kMaskmapAO = "_MaskmapAO";
                float        maskMapAO  = 0.0f;
                if (TryFindProperty(serializedMaterial, kMaskmapAO, SerializedType.Float, out var propertyMaskMapAO, out _, out _))
                {
                    maskMapAO = propertyMaskMapAO.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kMaskmapAO);
                }

                const string kMaskmapSmoothness = "_MaskmapSmoothness";
                float        maskMapSmoothness  = 0.0f;
                if (TryFindProperty(serializedMaterial, kMaskmapSmoothness, SerializedType.Float, out var propertyMaskMapSmoothness, out _, out _))
                {
                    maskMapSmoothness = propertyMaskMapSmoothness.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kMaskmapSmoothness);
                }

                const string kEmissive = "_Emissive";
                float        emissive  = 0.0f;
                if (TryFindProperty(serializedMaterial, kEmissive, SerializedType.Float, out var propertyEmissive, out _, out _))
                {
                    emissive = propertyEmissive.floatValue;
                    RemoveSerializedFloat(serializedMaterial, kEmissive);
                }

                // Not used anymore, just removed
                const string kMaskBlendMode = "_MaskBlendMode";
                if (TryFindProperty(serializedMaterial, kMaskBlendMode, SerializedType.Float, out var propertyUnused, out _, out _))
                {
                    RemoveSerializedFloat(serializedMaterial, kMaskBlendMode);
                }

                serializedMaterial.ApplyModifiedProperties();

                // Now apply old value to new properties
                const string kAffectAlbedo = "_AffectAlbedo";
                material.SetFloat(kAffectAlbedo, albedoMode);

                const string kAffectNormal = "_AffectNormal";
                material.SetFloat(kAffectNormal, normalMap);

                const string kAffectSmoothness = "_AffectSmoothness";
                material.SetFloat(kAffectSmoothness, maskMapSmoothness * maskMap);

                const string kAffectMetal = "_AffectMetal";
                material.SetFloat(kAffectMetal, maskMapMetal * maskMap);

                const string kAffectAO = "_AffectAO";
                material.SetFloat(kAffectAO, maskMapAO * maskMap);

                const string kAffectEmission = "_AffectEmission";
                material.SetFloat(kAffectEmission, emissive);

                // We can't erase obsolete disabled pass from already existing Material, so we need to re-enable all of them
                const string s_MeshDecalsMStr            = "DBufferMesh_M";
                const string s_MeshDecalsSStr            = "DBufferMesh_S";
                const string s_MeshDecalsMSStr           = "DBufferMesh_MS";
                const string s_MeshDecalsAOStr           = "DBufferMesh_AO";
                const string s_MeshDecalsMAOStr          = "DBufferMesh_MAO";
                const string s_MeshDecalsAOSStr          = "DBufferMesh_AOS";
                const string s_MeshDecalsMAOSStr         = "DBufferMesh_MAOS";
                const string s_MeshDecals3RTStr          = "DBufferMesh_3RT";
                const string s_MeshDecalsForwardEmissive = "Mesh_Emissive";

                material.SetShaderPassEnabled(s_MeshDecalsMStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsSStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsMSStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsAOStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsMAOStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsAOSStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsMAOSStr, true);
                material.SetShaderPassEnabled(s_MeshDecals3RTStr, true);
                material.SetShaderPassEnabled(s_MeshDecalsForwardEmissive, true);
            }

            if (id == HDShaderUtils.ShaderID.SG_Decal)
            {
                // We can't erase obsolete disabled pass from already existing Material, so we need to re-enable all of them
                const string s_ShaderGraphMeshDecals4RT            = "ShaderGraph_DBufferMesh4RT";
                const string s_ShaderGraphMeshDecals3RT            = "ShaderGraph_DBufferMesh3RT";
                const string s_ShaderGraphMeshDecalForwardEmissive = "ShaderGraph_MeshEmissive";

                material.SetShaderPassEnabled(s_ShaderGraphMeshDecals4RT, true);
                material.SetShaderPassEnabled(s_ShaderGraphMeshDecals3RT, true);
                material.SetShaderPassEnabled(s_ShaderGraphMeshDecalForwardEmissive, true);
            }

            if (id == HDShaderUtils.ShaderID.Decal || id == HDShaderUtils.ShaderID.SG_Decal)
            {
                HDShaderUtils.ResetMaterialKeywords(material);
            }
        }