UGUI-Mask的底层原理

2021-08-15  本文已影响0人  小飞不会飞_

解释Mask底层的一篇文章:https://aillieo.cn/post/2018-08-02-unity-3d-ugui-source-code-13/

Unity源码中的一些类型介绍

每个Canvas对应一个CanvasData,有两个CanvasHandle(Push和Pop),每个CanvasHandle持有CanvasData,每个CanvasData保存有该Canvas的UIInstruction。


句柄.png

Unity内部渲染UI分为Push和Pop两种模式,可以理解为底层渲染Canvas有两个队列:Push和Pop,先Push后Pop。所以CanvasRenderer就有两种渲染模式。(CanvasRenderer.h)


push和pop.png
Canvas.AddRenderersToCanvas函数是把Renderer加入到Canvas渲染队列中的功能:(Canvas.cpp中的函数)
static void AddRenderersToCanvas(JobFence& batchesBuiltFence, const Transform* start, Canvas* toAdd, int& nestedDepth)
    {
        CanvasRenderer* renderer = start->QueryComponent<CanvasRenderer>();
        Canvas* canvas = NULL;

        if (renderer && renderer->IsActive())
        {
            // Find the canvas that the Renderer is associated with
            canvas = FindAncestorComponent<Canvas>(renderer->GetGameObject());
            Assert(canvas != NULL);

            // Attach it if it is THIS
            if (canvas == toAdd)
            {
                // Synchronizing with the UI batch generation jobs is required because the UI batch generation jobs use this data
                // The synchronization is delegated to the higher level calling code, assert here if the jobs haven't completed
                AssertFormatMsg(FenceHasBeenSynced(batchesBuiltFence), "%s", "UI batch generation jobs still running: please report this as a bug with a reproduction case");
                renderer->AttachToCanvas(batchesBuiltFence, toAdd, CanvasRenderer::kPushInstruction);
            }
            else // otherwise insert a nesting command
            {
//…………(省略多行
            }
        }
//…………(省略多行

        if (canvas == toAdd && renderer && renderer->GetHasPopInstruction() && renderer->IsActive())
        {
            // Synchronizing with the UI batch generation jobs is required because the UI batch generation jobs use this data
            // The synchronization is delegated to the higher level calling code, assert here if the jobs haven't completed
            AssertFormatMsg(FenceHasBeenSynced(batchesBuiltFence), "%s", "UI batch generation jobs still running: please report this as a bug with a reproduction case");
            renderer->AttachToCanvas(batchesBuiltFence, toAdd, CanvasRenderer::kPopInstruction);
        }
    }

这个函数大多是在CanvasRenderer出现(Enable)的时候被调用。目前UGUI中只有Mask用到Pop队列,其余组件都是在Push队列。

由此再来理解Mask实现原理就好多了,跟Mask相关的脚本有几个:

如下代码摘自CanvasRenderer.cs文件

public virtual Material GetModifiedMaterial(Material baseMaterial)
        {
//…………(省略多行

            //第一级Mask
            // if we are at the first level...
            // we want to destroy what is there
            if (desiredStencilBit == 1)
            {
                var maskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always, m_ShowMaskGraphic ? ColorWriteMask.All : 0);
                StencilMaterial.Remove(m_MaskMaterial);
                m_MaskMaterial = maskMaterial;

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

                return m_MaskMaterial;
            }

//…………(省略多行

            return m_MaskMaterial;
        }

该代码设置了mask和unmask,红字部分,unmask设置在Pop队列。

RectMask2D是另一个方式实现,把裁剪矩形传入到每个材质中。

上一篇下一篇

猜你喜欢

热点阅读