当前位置:网站首页>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;
}
}边栏推荐
- Is it safe to open an account for Xiaobai stock? Can I apply online?
- 7月开发过程中遇到的问题总结
- Symbol類型
- (6) Decorator extension [email protected] Principle of use
- Soft test --- fundamentals of programming language (Part 1)
- js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
- 中城院真的在帮助供应商解决问题吗?
- Causal learning open source project: from prediction to decision!
- 攻防世界WEB练习区(backup、cookie、disabled_button)
- 云原生讲解【扩展篇】
猜你喜欢

Skywalking distributed system application performance monitoring tool - upper

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

攻防世界WEB練習區(view_source、get_post、robots)

Make life full of happiness

Recommendation system topic | recommendation system architecture and single domain cross domain recall model

redis数据类型概念

自定义kindeditor富文本默认的宽高

To forge ahead on a new journey, the city chain science and technology carnival was grandly held in Xiamen

O3DE 的Lumberyard 游戏引擎

Soft test --- fundamentals of programming language (Part 1)
随机推荐
JpaRepository扩展接口
C language actual combat guessing game
Attack and defense world web practice area (webshell, command_execution, simple_js)
go errors
Attack and defense world web practice area (view_source, get_post, robots)
Fasterrcnn sample code test 1: make anchor_ generator = None
X Actual combat - Cloud Server
Recommendation system topic | recommendation system architecture and single domain cross domain recall model
Live800: there is nothing trivial about customer service. Don't let service destroy the reputation of the enterprise
Pyg uses messagepassing to build GCN to realize node classification
Lcd1602——斌哥51
[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
"Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture
Leetcode stack and queue questions
[brother hero July training] day 23: dictionary tree
Redis data type concept
rust allow dead_ code
Jina AI and datawhale jointly launched a learning project!
Tutoriel sur l'utilisation de la ligne de temps unitaire
(6) Decorator extension [email protected] Principle of use
