Tanoda
SoftMaskScript.cs
Go to the documentation of this file.
1
4
6{
7 [ExecuteInEditMode]
8 [AddComponentMenu("UI/Effects/Extensions/SoftMaskScript")]
9 public class SoftMaskScript : MonoBehaviour
10 {
11 Material mat;
12
13 Canvas cachedCanvas = null;
14 Transform cachedCanvasTransform = null;
15 readonly Vector3[] m_WorldCorners = new Vector3[4];
16 readonly Vector3[] m_CanvasCorners = new Vector3[4];
17
18 [Tooltip("The area that is to be used as the container.")]
19 public RectTransform MaskArea;
20
21 [Tooltip("Texture to be used to do the soft alpha")]
22 public Texture AlphaMask;
23
24 [Tooltip("At what point to apply the alpha min range 0-1")]
25 [Range(0, 1)]
26 public float CutOff = 0;
27
28 [Tooltip("Implement a hard blend based on the Cutoff")]
29 public bool HardBlend = false;
30
31 [Tooltip("Flip the masks alpha value")]
32 public bool FlipAlphaMask = false;
33
34 [Tooltip("If a different Mask Scaling Rect is given, and this value is true, the area around the mask will not be clipped")]
35 public bool DontClipMaskScalingRect = false;
36
37 Vector2 maskOffset = Vector2.zero;
38 Vector2 maskScale = Vector2.one;
39
40 // Use this for initialization
41 void Start()
42 {
43 if (MaskArea == null)
44 {
45 MaskArea = GetComponent<RectTransform>();
46 }
47
48 var text = GetComponent<Text>();
49 if (text != null)
50 {
51 mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
52 text.material = mat;
53 cachedCanvas = text.canvas;
54 cachedCanvasTransform = cachedCanvas.transform;
55 // For some reason, having the mask control on the parent and disabled stops the mouse interacting
56 // with the texture layer that is not visible.. Not needed for the Image.
57 if (transform.parent.GetComponent<Mask>() == null)
58 transform.parent.gameObject.AddComponent<Mask>();
59
60 transform.parent.GetComponent<Mask>().enabled = false;
61 return;
62 }
63
64 var graphic = GetComponent<Graphic>();
65 if (graphic != null)
66 {
67 mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
68 graphic.material = mat;
69 cachedCanvas = graphic.canvas;
70 cachedCanvasTransform = cachedCanvas.transform;
71 }
72 }
73
74 void Update()
75 {
76 if (cachedCanvas != null)
77 {
78 SetMask();
79 }
80 }
81
82 void SetMask()
83 {
84 var worldRect = GetCanvasRect();
85 var size = worldRect.size;
86 maskScale.Set(1.0f / size.x, 1.0f / size.y);
87 maskOffset = -worldRect.min;
88 maskOffset.Scale(maskScale);
89
90 mat.SetTextureOffset("_AlphaMask", maskOffset);
91 mat.SetTextureScale("_AlphaMask", maskScale);
92 mat.SetTexture("_AlphaMask", AlphaMask);
93
94 mat.SetFloat("_HardBlend", HardBlend ? 1 : 0);
95 mat.SetInt("_FlipAlphaMask", FlipAlphaMask ? 1 : 0);
96 mat.SetInt("_NoOuterClip", DontClipMaskScalingRect ? 1 : 0);
97 mat.SetFloat("_CutOff", CutOff);
98 }
99
100 public Rect GetCanvasRect()
101 {
102 if (cachedCanvas == null)
103 return new Rect();
104
105 MaskArea.GetWorldCorners(m_WorldCorners);
106 for (int i = 0; i < 4; ++i)
107 m_CanvasCorners[i] = cachedCanvasTransform.InverseTransformPoint(m_WorldCorners[i]);
108
109 return new Rect(m_CanvasCorners[0].x, m_CanvasCorners[0].y, m_CanvasCorners[2].x - m_CanvasCorners[0].x, m_CanvasCorners[2].y - m_CanvasCorners[0].y);
110 }
111 }
112}
Credit Erdener Gonenc - @PixelEnvision.