Esempio n. 1
0
        static void OnShaderGraphSaved(Shader shader)
        {
            // Iterate all Materials
            string[] materialGuids = AssetDatabase.FindAssets(kMaterialFilter);
            try
            {
                for (int i = 0, length = materialGuids.Length; i < length; i++)
                {
                    // Only update progress bar every 10 materials
                    if (i % 10 == 9)
                    {
                        EditorUtility.DisplayProgressBar(
                            "Updating dependent materials...",
                            string.Format("{0} / {1} materials updated.", i, length),
                            i / (float)(length - 1));
                    }

                    // Get Material object
                    string   materialPath = AssetDatabase.GUIDToAssetPath(materialGuids[i]);
                    Material material     = AssetDatabase.LoadAssetAtPath <Material>(materialPath);

                    // Reset keywords
                    if (material.shader.name == shader.name)
                    {
                        HDEditorUtils.ResetMaterialKeywords(material);
                    }
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Esempio n. 2
0
        // This upgrade functioncopy all the keywords needed for the BlendStates
        // to be synced with their master node values, then it calls the HDRP material keyword reset function and finally
        // it set the render queue of the material to match the one on the shader graph.
        // It's required to sync the shader default properties with the material because when you create a new material,
        // by default the Lit shader is assigned to it and so write all his properties into the material. It's a problem
        // because now that the shader graphs uses these properties, the material properties don't match the shader settings.
        // This function basically fix this.
        static bool UpdateMaterial_ShaderGraphRenderStates(string path, Material mat)
        {
            // We only need to upgrade shadergraphs materials
            if (GraphUtil.IsShaderGraph(mat.shader))
            {
                var defaultProperties = new Material(mat.shader);

                foreach (var floatToReset in floatPropertiesToReset)
                {
                    if (mat.HasProperty(floatToReset))
                    {
                        mat.SetFloat(floatToReset, defaultProperties.GetFloat(floatToReset));
                    }
                }
                foreach (var vectorToReset in vectorPropertiesToReset)
                {
                    if (mat.HasProperty(vectorToReset))
                    {
                        mat.SetVector(vectorToReset, defaultProperties.GetVector(vectorToReset));
                    }
                }

                HDEditorUtils.ResetMaterialKeywords(mat);

                mat.renderQueue = mat.shader.renderQueue;

                defaultProperties = null;

                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        static bool ResetAllLoadedMaterialKeywords(string descriptionPrefix, float progressScale, float progressOffset)
        {
            var materials = Resources.FindObjectsOfTypeAll <Material>();

            bool VCSEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);

            bool anyMaterialDirty = false; // Will be true if any material is dirty.

            for (int i = 0, length = materials.Length; i < length; i++)
            {
                EditorUtility.DisplayProgressBar(
                    "Setup materials Keywords...",
                    string.Format("{0}{1} / {2} materials cleaned.", descriptionPrefix, i, length),
                    (i / (float)(length - 1)) * progressScale + progressOffset);

                CoreEditorUtils.CheckOutFile(VCSEnabled, materials[i]);

                if (HDEditorUtils.ResetMaterialKeywords(materials[i]))
                {
                    anyMaterialDirty = true;
                }
            }

            return(anyMaterialDirty);
        }
        public override void Convert(Material srcMaterial, Material dstMaterial)
        {
            //dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;

            base.Convert(srcMaterial, dstMaterial);

            HDEditorUtils.ResetMaterialKeywords(dstMaterial);
        }
Esempio n. 5
0
        void DrawShaderGraphGUI()
        {
            // Filter out properties we don't want to draw:
            PropertiesDefaultGUI(properties);

            // If we change a property in a shadergraph, we trigger a material keyword reset
            if (CheckPropertyChanged(properties))
            {
                foreach (var material in materials)
                {
                    HDEditorUtils.ResetMaterialKeywords(material);
                }
            }

            if (properties.Length > 0)
            {
                EditorGUILayout.Space();
            }

            if ((m_Features & Features.DiffusionProfileAsset) != 0)
            {
                DrawDiffusionProfileUI();
            }

            if ((m_Features & Features.EnableInstancing) != 0)
            {
                materialEditor.EnableInstancingField();
            }

            if ((m_Features & Features.DoubleSidedGI) != 0)
            {
                // If the shader graph have a double sided flag, then we don't display this field.
                // The double sided GI value will be synced with the double sided property during the SetupBaseUnlitKeywords()
                if (!materials[0].HasProperty(kDoubleSidedEnable))
                {
                    materialEditor.DoubleSidedGIField();
                }
            }

            if ((m_Features & Features.EmissionGI) != 0)
            {
                DrawEmissionGI();
            }

            if ((m_Features & Features.MotionVector) != 0)
            {
                DrawMotionVectorToggle();
            }
        }
Esempio n. 6
0
        static void ResetAllMaterialAssetsKeywords(float progressScale, float progressOffset)
        {
            var matIds = AssetDatabase.FindAssets("t:Material");

            bool VCSEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);

            for (int i = 0, length = matIds.Length; i < length; i++)
            {
                var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
                var mat  = AssetDatabase.LoadAssetAtPath <Material>(path);

                EditorUtility.DisplayProgressBar(
                    "Setup material asset's Keywords...",
                    string.Format("{0} / {1} materials cleaned.", i, length),
                    (i / (float)(length - 1)) * progressScale + progressOffset);

                CoreEditorUtils.CheckOutFile(VCSEnabled, mat);
                var h = Debug.unityLogger.logHandler;
                Debug.unityLogger.logHandler = new UnityContextualLogHandler(mat);
                HDEditorUtils.ResetMaterialKeywords(mat);
                Debug.unityLogger.logHandler = h;
            }
        }
Esempio n. 7
0
        static void Execute(CommandLineAction action)
        {
            switch (action.operation)
            {
            case CommandLineOperation.ResetMaterialKeywords:
            {
                Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] Starting material reset");

                var matIds = AssetDatabase.FindAssets("t:Material");

                for (int i = 0, length = matIds.Length; i < length; i++)
                {
                    var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
                    var mat  = AssetDatabase.LoadAssetAtPath <Material>(path);

                    if (HDEditorUtils.ResetMaterialKeywords(mat))
                    {
                        Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] " + path);
                    }
                }
                break;
            }
            }
        }
        public override void Convert(Material srcMaterial, Material dstMaterial)
        {
            dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;

            base.Convert(srcMaterial, dstMaterial);

            // ---------- Mask Map ----------

            // Metallic
            bool    hasMetallic = false;
            Texture metallicMap = TextureCombiner.TextureFromColor(Color.black);

            if ((srcMaterial.shader.name == Standard) || (srcMaterial.shader.name == Standard_Rough))
            {
                hasMetallic = srcMaterial.GetTexture("_MetallicGlossMap") != null;
                if (hasMetallic)
                {
                    metallicMap = TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", Color.white);
                }
                else
                {
                    metallicMap = TextureCombiner.TextureFromColor(Color.white);
                }

                // Convert _Metallic value from Gamma to Linear, or set to 1 if a map is used
                float metallicValue = Mathf.Pow(srcMaterial.GetFloat("_Metallic"), 2.2f);
                dstMaterial.SetFloat("_Metallic", hasMetallic? 1f : metallicValue);
            }

            // Occlusion
            bool    hasOcclusion = srcMaterial.GetTexture("_OcclusionMap") != null;
            Texture occlusionMap = Texture2D.whiteTexture;

            if (hasOcclusion)
            {
                occlusionMap = TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white);
            }

            dstMaterial.SetFloat("_AORemapMin", 1f - srcMaterial.GetFloat("_OcclusionStrength"));

            // Detail Mask
            bool    hasDetailMask = srcMaterial.GetTexture("_DetailMask") != null;
            Texture detailMaskMap = Texture2D.whiteTexture;

            if (hasDetailMask)
            {
                detailMaskMap = TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white);
            }

            // Smoothness
            bool      hasSmoothness = false;
            Texture2D smoothnessMap = TextureCombiner.TextureFromColor(Color.white);

            dstMaterial.SetFloat("_SmoothnessRemapMax", srcMaterial.GetFloat("_Glossiness"));

            if (srcMaterial.shader.name == Standard_Rough)
            {
                hasSmoothness = srcMaterial.GetTexture("_SpecGlossMap") != null;

                if (hasSmoothness)
                {
                    smoothnessMap = (Texture2D)TextureCombiner.GetTextureSafe(srcMaterial, "_SpecGlossMap", Color.grey);
                }
            }
            else
            {
                string smoothnessTextureChannel = "_MainTex";

                if (srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)
                {
                    if (srcMaterial.shader.name == Standard)
                    {
                        smoothnessTextureChannel = "_MetallicGlossMap";
                    }
                    if (srcMaterial.shader.name == Standard_Spec)
                    {
                        smoothnessTextureChannel = "_SpecGlossMap";
                    }
                }

                smoothnessMap = (Texture2D)srcMaterial.GetTexture(smoothnessTextureChannel);
                if (smoothnessMap != null)
                {
                    hasSmoothness = true;

                    dstMaterial.SetFloat("_SmoothnessRemapMax", srcMaterial.GetFloat("_GlossMapScale"));

                    if (!TextureCombiner.TextureHasAlpha(smoothnessMap))
                    {
                        smoothnessMap = TextureCombiner.TextureFromColor(Color.white);
                    }
                }
                else
                {
                    smoothnessMap = TextureCombiner.TextureFromColor(Color.white);
                }
            }


            // Build the mask map
            if (hasMetallic || hasOcclusion || hasDetailMask || hasSmoothness)
            {
                Texture2D maskMap;

                TextureCombiner maskMapCombiner = new TextureCombiner(
                    metallicMap, 0,                                                         // R: Metallic from red
                    occlusionMap, 1,                                                        // G: Occlusion from green
                    detailMaskMap, 3,                                                       // B: Detail Mask from alpha
                    smoothnessMap, (srcMaterial.shader.name == Standard_Rough) ? -4 : 3     // A: Smoothness Texture from inverse greyscale for roughness setup, or alpha
                    );

                string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
                maskMapPath = maskMapPath.Remove(maskMapPath.Length - 4) + "_MaskMap.png";
                maskMap     = maskMapCombiner.Combine(maskMapPath);
                dstMaterial.SetTexture("_MaskMap", maskMap);
            }

            // Specular Setup Specific
            if (srcMaterial.shader.name == Standard_Spec)
            {
                // if there is a specular map, change the specular color to white
                if (srcMaterial.GetTexture("_SpecGlossMap") != null)
                {
                    dstMaterial.SetColor("_SpecularColor", Color.white);
                }
            }

            // ---------- Height Map ----------
            bool hasHeightMap = srcMaterial.GetTexture("_ParallaxMap") != null;

            if (hasHeightMap) // Enable Parallax Occlusion Mapping
            {
                dstMaterial.SetFloat("_DisplacementMode", 2);
                dstMaterial.SetFloat("_HeightPoMAmplitude", srcMaterial.GetFloat("_Parallax") * 2f);
            }

            // ---------- Detail Map ----------
            bool hasDetailAlbedo = srcMaterial.GetTexture("_DetailAlbedoMap") != null;
            bool hasDetailNormal = srcMaterial.GetTexture("_DetailNormalMap") != null;

            if (hasDetailAlbedo || hasDetailNormal)
            {
                Texture2D       detailMap;
                TextureCombiner detailCombiner = new TextureCombiner(
                    TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", Color.grey), 4,     // Albedo (overlay)
                    TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 1,     // Normal Y
                    TextureCombiner.midGrey, 1,                                                         // Smoothness
                    TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 0      // Normal X
                    );
                string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
                detailMapPath = detailMapPath.Remove(detailMapPath.Length - 4) + "_DetailMap.png";
                detailMap     = detailCombiner.Combine(detailMapPath);
                dstMaterial.SetTexture("_DetailMap", detailMap);
            }


            // Blend Mode
            int previousBlendMode = srcMaterial.GetInt("_Mode");

            switch (previousBlendMode)
            {
            case 0:     // Opaque
                dstMaterial.SetFloat("_SurfaceType", 0);
                dstMaterial.SetFloat("_BlendMode", 0);
                dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
                dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
                break;

            case 1:     // Cutout
                dstMaterial.SetFloat("_SurfaceType", 0);
                dstMaterial.SetFloat("_BlendMode", 0);
                dstMaterial.SetFloat("_AlphaCutoffEnable", 1);
                dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
                break;

            case 2:     // Fade -> Alpha + Disable preserve specular
                dstMaterial.SetFloat("_SurfaceType", 1);
                dstMaterial.SetFloat("_BlendMode", 0);
                dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
                dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 0);
                break;

            case 3:     // Transparent -> Alpha
                dstMaterial.SetFloat("_SurfaceType", 1);
                dstMaterial.SetFloat("_BlendMode", 0);
                dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
                dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
                break;
            }

            Color hdrEmission = srcMaterial.GetColor("_EmissionColor");

            // Get the _EMISSION keyword of the Standard shader
            if (!srcMaterial.IsKeywordEnabled("_EMISSION"))
            {
                hdrEmission = Color.black;
            }

            // Emission toggle of Particle Standard Surface
            if (srcMaterial.HasProperty("_EmissionEnabled"))
            {
                if (srcMaterial.GetFloat("_EmissionEnabled") == 0)
                {
                    hdrEmission = Color.black;
                }
            }

            dstMaterial.SetColor("_EmissiveColor", hdrEmission);

            HDEditorUtils.ResetMaterialKeywords(dstMaterial);
        }