Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        public static void CleanupFallbackMaterials()
        {
            // Return if the list is empty.
            if (m_fallbackCleanupList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < m_fallbackCleanupList.Count; i++)
            {
                FallbackMaterial fallback = m_fallbackCleanupList[i];

                if (fallback.count < 1)
                {
                    //Debug.Log("Cleaning up " + fallback.fallbackMaterial.name);

                    Material mat = fallback.fallbackMaterial;
                    m_fallbackMaterials.Remove(fallback.fallbackID);
                    m_fallbackMaterialLookup.Remove(mat.GetInstanceID());
                    Object.DestroyImmediate(mat);
                    mat = null;
                }
            }

            m_fallbackCleanupList.Clear();
        }
Beispiel #2
0
        /// <summary>
        /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
        /// </summary>
        /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
        /// <param name="targetMaterial">The font atlas texture that should be assigned to the new material.</param>
        /// <returns></returns>
        public static Material GetFallbackMaterial(Material sourceMaterial, Material targetMaterial)
        {
            int     sourceID = sourceMaterial.GetInstanceID();
            Texture tex      = targetMaterial.GetTexture(ShaderUtilities.ID_MainTex);
            int     texID    = tex.GetInstanceID();
            long    key      = (long)sourceID << 32 | (long)(uint)texID;

            FallbackMaterial fallback;

            if (m_fallbackMaterials.TryGetValue(key, out fallback))
            {
                //Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists.");
                return(fallback.fallbackMaterial);
            }

            // Create new material from the source material and copy properties if using distance field shaders.
            Material fallbackMaterial = null;

            if (sourceMaterial.HasProperty(ShaderUtilities.ID_GradientScale) && targetMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
            {
                fallbackMaterial           = new Material(sourceMaterial);
                fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;

                #if UNITY_EDITOR
                fallbackMaterial.name += " + " + tex.name;
                //Debug.Log("Creating new fallback material for " + fallbackMaterial.name);
                #endif

                fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, tex);
                // Retain material properties unique to target material.
                fallbackMaterial.SetFloat(ShaderUtilities.ID_GradientScale, targetMaterial.GetFloat(ShaderUtilities.ID_GradientScale));
                fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureWidth, targetMaterial.GetFloat(ShaderUtilities.ID_TextureWidth));
                fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureHeight, targetMaterial.GetFloat(ShaderUtilities.ID_TextureHeight));
                fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightNormal, targetMaterial.GetFloat(ShaderUtilities.ID_WeightNormal));
                fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightBold, targetMaterial.GetFloat(ShaderUtilities.ID_WeightBold));
            }
            else
            {
                fallbackMaterial = new Material(targetMaterial);
            }

            fallback                  = new FallbackMaterial();
            fallback.baseID           = sourceID;
            fallback.baseMaterial     = sourceMaterial;
            fallback.fallbackID       = key;
            fallback.fallbackMaterial = fallbackMaterial;
            fallback.count            = 0;

            m_fallbackMaterials.Add(key, fallback);
            m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);

            #if TMP_DEBUG_MODE
            ListFallbackMaterials();
            #endif

            return(fallbackMaterial);
        }
Beispiel #3
0
        /// <summary>
        /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
        /// </summary>
        /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
        /// <param name="targetMaterial">The font atlas texture that should be assigned to the new material.</param>
        /// <returns></returns>
        public static Material GetFallbackMaterial(Material sourceMaterial, Material targetMaterial)
        {
            int     sourceID = sourceMaterial.GetInstanceID();
            Texture tex      = targetMaterial.GetTexture(ShaderUtilities.ID_MainTex);
            int     texID    = tex.GetInstanceID();
            long    key      = (long)sourceID << 32 | (long)(uint)texID;

            FallbackMaterial fallback;

            if (m_fallbackMaterials.TryGetValue(key, out fallback))
            {
                //Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists.");
                return(fallback.fallbackMaterial);
            }

            // Create new material from the source material
            Material fallbackMaterial = new Material(sourceMaterial);

            fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;

            #if UNITY_EDITOR
            fallbackMaterial.name += " + " + tex.name;
            //Debug.Log("Creating new fallback material for " + fallbackMaterial.name);
            #endif

            fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, tex);
            // Retain material properties unique to target material.
            fallbackMaterial.SetFloat(ShaderUtilities.ID_GradientScale, targetMaterial.GetFloat(ShaderUtilities.ID_GradientScale));
            fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureWidth, targetMaterial.GetFloat(ShaderUtilities.ID_TextureWidth));
            fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureHeight, targetMaterial.GetFloat(ShaderUtilities.ID_TextureHeight));
            fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightNormal, targetMaterial.GetFloat(ShaderUtilities.ID_WeightNormal));
            fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightBold, targetMaterial.GetFloat(ShaderUtilities.ID_WeightBold));

            fallback                  = new FallbackMaterial();
            fallback.baseID           = sourceID;
            fallback.baseMaterial     = sourceMaterial;
            fallback.fallbackID       = key;
            fallback.fallbackMaterial = fallbackMaterial;
            fallback.count            = 0;

            #if UNITY_5_0 || UNITY_5_1
            // Have to manually copy shader keywords in Unity 5.0 and 5.1
            fallbackMaterial.shaderKeywords = sourceMaterial.shaderKeywords;
            #endif

            m_fallbackMaterials.Add(key, fallback);
            m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);

            #if DEBUG_ON
            ListFallbackMaterials();
            #endif

            return(fallbackMaterial);
        }
        internal static Material GetFallbackMaterial(TMP_FontAsset fontAsset, Material sourceMaterial, int atlasIndex)
        {
            int              sourceMaterialID = sourceMaterial.GetInstanceID();
            Texture          tex   = fontAsset.atlasTextures[atlasIndex];
            int              texID = tex.GetInstanceID();
            long             key   = (long)sourceMaterialID << 32 | (long)(uint)texID;
            FallbackMaterial fallback;

            if (m_fallbackMaterials.TryGetValue(key, out fallback))
            {
                // Check if source material properties have changed.
                int sourceMaterialCRC = sourceMaterial.ComputeCRC();
                if (sourceMaterialCRC == fallback.sourceMaterialCRC)
                {
                    return(fallback.fallbackMaterial);
                }

                CopyMaterialPresetProperties(sourceMaterial, fallback.fallbackMaterial);
                fallback.sourceMaterialCRC = sourceMaterialCRC;
                return(fallback.fallbackMaterial);
            }

            // Create new material from the source material and assign relevant atlas texture
            Material fallbackMaterial = new Material(sourceMaterial);

            fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, tex);

            fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;

            #if UNITY_EDITOR
            fallbackMaterial.name += " + " + tex.name;
            #endif

            fallback                   = new FallbackMaterial();
            fallback.fallbackID        = key;
            fallback.sourceMaterial    = fontAsset.material;
            fallback.sourceMaterialCRC = sourceMaterial.ComputeCRC();
            fallback.fallbackMaterial  = fallbackMaterial;
            fallback.count             = 0;

            m_fallbackMaterials.Add(key, fallback);
            m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);

            #if TMP_DEBUG_MODE
            ListFallbackMaterials();
            #endif

            return(fallbackMaterial);
        }
        /// <summary>
        /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
        /// </summary>
        /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
        /// <param name="sourceAtlasTexture">The font atlas texture that should be assigned to the new material.</param>
        /// <returns></returns>
        public static Material GetFallbackMaterial(Material sourceMaterial, Texture sourceAtlasTexture)
        {
            int  sourceID = sourceMaterial.GetInstanceID();
            int  texID    = sourceAtlasTexture.GetInstanceID();
            long key      = (long)sourceID << 32 + texID;

            FallbackMaterial fallback;

            if (m_fallbackMaterials.TryGetValue(key, out fallback))
            {
                //Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists.");
                return(fallback.fallbackMaterial);
            }


            // Create new material from the source material
            Material fallbackMaterial = new Material(sourceMaterial);

            fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;

            #if UNITY_EDITOR
            fallbackMaterial.name += " + " + sourceAtlasTexture.name;
            #endif

            fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, sourceAtlasTexture);


            fallback                  = new FallbackMaterial();
            fallback.baseID           = sourceID;
            fallback.baseMaterial     = sourceMaterial;
            fallback.fallbackMaterial = fallbackMaterial;
            fallback.count            = 0;

            m_fallbackMaterials.Add(key, fallback);
            m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);

            #if DEBUG_ON
            ListFallbackMaterials();
            #endif

            return(fallbackMaterial);
        }
 public static void CleanupFallbackMaterials()
 {
     if (m_fallbackCleanupList.Count == 0)
     {
         return;
     }
     for (int i = 0; i < m_fallbackCleanupList.Count; i++)
     {
         FallbackMaterial fallbackMaterial = m_fallbackCleanupList[i];
         if (fallbackMaterial.count < 1)
         {
             Material fallbackMaterial2 = fallbackMaterial.fallbackMaterial;
             m_fallbackMaterials.Remove(fallbackMaterial.fallbackID);
             m_fallbackMaterialLookup.Remove(fallbackMaterial2.GetInstanceID());
             UnityEngine.Object.DestroyImmediate(fallbackMaterial2);
             fallbackMaterial2 = null;
         }
     }
     m_fallbackCleanupList.Clear();
 }
Beispiel #7
0
        public static Material GetFallbackMaterial(Material sourceMaterial, Material targetMaterial)
        {
            int              instanceID  = sourceMaterial.GetInstanceID();
            Texture          texture     = targetMaterial.GetTexture(ShaderUtilities.ID_MainTex);
            int              instanceID2 = texture.GetInstanceID();
            long             num         = ((long)instanceID << 32) | (uint)instanceID2;
            FallbackMaterial value;

            if (m_fallbackMaterials.TryGetValue(num, out value))
            {
                return(value.fallbackMaterial);
            }
            Material material = null;

            if (sourceMaterial.HasProperty(ShaderUtilities.ID_GradientScale) && targetMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
            {
                material           = new Material(sourceMaterial);
                material.hideFlags = HideFlags.HideAndDontSave;
                Material material2 = material;
                material2.name = material2.name + " + " + texture.name;
                material.SetTexture(ShaderUtilities.ID_MainTex, texture);
                material.SetFloat(ShaderUtilities.ID_GradientScale, targetMaterial.GetFloat(ShaderUtilities.ID_GradientScale));
                material.SetFloat(ShaderUtilities.ID_TextureWidth, targetMaterial.GetFloat(ShaderUtilities.ID_TextureWidth));
                material.SetFloat(ShaderUtilities.ID_TextureHeight, targetMaterial.GetFloat(ShaderUtilities.ID_TextureHeight));
                material.SetFloat(ShaderUtilities.ID_WeightNormal, targetMaterial.GetFloat(ShaderUtilities.ID_WeightNormal));
                material.SetFloat(ShaderUtilities.ID_WeightBold, targetMaterial.GetFloat(ShaderUtilities.ID_WeightBold));
            }
            else
            {
                material = new Material(targetMaterial);
            }
            value                  = new FallbackMaterial();
            value.baseID           = instanceID;
            value.baseMaterial     = sourceMaterial;
            value.fallbackID       = num;
            value.fallbackMaterial = material;
            value.count            = 0;
            m_fallbackMaterials.Add(num, value);
            m_fallbackMaterialLookup.Add(material.GetInstanceID(), num);
            return(material);
        }
        /// <summary>
        /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
        /// </summary>
        /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
        /// <param name="sourceAtlasTexture">The font atlas texture that should be assigned to the new material.</param>
        /// <returns></returns>
        public static Material GetFallbackMaterial(Material sourceMaterial, Texture sourceAtlasTexture)
        {
            int sourceID = sourceMaterial.GetInstanceID();
            int texID = sourceAtlasTexture.GetInstanceID();
            long key = (long)sourceID << 32 + texID;

            FallbackMaterial fallback;
            if (m_fallbackMaterials.TryGetValue(key, out fallback))
            {
                //Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists.");
                return fallback.fallbackMaterial;
            }

            // Create new material from the source material
            Material fallbackMaterial = new Material(sourceMaterial);
            fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;

            #if UNITY_EDITOR
                fallbackMaterial.name += " + " + sourceAtlasTexture.name;
            #endif

            fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, sourceAtlasTexture);

            fallback = new FallbackMaterial();
            fallback.baseID = sourceID;
            fallback.baseMaterial = sourceMaterial;
            fallback.fallbackMaterial = fallbackMaterial;
            fallback.count = 0;

            m_fallbackMaterials.Add(key, fallback);
            m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);

            #if DEBUG_ON
                ListFallbackMaterials();
            #endif

            return fallbackMaterial;
        }