public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
		{
			if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
			{
				Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
				return baseMaterial;
			}
			Material material = null;
			int num = m_materialList.FindIndex((MaskingMaterial item) => item.baseMaterial == baseMaterial && item.stencilID == stencilID);
			if (num == -1)
			{
				material = new Material(baseMaterial);
				material.hideFlags = HideFlags.HideAndDontSave;
				Material material2 = material;
				material2.name = material2.name + " Masking ID:" + stencilID;
				material.shaderKeywords = baseMaterial.shaderKeywords;
				ShaderUtilities.GetShaderPropertyIDs();
				material.SetFloat(ShaderUtilities.ID_StencilID, (float)stencilID);
				material.SetFloat(ShaderUtilities.ID_StencilComp, 4f);
				MaskingMaterial maskingMaterial = new MaskingMaterial();
				maskingMaterial.baseMaterial = baseMaterial;
				maskingMaterial.stencilMaterial = material;
				maskingMaterial.stencilID = stencilID;
				maskingMaterial.count = 1;
				m_materialList.Add(maskingMaterial);
			}
			else
			{
				material = m_materialList[num].stencilMaterial;
				m_materialList[num].count++;
			}
			ListMaterials();
			return material;
		}
Exemplo n.º 2
0
        public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
        {
            if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
            {
                Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
                return(baseMaterial);
            }
            int      num = MaterialManager.m_materialList.FindIndex((MaterialManager.MaskingMaterial item) => item.baseMaterial == baseMaterial && item.stencilID == stencilID);
            Material material;

            if (num == -1)
            {
                material           = new Material(baseMaterial);
                material.hideFlags = (HideFlags.HideInHierarchy | HideFlags.DontSaveInEditor | HideFlags.NotEditable);
                Material expr_6F = material;
                expr_6F.name            = expr_6F.name + " Masking ID:" + stencilID;
                material.shaderKeywords = baseMaterial.shaderKeywords;
                ShaderUtilities.GetShaderPropertyIDs();
                material.SetFloat(ShaderUtilities.ID_StencilID, (float)stencilID);
                material.SetFloat(ShaderUtilities.ID_StencilComp, 3f);
                MaterialManager.MaskingMaterial maskingMaterial = new MaterialManager.MaskingMaterial();
                maskingMaterial.baseMaterial    = baseMaterial;
                maskingMaterial.stencilMaterial = material;
                maskingMaterial.stencilID       = stencilID;
                maskingMaterial.count           = 1;
                MaterialManager.m_materialList.Add(maskingMaterial);
            }
            else
            {
                material = MaterialManager.m_materialList.get_Item(num).stencilMaterial;
                MaterialManager.m_materialList.get_Item(num).count++;
            }
            MaterialManager.ListMaterials();
            return(material);
        }
        public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
        {
            if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
            {
                Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
                return(baseMaterial);
            }
            int instanceID = baseMaterial.GetInstanceID();

            for (int i = 0; i < TMP_MaterialManager.m_materialList.Count; i++)
            {
                if (TMP_MaterialManager.m_materialList[i].baseMaterial.GetInstanceID() == instanceID && TMP_MaterialManager.m_materialList[i].stencilID == stencilID)
                {
                    TMP_MaterialManager.m_materialList[i].count++;
                    return(TMP_MaterialManager.m_materialList[i].stencilMaterial);
                }
            }
            Material material = new Material(baseMaterial);

            material.hideFlags      = HideFlags.HideAndDontSave;
            material.shaderKeywords = baseMaterial.shaderKeywords;
            ShaderUtilities.GetShaderPropertyIDs();
            material.SetFloat(ShaderUtilities.ID_StencilID, (float)stencilID);
            material.SetFloat(ShaderUtilities.ID_StencilComp, 4f);
            TMP_MaterialManager.MaskingMaterial maskingMaterial = new TMP_MaterialManager.MaskingMaterial();
            maskingMaterial.baseMaterial    = baseMaterial;
            maskingMaterial.stencilMaterial = material;
            maskingMaterial.stencilID       = stencilID;
            maskingMaterial.count           = 1;
            TMP_MaterialManager.m_materialList.Add(maskingMaterial);
            return(material);
        }
Exemplo n.º 4
0
        /// <summary>

        /// Create a material for the sprite asset.

        /// </summary>

        /// <returns></returns>

        Material GetDefaultSpriteMaterial()

        {
            //isEditingAsset = true;

            ShaderUtilities.GetShaderPropertyIDs();



            // Add a new material

            Shader shader = Shader.Find("TextMeshPro/Sprite");

            Material tempMaterial = new Material(shader);

            tempMaterial.SetTexture(ShaderUtilities.ID_MainTex, spriteSheet);

            tempMaterial.hideFlags = HideFlags.HideInHierarchy;



#if UNITY_EDITOR
            UnityEditor.AssetDatabase.AddObjectToAsset(tempMaterial, this);

            UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(this));
#endif

            //isEditingAsset = false;



            return(tempMaterial);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a Masking Material Instance for the given ID
        /// </summary>
        /// <param name="baseMaterial"></param>
        /// <param name="stencilID"></param>
        /// <returns></returns>
        public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
        {
            // Check if Material supports masking
            if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
            {
                Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
                return(baseMaterial);
            }

            int baseMaterialID = baseMaterial.GetInstanceID();

            // If baseMaterial already has a corresponding masking material, return it.
            for (int i = 0; i < m_materialList.Count; i++)
            {
                if (m_materialList[i].baseMaterial.GetInstanceID() == baseMaterialID && m_materialList[i].stencilID == stencilID)
                {
                    m_materialList[i].count += 1;

                    #if DEBUG_ON
                    ListMaterials();
                    #endif

                    return(m_materialList[i].stencilMaterial);
                }
            }

            // No matching masking material found. Create and return a new one.

            Material stencilMaterial;

            //Create new Masking Material Instance for this Base Material
            stencilMaterial                = new Material(baseMaterial);
            stencilMaterial.hideFlags      = HideFlags.HideAndDontSave;
            stencilMaterial.name          += " Masking ID:" + stencilID;
            stencilMaterial.shaderKeywords = baseMaterial.shaderKeywords;

            // Set Stencil Properties
            ShaderUtilities.GetShaderPropertyIDs();
            stencilMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
            //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0);
            stencilMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 4);
            //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID);
            //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0);

            MaskingMaterial temp = new MaskingMaterial();
            temp.baseMaterial    = baseMaterial;
            temp.stencilMaterial = stencilMaterial;
            temp.stencilID       = stencilID;
            temp.count           = 1;

            m_materialList.Add(temp);

            #if DEBUG_ON
            ListMaterials();
            #endif

            return(stencilMaterial);
        }
        private Material GetDefaultSpriteMaterial()
        {
            ShaderUtilities.GetShaderPropertyIDs();
            Shader   shader   = Shader.Find("TextMeshPro/Sprite");
            Material material = new Material(shader);

            material.SetTexture(ShaderUtilities.ID_MainTex, this.spriteSheet);
            material.hideFlags = HideFlags.HideInHierarchy;
            return(material);
        }
        /// <summary>
        /// Create a Masking Material Instance for the given ID
        /// </summary>
        /// <param name="baseMaterial"></param>
        /// <param name="stencilID"></param>
        /// <returns></returns>
        public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
        {
            // Check if Material supports masking
            if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
            {
                Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
                return(baseMaterial);
            }

            Material stencilMaterial = null;

            // Check if baseMaterial already has a masking material associated with it.
            int index = m_materialList.FindIndex(item => item.baseMaterial == baseMaterial && item.stencilID == stencilID);

            if (index == -1)
            {
                //Create new Masking Material Instance for this Base Material
                stencilMaterial                = new Material(baseMaterial);
                stencilMaterial.hideFlags      = HideFlags.HideAndDontSave;
                stencilMaterial.name          += " Masking ID:" + stencilID;
                stencilMaterial.shaderKeywords = baseMaterial.shaderKeywords;

                // Set Stencil Properties
                ShaderUtilities.GetShaderPropertyIDs();
                stencilMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
                stencilMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 4);

                MaskingMaterial temp = new MaskingMaterial();
                temp.baseMaterial    = baseMaterial;
                temp.stencilMaterial = stencilMaterial;
                temp.stencilID       = stencilID;
                temp.count           = 1;

                m_materialList.Add(temp);

                //Debug.Log("Masking material for " + baseMaterial.name + " DOES NOT exists. Creating new " + maskingMaterial.name + " with ID " + maskingMaterial.GetInstanceID() + " which is used " + temp.count + " time(s).");
            }
            else
            {
                stencilMaterial              = m_materialList[index].stencilMaterial;
                m_materialList[index].count += 1;

                //Debug.Log("Masking material for " + baseMaterial.name + " already exists. Passing reference to " + maskingMaterial.name + " with ID " + maskingMaterial.GetInstanceID() + " which is used " + m_materialList[index].count + " time(s).");
            }

            // Used for Debug
            ListMaterials();

            return(stencilMaterial);
        }
Exemplo n.º 8
0
        private Material GetDefaultSpriteMaterial()
        {
            ShaderUtilities.GetShaderPropertyIDs();
            Shader   shader   = Shader.Find("TextMeshPro/Sprite");
            Material material = new Material(shader);

            material.SetTexture(ShaderUtilities.ID_MainTex, spriteSheet);
            material.hideFlags = HideFlags.HideInHierarchy;
#if UNITY_EDITOR
            /*AssetDatabase.AddObjectToAsset(material, this);
             * AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(this));*/
#endif
            return(material);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Method called when Stencil Mask needs to be updated on this element and parents.
        /// </summary>
        public override void RecalculateMasking()
        {
            //Debug.Log("***** RecalculateMasking() *****");

            if (m_fontAsset == null)
            {
                return;
            }

            //if (m_canvas == null) m_canvas = GetComponentInParent<Canvas>();


            if (!m_isAwake)
            {
                return;
            }

            m_stencilID = MaterialManager.GetStencilID(gameObject);
            //m_stencilID = MaskUtilities.GetStencilDepth(this.transform, m_canvas.transform);
            //Debug.Log("Stencil ID: " + m_stencilID + "  Stencil ID (2): " + MaskUtilities.GetStencilDepth(this.transform, m_canvas.transform));

            if (m_stencilID == 0)
            {
                if (m_maskingMaterial != null)
                {
                    MaterialManager.ReleaseStencilMaterial(m_maskingMaterial);
                    m_maskingMaterial = null;

                    m_sharedMaterial = m_baseMaterial;
                }
                else if (m_fontMaterial != null)
                {
                    m_sharedMaterial = MaterialManager.SetStencil(m_fontMaterial, 0);
                }
                else
                {
                    m_sharedMaterial = m_baseMaterial;
                }
            }
            else
            {
                ShaderUtilities.GetShaderPropertyIDs();

                if (m_fontMaterial != null)
                {
                    m_sharedMaterial = MaterialManager.SetStencil(m_fontMaterial, m_stencilID);
                }
                else if (m_maskingMaterial == null)
                {
                    m_maskingMaterial = MaterialManager.GetStencilMaterial(m_baseMaterial, m_stencilID);
                    m_sharedMaterial  = m_maskingMaterial;
                }
                else if (m_maskingMaterial.GetInt(ShaderUtilities.ID_StencilID) != m_stencilID || m_isNewBaseMaterial)
                {
                    MaterialManager.ReleaseStencilMaterial(m_maskingMaterial);

                    m_maskingMaterial = MaterialManager.GetStencilMaterial(m_baseMaterial, m_stencilID);
                    m_sharedMaterial  = m_maskingMaterial;
                }


                if (m_isMaskingEnabled)
                {
                    EnableMasking();
                }

                //Debug.Log("Masking Enabled. Assigning " + m_maskingMaterial.name + " with ID " + m_maskingMaterial.GetInstanceID());
            }

            m_uiRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.mainTexture);
            m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
            //m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial);
        }
Exemplo n.º 10
0
        public static float GetPadding(Material[] materials, bool enableExtraPadding, bool isBold)
        {
            if (!ShaderUtilities.isInitialized)
            {
                ShaderUtilities.GetShaderPropertyIDs();
            }
            if (materials == null)
            {
                return(0f);
            }
            int num = (!enableExtraPadding) ? 0 : 4;

            if (!materials[0].HasProperty(ShaderUtilities.ID_GradientScale))
            {
                return((float)num);
            }
            Vector4 a    = Vector4.zero;
            Vector4 zero = Vector4.zero;
            float   num2 = 0f;
            float   num3 = 0f;
            float   num4 = 0f;
            float   num5 = 0f;
            float   num6 = 0f;
            float   num7 = 0f;
            float   num8 = 0f;
            float   num9 = 0f;
            float   num10;

            for (int i = 0; i < materials.Length; i++)
            {
                ShaderUtilities.UpdateShaderRatios(materials[i]);
                string[] shaderKeywords = materials[i].shaderKeywords;
                if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_A))
                {
                    num5 = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_A);
                }
                if (materials[i].HasProperty(ShaderUtilities.ID_FaceDilate))
                {
                    num2 = materials[i].GetFloat(ShaderUtilities.ID_FaceDilate) * num5;
                }
                if (materials[i].HasProperty(ShaderUtilities.ID_OutlineSoftness))
                {
                    num3 = materials[i].GetFloat(ShaderUtilities.ID_OutlineSoftness) * num5;
                }
                if (materials[i].HasProperty(ShaderUtilities.ID_OutlineWidth))
                {
                    num4 = materials[i].GetFloat(ShaderUtilities.ID_OutlineWidth) * num5;
                }
                num10 = num4 + num3 + num2;
                if (materials[i].HasProperty(ShaderUtilities.ID_GlowOffset) && shaderKeywords.Contains(ShaderUtilities.Keyword_Glow))
                {
                    if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_B))
                    {
                        num6 = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_B);
                    }
                    num8 = materials[i].GetFloat(ShaderUtilities.ID_GlowOffset) * num6;
                    num9 = materials[i].GetFloat(ShaderUtilities.ID_GlowOuter) * num6;
                }
                num10 = Mathf.Max(num10, num2 + num8 + num9);
                if (materials[i].HasProperty(ShaderUtilities.ID_UnderlaySoftness) && shaderKeywords.Contains(ShaderUtilities.Keyword_Underlay))
                {
                    if (materials[i].HasProperty(ShaderUtilities.ID_ScaleRatio_C))
                    {
                        num7 = materials[i].GetFloat(ShaderUtilities.ID_ScaleRatio_C);
                    }
                    float num11 = materials[i].GetFloat(ShaderUtilities.ID_UnderlayOffsetX) * num7;
                    float num12 = materials[i].GetFloat(ShaderUtilities.ID_UnderlayOffsetY) * num7;
                    float num13 = materials[i].GetFloat(ShaderUtilities.ID_UnderlayDilate) * num7;
                    float num14 = materials[i].GetFloat(ShaderUtilities.ID_UnderlaySoftness) * num7;
                    a.x = Mathf.Max(a.x, num2 + num13 + num14 - num11);
                    a.y = Mathf.Max(a.y, num2 + num13 + num14 - num12);
                    a.z = Mathf.Max(a.z, num2 + num13 + num14 + num11);
                    a.w = Mathf.Max(a.w, num2 + num13 + num14 + num12);
                }
                a.x    = Mathf.Max(a.x, num10);
                a.y    = Mathf.Max(a.y, num10);
                a.z    = Mathf.Max(a.z, num10);
                a.w    = Mathf.Max(a.w, num10);
                a.x   += (float)num;
                a.y   += (float)num;
                a.z   += (float)num;
                a.w   += (float)num;
                a.x    = Mathf.Min(a.x, 1f);
                a.y    = Mathf.Min(a.y, 1f);
                a.z    = Mathf.Min(a.z, 1f);
                a.w    = Mathf.Min(a.w, 1f);
                zero.x = ((zero.x >= a.x) ? zero.x : a.x);
                zero.y = ((zero.y >= a.y) ? zero.y : a.y);
                zero.z = ((zero.z >= a.z) ? zero.z : a.z);
                zero.w = ((zero.w >= a.w) ? zero.w : a.w);
            }
            float @float = materials[0].GetFloat(ShaderUtilities.ID_GradientScale);

            a    *= @float;
            num10 = Mathf.Max(a.x, a.y);
            num10 = Mathf.Max(a.z, num10);
            num10 = Mathf.Max(a.w, num10);
            return(num10 + 0.25f);
        }
Exemplo n.º 11
0
 static ShaderUtilities()
 {
     ShaderUtilities.GetShaderPropertyIDs();
 }
Exemplo n.º 12
0
        //public override Material defaultMaterial
        //{
        //    get { Debug.Log("Default Material called."); return m_sharedMaterial; }
        //}


        //public bool MaskEnabled()
        //{
        //    Debug.Log("MaskEnabled() called.");
        //    return true;
        //}


        public void ParentMaskStateChanged()
        {
            //Debug.Log("***** PARENT MASK STATE CHANGED *****");
            if (m_fontAsset == null)
            {
                return;
            }

            m_stencilID = MaterialManager.GetStencilID(gameObject);

            if (!m_isAwake)
            {
                return;
            }

            if (m_stencilID == 0)
            {
                if (m_maskingMaterial != null)
                {
                    MaterialManager.ReleaseStencilMaterial(m_maskingMaterial);
                    m_maskingMaterial = null;

                    m_sharedMaterial = m_baseMaterial;
                }
                else if (m_fontMaterial != null)
                {
                    m_sharedMaterial = MaterialManager.SetStencil(m_fontMaterial, 0);
                }
            }
            else
            {
                ShaderUtilities.GetShaderPropertyIDs();

                if (m_fontMaterial != null)
                {
                    m_sharedMaterial = MaterialManager.SetStencil(m_fontMaterial, m_stencilID);
                }
                else if (m_maskingMaterial == null)
                {
                    m_maskingMaterial = MaterialManager.GetStencilMaterial(m_baseMaterial, m_stencilID);
                    m_sharedMaterial  = m_maskingMaterial;
                }
                else if (m_maskingMaterial.GetInt(ShaderUtilities.ID_StencilID) != m_stencilID || m_isNewBaseMaterial)
                {
                    MaterialManager.ReleaseStencilMaterial(m_maskingMaterial);

                    m_maskingMaterial = MaterialManager.GetStencilMaterial(m_baseMaterial, m_stencilID);
                    m_sharedMaterial  = m_maskingMaterial;
                }


                if (m_isMaskingEnabled)
                {
                    EnableMasking();
                }

                //Debug.Log("Masking Enabled. Assigning " + m_maskingMaterial.name + " with ID " + m_maskingMaterial.GetInstanceID());
            }

            m_uiRenderer.SetMaterial(m_sharedMaterial, null);
            m_padding          = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
            m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial);
        }