2015-12-02 22:36:13 +08:00
|
|
|
|
/// Credit NemoKrad (aka Charles Humphrey)
|
|
|
|
|
/// Sourced from - http://www.randomchaos.co.uk/SoftAlphaUIMask.aspx
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-12-02 07:29:01 +08:00
|
|
|
|
{
|
2015-12-02 22:36:13 +08:00
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
[AddComponentMenu("UI/Effects/Extensions/SoftMaskScript")]
|
|
|
|
|
public class SoftMaskScript : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
Material mat;
|
|
|
|
|
Canvas canvas;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
[Tooltip("The area that is to be used as the container.")]
|
|
|
|
|
public RectTransform MaskArea;
|
|
|
|
|
RectTransform myRect;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-11 05:37:51 +08:00
|
|
|
|
[Tooltip("A Rect Transform that can be used to scale and move the mask - Does not apply to Text UI Components being masked")]
|
|
|
|
|
public RectTransform maskScalingRect;
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
[Tooltip("Texture to be used to do the soft alpha")]
|
|
|
|
|
public Texture AlphaMask;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
[Tooltip("At what point to apply the alpha min range 0-1")]
|
|
|
|
|
[Range(0, 1)]
|
|
|
|
|
public float CutOff = 0;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
[Tooltip("Implement a hard blend based on the Cutoff")]
|
|
|
|
|
public bool HardBlend = false;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-11 05:37:51 +08:00
|
|
|
|
[Tooltip("Flip the masks alpha value")]
|
|
|
|
|
public bool FlipAlphaMask = false;
|
|
|
|
|
|
2016-05-17 20:26:50 +08:00
|
|
|
|
[Tooltip("If Mask Scaling Rect is given and this value is true, the area around the mask will not be clipped")]
|
2015-12-16 06:43:35 +08:00
|
|
|
|
public bool DontClipMaskScalingRect = false;
|
|
|
|
|
|
2016-05-17 20:26:50 +08:00
|
|
|
|
[Tooltip("If set to true, this mask is applied to all child Text and Graphic objects belonging to this object.")]
|
|
|
|
|
public bool CascadeToALLChildren;
|
2015-12-02 22:36:13 +08:00
|
|
|
|
Vector3[] worldCorners;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
Vector2 AlphaUV;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
Vector2 min;
|
|
|
|
|
Vector2 max = Vector2.one;
|
|
|
|
|
Vector2 p;
|
|
|
|
|
Vector2 siz;
|
2016-05-17 20:26:50 +08:00
|
|
|
|
Vector2 tp = new Vector2(.5f, .5f);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2016-05-17 20:26:50 +08:00
|
|
|
|
|
|
|
|
|
bool MaterialNotSupported; // UI items like toggles, we can stil lcascade down to them though :)
|
2015-12-02 22:36:13 +08:00
|
|
|
|
Rect maskRect;
|
|
|
|
|
Rect contentRect;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
Vector2 centre;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
bool isText = false;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start()
|
2015-12-02 07:29:01 +08:00
|
|
|
|
{
|
2015-12-02 22:36:13 +08:00
|
|
|
|
myRect = GetComponent<RectTransform>();
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
if (!MaskArea)
|
|
|
|
|
{
|
|
|
|
|
MaskArea = myRect;
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
if (GetComponent<Graphic>() != null)
|
|
|
|
|
{
|
|
|
|
|
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
|
|
|
|
|
GetComponent<Graphic>().material = mat;
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
if (GetComponent<Text>())
|
|
|
|
|
{
|
|
|
|
|
isText = true;
|
|
|
|
|
mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText"));
|
|
|
|
|
GetComponent<Text>().material = mat;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
GetCanvas();
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// For some reason, having the mask control on the parent and disabled stops the mouse interacting
|
|
|
|
|
// with the texture layer that is not visible.. Not needed for the Image.
|
2016-05-17 20:26:50 +08:00
|
|
|
|
if (transform.parent.GetComponent<Button>() == null && transform.parent.GetComponent<Mask>() == null)
|
2015-12-02 22:36:13 +08:00
|
|
|
|
transform.parent.gameObject.AddComponent<Mask>();
|
|
|
|
|
|
2016-05-17 20:26:50 +08:00
|
|
|
|
if (transform.parent.GetComponent<Mask>() != null)
|
|
|
|
|
transform.parent.GetComponent<Mask>().enabled = false;
|
|
|
|
|
}
|
|
|
|
|
if (CascadeToALLChildren)
|
|
|
|
|
{
|
|
|
|
|
for (int c = 0; c < transform.childCount; c++)
|
|
|
|
|
{
|
|
|
|
|
SetSAM(transform.GetChild(c));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaterialNotSupported = mat == null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetSAM(Transform t)
|
|
|
|
|
{
|
|
|
|
|
SoftMaskScript thisSam = t.gameObject.GetComponent<SoftMaskScript>();
|
|
|
|
|
if (thisSam == null)
|
|
|
|
|
{
|
|
|
|
|
thisSam = t.gameObject.AddComponent<SoftMaskScript>();
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
}
|
2016-05-17 20:26:50 +08:00
|
|
|
|
thisSam.MaskArea = MaskArea;
|
|
|
|
|
thisSam.AlphaMask = AlphaMask;
|
|
|
|
|
thisSam.CutOff = CutOff;
|
|
|
|
|
thisSam.HardBlend = HardBlend;
|
|
|
|
|
thisSam.FlipAlphaMask = FlipAlphaMask;
|
|
|
|
|
thisSam.maskScalingRect = maskScalingRect;
|
|
|
|
|
thisSam.DontClipMaskScalingRect = DontClipMaskScalingRect;
|
|
|
|
|
thisSam.CascadeToALLChildren = CascadeToALLChildren;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
void GetCanvas()
|
|
|
|
|
{
|
|
|
|
|
Transform t = transform;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
int lvlLimit = 100;
|
|
|
|
|
int lvl = 0;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
while (canvas == null && lvl < lvlLimit)
|
|
|
|
|
{
|
|
|
|
|
canvas = t.gameObject.GetComponent<Canvas>();
|
|
|
|
|
if (canvas == null)
|
2016-05-17 20:26:50 +08:00
|
|
|
|
{
|
|
|
|
|
t = t.parent;
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
lvl++;
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
SetMask();
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
void SetMask()
|
2015-12-02 07:29:01 +08:00
|
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
|
if (MaterialNotSupported)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Get the two rectangle areas
|
|
|
|
|
maskRect = MaskArea.rect;
|
|
|
|
|
contentRect = myRect.rect;
|
|
|
|
|
|
|
|
|
|
if (isText) // Need to do our calculations in world for Text
|
2015-12-02 07:29:01 +08:00
|
|
|
|
{
|
2015-12-11 05:37:51 +08:00
|
|
|
|
maskScalingRect = null;
|
2015-12-02 22:36:13 +08:00
|
|
|
|
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay && Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
p = canvas.transform.InverseTransformPoint(MaskArea.transform.position);
|
|
|
|
|
siz = new Vector2(maskRect.width, maskRect.height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
worldCorners = new Vector3[4];
|
|
|
|
|
MaskArea.GetWorldCorners(worldCorners);
|
|
|
|
|
siz = (worldCorners[2] - worldCorners[0]);
|
|
|
|
|
p = MaskArea.transform.position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
min = p - (new Vector2(siz.x, siz.y) * .5f);
|
|
|
|
|
max = p + (new Vector2(siz.x, siz.y) * .5f);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
}
|
2015-12-02 22:36:13 +08:00
|
|
|
|
else // Need to do our calculations in tex space for Image.
|
2015-12-02 07:29:01 +08:00
|
|
|
|
{
|
2015-12-11 05:37:51 +08:00
|
|
|
|
if (maskScalingRect != null)
|
|
|
|
|
{
|
|
|
|
|
maskRect = maskScalingRect.rect;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Get the centre offset
|
2015-12-11 05:37:51 +08:00
|
|
|
|
if (maskScalingRect != null)
|
|
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
|
centre = myRect.transform.InverseTransformPoint(maskScalingRect.transform.TransformPoint(maskScalingRect.rect.center));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
centre = myRect.transform.InverseTransformPoint(MaskArea.transform.TransformPoint(MaskArea.rect.center));
|
2015-12-11 05:37:51 +08:00
|
|
|
|
}
|
2016-05-17 20:26:50 +08:00
|
|
|
|
centre += (Vector2)myRect.transform.InverseTransformPoint(myRect.transform.position) - myRect.rect.center;
|
2015-12-11 05:37:51 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Set the scale for mapping texcoords mask
|
|
|
|
|
AlphaUV = new Vector2(maskRect.width / contentRect.width, maskRect.height / contentRect.height);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// set my min and max to the centre offest
|
|
|
|
|
min = centre;
|
|
|
|
|
max = min;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
siz = new Vector2(maskRect.width, maskRect.height) * .5f;
|
|
|
|
|
// Move them out to the min max extreams
|
|
|
|
|
min -= siz;
|
|
|
|
|
max += siz;
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Now move these into texture space. 0 - 1
|
2016-05-17 20:26:50 +08:00
|
|
|
|
min = new Vector2(min.x / contentRect.width, min.y / contentRect.height) + tp;
|
|
|
|
|
max = new Vector2(max.x / contentRect.width, max.y / contentRect.height) + tp;
|
2015-12-02 22:36:13 +08:00
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
mat.SetFloat("_HardBlend", HardBlend ? 1 : 0);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
// Pass the values to the shader
|
|
|
|
|
mat.SetVector("_Min", min);
|
|
|
|
|
mat.SetVector("_Max", max);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-11 05:37:51 +08:00
|
|
|
|
mat.SetInt("_FlipAlphaMask", FlipAlphaMask ? 1 : 0);
|
2016-05-17 20:26:50 +08:00
|
|
|
|
mat.SetTexture("_AlphaMask", AlphaMask);
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-16 06:43:35 +08:00
|
|
|
|
mat.SetInt("_NoOuterClip", DontClipMaskScalingRect && maskScalingRect != null ? 1 : 0);
|
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
if (!isText) // No mod needed for Text
|
2016-05-17 20:26:50 +08:00
|
|
|
|
{
|
2015-12-02 22:36:13 +08:00
|
|
|
|
mat.SetVector("_AlphaUV", AlphaUV);
|
2016-05-17 20:26:50 +08:00
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
|
2015-12-02 22:36:13 +08:00
|
|
|
|
mat.SetFloat("_CutOff", CutOff);
|
|
|
|
|
}
|
2015-12-02 07:29:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|