Exemplo n.º 1
0
        public Material GetModifiedMaterial(Material baseMaterial)
        {
            var targetMat = baseMaterial;

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

            var mask = this.GetComponent <NewMask>();

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


            return(targetMat);
        }
Exemplo n.º 2
0
        public Material GetModifiedMaterial(Material baseMaterial)
        {
            if (!MaskEnabled())
            {
                return(baseMaterial);
            }

            var rootSortCanvas = NewMaskUtil.FindRootSortOverrideCanvas(transform);
            var stencilDepth   = NewMaskUtil.GetStencilDepth(transform, rootSortCanvas);

            if (stencilDepth >= 8)
            {
                Debug.LogError("Attempting to use a stencil mask with depth > 8", gameObject);
                return(baseMaterial);
            }

            int desiredStencilBit = 1 << stencilDepth;


            if (desiredStencilBit == 1)
            {
                var maskMaterial = NewStencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always,
                                                          m_ShowMaskGraphic ? ColorWriteMask.All : 0);
                NewStencilMaterial.Remove(m_MaskMaterial);
                m_MaskMaterial = maskMaterial;

                var unmaskMaterial = NewStencilMaterial.Add(baseMaterial, 1, StencilOp.Zero, CompareFunction.Always, 0);
                NewStencilMaterial.Remove(m_UnmaskMaterial);
                m_UnmaskMaterial = unmaskMaterial;
                graphic.canvasRenderer.popMaterialCount = 1;
                graphic.canvasRenderer.SetPopMaterial(m_UnmaskMaterial, 0);

                return(m_MaskMaterial);
            }


            var maskMaterial2 = NewStencilMaterial.Add(baseMaterial, desiredStencilBit | (desiredStencilBit - 1),
                                                       StencilOp.Replace, CompareFunction.Equal, m_ShowMaskGraphic ? ColorWriteMask.All : 0,
                                                       desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));

            NewStencilMaterial.Remove(m_MaskMaterial);
            m_MaskMaterial = maskMaterial2;

            graphic.canvasRenderer.hasPopInstruction = true;
            var unmaskMaterial2 = NewStencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Replace,
                                                         CompareFunction.Equal, 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));

            NewStencilMaterial.Remove(m_UnmaskMaterial);
            m_UnmaskMaterial = unmaskMaterial2;
            graphic.canvasRenderer.popMaterialCount = 1;
            graphic.canvasRenderer.SetPopMaterial(m_UnmaskMaterial, 0);

            return(m_MaskMaterial);
        }