/// <summary>
        /// Add a new material using the specified base and stencil ID.
        /// </summary>
        public static Material Add(Material baseMat, int stencilID)
        {
            if (stencilID <= 0 || baseMat == null)
                return null;

            if (!baseMat.HasProperty("_Stencil"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have stencil properties", baseMat);
                return null;
            }

            for (int i = 0; i < m_List.Count; ++i)
            {
                MatEntry ent = m_List[i];

                if (ent.baseMat == baseMat && ent.stencilID == stencilID)
                {
                    ++ent.count;
                    return ent.customMat;
                }
            }

            var newEnt = new MatEntry();
            newEnt.count = 1;
            newEnt.baseMat = baseMat;
            newEnt.customMat = new Material(baseMat);
            newEnt.customMat.name = "Stencil " + stencilID + " (" + baseMat.name + ")";
            newEnt.customMat.hideFlags = HideFlags.HideAndDontSave;
            newEnt.stencilID = stencilID;

            if (baseMat.HasProperty("_StencilComp"))
                newEnt.customMat.SetInt("_StencilComp", (int)CompareFunction.Equal);

            newEnt.customMat.SetInt("_Stencil", stencilID);
            m_List.Add(newEnt);
            return newEnt.customMat;
        }
 public static Material Add(Material baseMat, int stencilID, StencilOp operation, CompareFunction compareFunction, ColorWriteMask colorWriteMask, int readMask, int writeMask)
 {
     if (((stencilID <= 0) && (colorWriteMask == ColorWriteMask.All)) || (baseMat == null))
     {
         return baseMat;
     }
     if (!baseMat.HasProperty("_Stencil"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _Stencil property", baseMat);
         return baseMat;
     }
     if (!baseMat.HasProperty("_StencilOp"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilOp property", baseMat);
         return baseMat;
     }
     if (!baseMat.HasProperty("_StencilComp"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilComp property", baseMat);
         return baseMat;
     }
     if (!baseMat.HasProperty("_StencilReadMask"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilReadMask property", baseMat);
         return baseMat;
     }
     if (!baseMat.HasProperty("_StencilReadMask"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilWriteMask property", baseMat);
         return baseMat;
     }
     if (!baseMat.HasProperty("_ColorMask"))
     {
         Debug.LogWarning("Material " + baseMat.name + " doesn't have _ColorMask property", baseMat);
         return baseMat;
     }
     for (int i = 0; i < m_List.Count; i++)
     {
         MatEntry entry = m_List[i];
         if ((((entry.baseMat == baseMat) && (entry.stencilId == stencilID)) && ((entry.operation == operation) && (entry.compareFunction == compareFunction))) && (((entry.readMask == readMask) && (entry.writeMask == writeMask)) && (entry.colorMask == colorWriteMask)))
         {
             entry.count++;
             return entry.customMat;
         }
     }
     MatEntry item = new MatEntry {
         count = 1,
         baseMat = baseMat,
         customMat = new Material(baseMat)
     };
     item.customMat.hideFlags = HideFlags.HideAndDontSave;
     item.stencilId = stencilID;
     item.operation = operation;
     item.compareFunction = compareFunction;
     item.readMask = readMask;
     item.writeMask = writeMask;
     item.colorMask = colorWriteMask;
     item.useAlphaClip = (operation != StencilOp.Keep) && (writeMask > 0);
     item.customMat.name = string.Format("Stencil Id:{0}, Op:{1}, Comp:{2}, WriteMask:{3}, ReadMask:{4}, ColorMask:{5} AlphaClip:{6} ({7})", new object[] { stencilID, operation, compareFunction, writeMask, readMask, colorWriteMask, item.useAlphaClip, baseMat.name });
     item.customMat.SetInt("_Stencil", stencilID);
     item.customMat.SetInt("_StencilOp", (int) operation);
     item.customMat.SetInt("_StencilComp", (int) compareFunction);
     item.customMat.SetInt("_StencilReadMask", readMask);
     item.customMat.SetInt("_StencilWriteMask", writeMask);
     item.customMat.SetInt("_ColorMask", (int) colorWriteMask);
     if (item.customMat.HasProperty("_UseAlphaClip"))
     {
         item.customMat.SetInt("_UseAlphaClip", !item.useAlphaClip ? 0 : 1);
     }
     if (item.useAlphaClip)
     {
         item.customMat.EnableKeyword("UNITY_UI_ALPHACLIP");
     }
     else
     {
         item.customMat.DisableKeyword("UNITY_UI_ALPHACLIP");
     }
     m_List.Add(item);
     return item.customMat;
 }
Beispiel #3
0
        /// <summary>
        /// Add a new material using the specified base and stencil ID.
        /// </summary>
        public static Material Add(Material baseMat, int stencilID, StencilOp operation, CompareFunction compareFunction, ColorWriteMask colorWriteMask, int readMask, int writeMask)
        {
            if ((stencilID <= 0 && colorWriteMask == ColorWriteMask.All) || baseMat == null)
                return baseMat;

            if (!baseMat.HasProperty("_Stencil"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _Stencil property", baseMat);
                return baseMat;
            }
            if (!baseMat.HasProperty("_StencilOp"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilOp property", baseMat);
                return baseMat;
            }
            if (!baseMat.HasProperty("_StencilComp"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilComp property", baseMat);
                return baseMat;
            }
            if (!baseMat.HasProperty("_StencilReadMask"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilReadMask property", baseMat);
                return baseMat;
            }
            if (!baseMat.HasProperty("_StencilReadMask"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _StencilWriteMask property", baseMat);
                return baseMat;
            }
            if (!baseMat.HasProperty("_ColorMask"))
            {
                Debug.LogWarning("Material " + baseMat.name + " doesn't have _ColorMask property", baseMat);
                return baseMat;
            }

            for (int i = 0; i < m_List.Count; ++i)
            {
                MatEntry ent = m_List[i];

                if (ent.baseMat == baseMat
                    && ent.stencilId == stencilID
                    && ent.operation == operation
                    && ent.compareFunction == compareFunction
                    && ent.readMask == readMask
                    && ent.writeMask == writeMask
                    && ent.colorMask == colorWriteMask)
                {
                    ++ent.count;
                    return ent.customMat;
                }
            }

            var newEnt = new MatEntry();
            newEnt.count = 1;
            newEnt.baseMat = baseMat;
            newEnt.customMat = new Material(baseMat);
            newEnt.customMat.hideFlags = HideFlags.HideAndDontSave;
            newEnt.stencilId = stencilID;
            newEnt.operation = operation;
            newEnt.compareFunction = compareFunction;
            newEnt.readMask = readMask;
            newEnt.writeMask = writeMask;
            newEnt.colorMask = colorWriteMask;
            newEnt.useAlphaClip = operation != StencilOp.Keep && writeMask > 0;

            newEnt.customMat.name = string.Format("Stencil Id:{0}, Op:{1}, Comp:{2}, WriteMask:{3}, ReadMask:{4}, ColorMask:{5} AlphaClip:{6} ({7})", stencilID, operation, compareFunction, writeMask, readMask, colorWriteMask, newEnt.useAlphaClip, baseMat.name);

            newEnt.customMat.SetInt("_Stencil", stencilID);
            newEnt.customMat.SetInt("_StencilOp", (int)operation);
            newEnt.customMat.SetInt("_StencilComp", (int)compareFunction);
            newEnt.customMat.SetInt("_StencilReadMask", readMask);
            newEnt.customMat.SetInt("_StencilWriteMask", writeMask);
            newEnt.customMat.SetInt("_ColorMask", (int)colorWriteMask);

            // left for backwards compatability
            if (newEnt.customMat.HasProperty("_UseAlphaClip"))
                newEnt.customMat.SetInt("_UseAlphaClip", newEnt.useAlphaClip ? 1 : 0);

            if (newEnt.useAlphaClip)
                newEnt.customMat.EnableKeyword("UNITY_UI_ALPHACLIP");
            else
                newEnt.customMat.DisableKeyword("UNITY_UI_ALPHACLIP");

            m_List.Add(newEnt);
            return newEnt.customMat;
        }