Ejemplo n.º 1
0
        /// <summary>
        /// See IMaterialModifier.GetModifiedMaterial
        /// </summary>
        public virtual Material GetModifiedMaterial(Material baseMaterial)
        {
            var toUse = baseMaterial;

            if (m_ShouldRecalculateStencil)
            {
                var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
                m_StencilValue             = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : 0;
                m_ShouldRecalculateStencil = false;
            }

            // if we have a enabled Mask component then it will
            // generate the mask material. This is an optimisation
            // it adds some coupling between components though :(
            Mask maskComponent = GetComponent <Mask>();

            if (m_StencilValue > 0 && (maskComponent == null || !maskComponent.IsActive()))
            {
                var maskMat = StencilMaterial.Add(toUse, (1 << m_StencilValue) - 1, StencilOp.Keep,
                                                  CompareFunction.Equal, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0);
                StencilMaterial.Remove(m_MaskMaterial);
                m_MaskMaterial = maskMat;
                toUse          = m_MaskMaterial;
            }

            return(toUse);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// See IMaterialModifier.GetModifiedMaterial
        /// </summary>
        public virtual Material GetModifiedMaterial(Material baseMaterial)
        {
            var toUse = baseMaterial;

            if (m_ShouldRecalculateStencil)
            {
                var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
                m_StencilValue             = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : 0;
                m_ShouldRecalculateStencil = false;
            }

            // if we have a enabled Mask component then it will
            // generate the mask material. This is an optimisation
            // it adds some coupling between components though :(

            // StencilMaterial.Add(...)方法向传入的材质添加了一些模板信息(即后边的一长串参数),
            // 返回设置好了模板参数的新的材质(StencilMaterial内部做了一些优化,
            // 对于相同的模板参数会共用同一个材质对象)这个m_StencilValue通过一个简单的变换1 << m_StencilValue) - 1得到一个新的整数值,
            // 这个值在后续的使用中称为stencilID,在StencilMaterial.Add(...)中我们看到有以下代码:

            /*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);
             * 对应的,在UI默认的shader中有:
             * Stencil
             * {
             *  Ref [_Stencil]
             *  Comp [_StencilComp]
             *  Pass [_StencilOp]
             *  ReadMask [_StencilReadMask]
             *  WriteMask [_StencilWriteMask]
             * }
             *
             * ColorMask [_ColorMask]*/
            Mask maskComponent = GetComponent <Mask>();

            if (m_StencilValue > 0 && (maskComponent == null || !maskComponent.IsActive()))
            {
                var maskMat = StencilMaterial.Add(toUse,
                                                  (1 << m_StencilValue) - 1,
                                                  StencilOp.Keep,
                                                  CompareFunction.Equal,
                                                  ColorWriteMask.All,
                                                  (1 << m_StencilValue) - 1,
                                                  0);
                StencilMaterial.Remove(m_MaskMaterial);
                m_MaskMaterial = maskMat;
                toUse          = m_MaskMaterial;
            }
            return(toUse);
        }
Ejemplo n.º 3
0
        public virtual Material GetModifiedMaterial(Material baseMaterial)
        {
            Material material = baseMaterial;

            if (this.m_ShouldRecalculateStencil)
            {
                Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(base.transform);
                this.m_StencilValue             = ((!this.maskable) ? 0 : MaskUtilities.GetStencilDepth(base.transform, stopAfter));
                this.m_ShouldRecalculateStencil = false;
            }
            Mask component = base.GetComponent <Mask>();

            if (this.m_StencilValue > 0 && (component == null || !component.IsActive()))
            {
                Material maskMaterial = StencilMaterial.Add(material, (1 << this.m_StencilValue) - 1, StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, (1 << this.m_StencilValue) - 1, 0);
                StencilMaterial.Remove(this.m_MaskMaterial);
                this.m_MaskMaterial = maskMaterial;
                material            = this.m_MaskMaterial;
            }
            return(material);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// <para>See IMaterialModifier.GetModifiedMaterial.</para>
        /// </summary>
        /// <param name="baseMaterial"></param>
        public virtual Material GetModifiedMaterial(Material baseMaterial)
        {
            Material baseMat = baseMaterial;

            if (this.m_ShouldRecalculateStencil)
            {
                Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(base.transform);
                this.m_StencilValue             = !this.maskable ? 0 : MaskUtilities.GetStencilDepth(base.transform, stopAfter);
                this.m_ShouldRecalculateStencil = false;
            }
            Mask component = base.GetComponent <Mask>();

            if ((this.m_StencilValue <= 0) || ((component != null) && component.IsActive()))
            {
                return(baseMat);
            }
            Material material2 = StencilMaterial.Add(baseMat, (((int)1) << this.m_StencilValue) - 1, StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, (((int)1) << this.m_StencilValue) - 1, 0);

            StencilMaterial.Remove(this.m_MaskMaterial);
            this.m_MaskMaterial = material2;
            return(this.m_MaskMaterial);
        }