当前位置:网站首页>Ugui source code analysis - Mask
Ugui source code analysis - Mask
2022-07-24 02:48:00 【Hello Bug.】
One : Preface
Mask Is the mask component , Inherited from UIBehaviour、ICanvasRaycastFilter、IMaterialModifier
The shape of its mask is made of Graphic decision , So you can use different Graphic Realize masks of different shapes
Its implementation principle is to use StencilBuffer( Template cache )
stay Mask Of GetModifiedMaterial Method to generate a material that will StencilBuffer The value is set to a specific value
stay MaskableGraphic Of GetModifiedMaterial In the method Make a material , This material will be taken out when rendering StencilBuffer Value , Determine whether it is equal to a specific value , Render only if they are equal
Two : The principle of template caching
Mask Components are assigned to parents and children UI A special material , This material will give Image Each pixel of is marked and placed in one called Stencil Buffer In the cache , First, set the mark of each pixel of the parent to a specific value , Sub level UI When rendering, you will check whether the specific value in the coincident area is equal to this specific value , Render if equal , Otherwise, no rendering
Template caching steps : Here are two pictures , The parent object is a green picture , The sub object is a red picture
If you don't add Mask The component first draws the color of each pixel of the green picture on the screen , Then draw each pixel color of red on the screen , Red completely covers green in the overlapping area
If you add Mask Components , In the first frame of rendering, draw the color of each pixel of the green picture on the screen at the same time Set the stencil buffer Value is set to 1, Next, draw a red picture , Before drawing, draw the stencil buffer It's worth taking out , If it is 1 Continue to draw if it is 0 It doesn't draw , Thus, the mask effect is realized 


3、 ... and : Source code analysis
——OnEnable
call NotifyStencilStateChanged Method to reset all mask materials , Finally called Graphic Class SetMaterialDirty Method to update the material
——OnDisable
from StencilMaterial Remove m_MaskMaterial and m_UnmaskMaterial Juxtapose to null , call NotifyStencilStateChanged Method to reset all mask materials , Finally called Graphic Class SetMaterialDirty Method to update the material
——GetModifiedMaterial
Inherited from IMaterialModifier Interface ,MaskableGraphic It also inherits this interface , Modify the template buffer value through this new material setting
It's divided into two parts
The first part : There is only one processing Mask The situation of
First, find the root canvas of the object , Then calculate the distance between itself and the root canvas Mask The number of , If there is only one oneself Mask be stencilDepth by 0,desiredStencilBit Then for 1,desiredStencilBit Represents the reference value actually to be written to the template buffer ,
At this point through StencilMaterial.Add Get a new material (StencilOp.Replace-2,CompareFunction.Always-8) And return this material to , At this time, the reference value of the parent object template buffer is 1
Then get a new material (StencilOp.Zero-1,CompareFunction.Always-8), This material is actually used to clear the template buffer , To avoid affecting subsequent rendering
The second part : Handle nesting Mask The situation of
Similar to the first part , Only two parameters will be passed in at the end :readMask( Read mask ) and writeMask( Write mask )
It turns on when setting the material UNITY_UI_ALPHACLIP, This also explains why Mask On the object Graphic Corresponding Alpha Value is set to 0, All sub objects are not displayed , stay UI-Default.shader There are the following operations in : If the transparency is too small, it will be cut
Four : Hole digging mask
I understand UGUI Of Mask After realizing the principle , We can set CompareFunction Parameter implements reverse mask ,UGUI Of Image Is the designated area display ,HoleImage Yes, the specified area is not displayed
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
/// <summary>
/// Hole digging Image
/// </summary>
public class HoleImage : Image
{
public override 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 (m_StencilValue > 0 && !isMaskingGraphic)
{
var maskMat = StencilMaterial.Add(toUse, (1 << m_StencilValue) - 1, StencilOp.Keep, CompareFunction.NotEqual, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0);
StencilMaterial.Remove(m_MaskMaterial);
m_MaskMaterial = maskMat;
toUse = m_MaskMaterial;
}
return toUse;
}
}边栏推荐
- Only beautiful ones can be opened
- Attack and defense world web practice area (view_source, get_post, robots)
- "Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture
- 日常杂谈(一)
- 22 -- range and of binary search tree
- 22 -- 二叉搜索树的范围和
- Dynamic programming-01 knapsack problem
- PMP preparation experience | good habits, good process, good results
- 通用机环境下安全版单机数据库使用非root用户管理的解决方案
- Summary of problems encountered in the development process in July
猜你喜欢

SSM的技术论坛含前后台

Attack and defense world web practice area (weak_auth, simple_php, xff_referer)

ssm的求职招聘系统兼职应聘求职

TP5 framework link promotion project

攻防世界WEB练习区(view_source、get_post、robots)

Attack and defense world web practice area (backup, cookie, disabled_button)

动态规划-01背包问题

攻防世界WEB练习区(backup、cookie、disabled_button)

Soft test --- fundamentals of programming language (Part 1)

redis数据类型概念
随机推荐
TCP connection principle
Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
如何获取步态能量图gei
Mysql database, sorting and single line processing functions
QT display Chinese garbled code
I'm a novice. I heard that there is a breakeven financial product in opening an account. What is it?
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
软考---程序设计语言基础(上)
Jina AI and datawhale jointly launched a learning project!
Detailed vector
JpaRepository扩展接口
动态规划-01背包问题
Unscramble the category and application principle of robot vision
【英雄哥七月集训】第 23天: 字典树
c语言小练习
Customize the default width and height of kindeditor rich text
【HDLBits 刷题】Verilog Language(2)Vectors 部分
Do securities companies really have principal guaranteed financial products?
22 -- 二叉搜索树的范围和
Essential skills for programmers -- breakpoint debugging (idea version)
