summaryrefslogtreecommitdiff
path: root/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-10-08 09:50:33 +0800
committerchai <chaifix@163.com>2020-10-08 09:50:33 +0800
commit00dae1bd426d892dff73a50f1c505afd1ac00a90 (patch)
tree5d75f8495406f5b8dd01595e3dd9216887996a34 /Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs
+init
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs')
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs166
1 files changed, 166 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs
new file mode 100644
index 0000000..39dc291
--- /dev/null
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask.cs
@@ -0,0 +1,166 @@
+using System;
+using UnityEngine;
+using UnityEngine.EventSystems;
+using UnityEngine.Rendering;
+using UnityEngine.Serialization;
+
+namespace UnityEngine.UI
+{
+ [AddComponentMenu("UI/Mask", 13)]
+ [ExecuteInEditMode]
+ [RequireComponent(typeof(RectTransform))]
+ [DisallowMultipleComponent]
+ public class Mask : UIBehaviour, ICanvasRaycastFilter, IMaterialModifier
+ {
+ [NonSerialized]
+ private RectTransform m_RectTransform;
+ public RectTransform rectTransform
+ {
+ get { return m_RectTransform ?? (m_RectTransform = GetComponent<RectTransform>()); }
+ }
+
+ [SerializeField]
+ [FormerlySerializedAs("m_ShowGraphic")]
+ private bool m_ShowMaskGraphic = true;
+ public bool showMaskGraphic
+ {
+ get { return m_ShowMaskGraphic; }
+ set
+ {
+ if (m_ShowMaskGraphic == value)
+ return;
+
+ m_ShowMaskGraphic = value;
+ if (graphic != null)
+ graphic.SetMaterialDirty();
+ }
+ }
+
+ [NonSerialized]
+ private Graphic m_Graphic;
+ public Graphic graphic
+ {
+ get { return m_Graphic ?? (m_Graphic = GetComponent<Graphic>()); }
+ }
+
+ [NonSerialized]
+ private Material m_MaskMaterial;
+
+ [NonSerialized]
+ private Material m_UnmaskMaterial;
+
+ protected Mask()
+ {}
+
+ public virtual bool MaskEnabled() { return IsActive() && graphic != null; }
+
+ [Obsolete("Not used anymore.")]
+ public virtual void OnSiblingGraphicEnabledDisabled() {}
+
+ protected override void OnEnable()
+ {
+ base.OnEnable();
+ if (graphic != null)
+ {
+ graphic.canvasRenderer.hasPopInstruction = true;
+ graphic.SetMaterialDirty();
+ }
+
+ MaskUtilities.NotifyStencilStateChanged(this);
+ }
+
+ protected override void OnDisable()
+ {
+ // we call base OnDisable first here
+ // as we need to have the IsActive return the
+ // correct value when we notify the children
+ // that the mask state has changed.
+ base.OnDisable();
+ if (graphic != null)
+ {
+ graphic.SetMaterialDirty();
+ graphic.canvasRenderer.hasPopInstruction = false;
+ graphic.canvasRenderer.popMaterialCount = 0;
+ }
+
+ StencilMaterial.Remove(m_MaskMaterial);
+ m_MaskMaterial = null;
+ StencilMaterial.Remove(m_UnmaskMaterial);
+ m_UnmaskMaterial = null;
+
+ MaskUtilities.NotifyStencilStateChanged(this);
+ }
+
+#if UNITY_EDITOR
+ protected override void OnValidate()
+ {
+ base.OnValidate();
+
+ if (!IsActive())
+ return;
+
+ if (graphic != null)
+ graphic.SetMaterialDirty();
+
+ MaskUtilities.NotifyStencilStateChanged(this);
+ }
+
+#endif
+
+ public virtual bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
+ {
+ if (!isActiveAndEnabled)
+ return true;
+
+ return RectTransformUtility.RectangleContainsScreenPoint(rectTransform, sp, eventCamera);
+ }
+
+ /// Stencil calculation time!
+ public virtual Material GetModifiedMaterial(Material baseMaterial)
+ {
+ if (!MaskEnabled())
+ return baseMaterial;
+
+ var rootSortCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
+ var stencilDepth = MaskUtilities.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 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;
+ }
+
+ //otherwise we need to be a bit smarter and set some read / write masks
+ var maskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit | (desiredStencilBit - 1), StencilOp.Replace, CompareFunction.Equal, m_ShowMaskGraphic ? ColorWriteMask.All : 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));
+ StencilMaterial.Remove(m_MaskMaterial);
+ m_MaskMaterial = maskMaterial2;
+
+ graphic.canvasRenderer.hasPopInstruction = true;
+ var unmaskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Replace, CompareFunction.Equal, 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));
+ StencilMaterial.Remove(m_UnmaskMaterial);
+ m_UnmaskMaterial = unmaskMaterial2;
+ graphic.canvasRenderer.popMaterialCount = 1;
+ graphic.canvasRenderer.SetPopMaterial(m_UnmaskMaterial, 0);
+
+ return m_MaskMaterial;
+ }
+ }
+}