parent
712d61de99
commit
5ed1e48cb2
|
@ -8,15 +8,12 @@ namespace UnityEngine.UI.Extensions
|
|||
public class SoftMaskScript : MonoBehaviour
|
||||
{
|
||||
Material mat;
|
||||
Canvas canvas;
|
||||
Canvas cachedCanvas= null;
|
||||
|
||||
[Tooltip("The area that is to be used as the container.")]
|
||||
public RectTransform MaskArea;
|
||||
RectTransform myRect;
|
||||
|
||||
[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;
|
||||
|
||||
[Tooltip("Texture to be used to do the soft alpha")]
|
||||
public Texture AlphaMask;
|
||||
|
||||
|
@ -30,27 +27,12 @@ namespace UnityEngine.UI.Extensions
|
|||
[Tooltip("Flip the masks alpha value")]
|
||||
public bool FlipAlphaMask = false;
|
||||
|
||||
[Tooltip("If Mask Scaling Rect is given and this value is true, the area around the mask will not be clipped")]
|
||||
[Tooltip("If Mask Scals Rect is given, and this value is true, the area around the mask will not be clipped")]
|
||||
public bool DontClipMaskScalingRect = false;
|
||||
|
||||
[Tooltip("If set to true, this mask is applied to all child Text and Graphic objects belonging to this object.")]
|
||||
public bool CascadeToALLChildren;
|
||||
Vector3[] worldCorners;
|
||||
|
||||
Vector2 AlphaUV;
|
||||
|
||||
Vector2 min;
|
||||
Vector2 max = Vector2.one;
|
||||
Vector2 p;
|
||||
Vector2 siz;
|
||||
Vector2 tp = new Vector2(.5f, .5f);
|
||||
|
||||
|
||||
bool MaterialNotSupported; // UI items like toggles, we can stil lcascade down to them though :)
|
||||
Rect maskRect;
|
||||
Rect contentRect;
|
||||
|
||||
Vector2 centre;
|
||||
Vector3[] worldCorners = new Vector3[4];
|
||||
Vector2 maskOffset = Vector2.zero;
|
||||
Vector2 maskScale = Vector2.one;
|
||||
|
||||
bool isText = false;
|
||||
|
||||
|
@ -59,167 +41,68 @@ namespace UnityEngine.UI.Extensions
|
|||
{
|
||||
myRect = GetComponent<RectTransform>();
|
||||
|
||||
if (!MaskArea)
|
||||
if (MaskArea == null)
|
||||
{
|
||||
MaskArea = myRect;
|
||||
}
|
||||
|
||||
if (GetComponent<Graphic>() != null)
|
||||
{
|
||||
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
|
||||
GetComponent<Graphic>().material = mat;
|
||||
}
|
||||
|
||||
if (GetComponent<Text>())
|
||||
var text = GetComponent<Text>();
|
||||
if (text != null)
|
||||
{
|
||||
isText = true;
|
||||
mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText"));
|
||||
GetComponent<Text>().material = mat;
|
||||
|
||||
GetCanvas();
|
||||
text.material = mat;
|
||||
cachedCanvas = text.canvas;
|
||||
|
||||
// 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.
|
||||
if (transform.parent.GetComponent<Button>() == null && transform.parent.GetComponent<Mask>() == null)
|
||||
if (transform.parent.GetComponent<Mask>() == null)
|
||||
transform.parent.gameObject.AddComponent<Mask>();
|
||||
|
||||
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));
|
||||
}
|
||||
transform.parent.GetComponent<Mask>().enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
MaterialNotSupported = mat == null;
|
||||
}
|
||||
|
||||
void SetSAM(Transform t)
|
||||
{
|
||||
SoftMaskScript thisSam = t.gameObject.GetComponent<SoftMaskScript>();
|
||||
if (thisSam == null)
|
||||
var graphic = GetComponent<Graphic>();
|
||||
if (graphic != null)
|
||||
{
|
||||
thisSam = t.gameObject.AddComponent<SoftMaskScript>();
|
||||
|
||||
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
|
||||
graphic.material = mat;
|
||||
cachedCanvas = graphic.canvas;
|
||||
}
|
||||
thisSam.MaskArea = MaskArea;
|
||||
thisSam.AlphaMask = AlphaMask;
|
||||
thisSam.CutOff = CutOff;
|
||||
thisSam.HardBlend = HardBlend;
|
||||
thisSam.FlipAlphaMask = FlipAlphaMask;
|
||||
thisSam.maskScalingRect = maskScalingRect;
|
||||
thisSam.DontClipMaskScalingRect = DontClipMaskScalingRect;
|
||||
thisSam.CascadeToALLChildren = CascadeToALLChildren;
|
||||
|
||||
}
|
||||
|
||||
void GetCanvas()
|
||||
Transform GetParentTranform(Transform t)
|
||||
{
|
||||
Transform t = transform;
|
||||
|
||||
int lvlLimit = 100;
|
||||
int lvl = 0;
|
||||
|
||||
while (canvas == null && lvl < lvlLimit)
|
||||
{
|
||||
canvas = t.gameObject.GetComponent<Canvas>();
|
||||
if (canvas == null)
|
||||
{
|
||||
t = t.parent;
|
||||
}
|
||||
|
||||
lvl++;
|
||||
}
|
||||
return t.parent;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
SetMask();
|
||||
if (cachedCanvas != null)
|
||||
{
|
||||
SetMask();
|
||||
}
|
||||
}
|
||||
|
||||
void SetMask()
|
||||
{
|
||||
if (MaterialNotSupported)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var maskRectXform = MaskArea;
|
||||
var worldRect = RectTransformUtility.PixelAdjustRect(MaskArea, cachedCanvas);
|
||||
|
||||
// Get the two rectangle areas
|
||||
maskRect = MaskArea.rect;
|
||||
contentRect = myRect.rect;
|
||||
var size = worldRect.size;
|
||||
maskScale.Set(1.0f / size.x, 1.0f / size.y);
|
||||
maskOffset = -worldRect.min;
|
||||
maskOffset.Scale(maskScale);
|
||||
|
||||
if (isText) // Need to do our calculations in world for Text
|
||||
{
|
||||
maskScalingRect = null;
|
||||
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);
|
||||
}
|
||||
else // Need to do our calculations in tex space for Image.
|
||||
{
|
||||
if (maskScalingRect != null)
|
||||
{
|
||||
maskRect = maskScalingRect.rect;
|
||||
}
|
||||
|
||||
// Get the centre offset
|
||||
if (maskScalingRect != null)
|
||||
{
|
||||
centre = myRect.transform.InverseTransformPoint(maskScalingRect.transform.TransformPoint(maskScalingRect.rect.center));
|
||||
}
|
||||
else
|
||||
{
|
||||
centre = myRect.transform.InverseTransformPoint(MaskArea.transform.TransformPoint(MaskArea.rect.center));
|
||||
}
|
||||
centre += (Vector2)myRect.transform.InverseTransformPoint(myRect.transform.position) - myRect.rect.center;
|
||||
|
||||
// Set the scale for mapping texcoords mask
|
||||
AlphaUV = new Vector2(maskRect.width / contentRect.width, maskRect.height / contentRect.height);
|
||||
|
||||
// set my min and max to the centre offest
|
||||
min = centre;
|
||||
max = min;
|
||||
|
||||
siz = new Vector2(maskRect.width, maskRect.height) * .5f;
|
||||
// Move them out to the min max extreams
|
||||
min -= siz;
|
||||
max += siz;
|
||||
|
||||
// Now move these into texture space. 0 - 1
|
||||
min = new Vector2(min.x / contentRect.width, min.y / contentRect.height) + tp;
|
||||
max = new Vector2(max.x / contentRect.width, max.y / contentRect.height) + tp;
|
||||
}
|
||||
|
||||
mat.SetFloat("_HardBlend", HardBlend ? 1 : 0);
|
||||
|
||||
// Pass the values to the shader
|
||||
mat.SetVector("_Min", min);
|
||||
mat.SetVector("_Max", max);
|
||||
|
||||
mat.SetInt("_FlipAlphaMask", FlipAlphaMask ? 1 : 0);
|
||||
mat.SetTextureOffset("_AlphaMask", maskOffset);
|
||||
mat.SetTextureScale("_AlphaMask", maskScale);
|
||||
mat.SetTexture("_AlphaMask", AlphaMask);
|
||||
|
||||
mat.SetInt("_NoOuterClip", DontClipMaskScalingRect && maskScalingRect != null ? 1 : 0);
|
||||
|
||||
if (!isText) // No mod needed for Text
|
||||
{
|
||||
mat.SetVector("_AlphaUV", AlphaUV);
|
||||
}
|
||||
|
||||
mat.SetFloat("_HardBlend", HardBlend ? 1 : 0);
|
||||
mat.SetInt("_FlipAlphaMask", FlipAlphaMask ? 1 : 0);
|
||||
mat.SetInt("_NoOuterClip", DontClipMaskScalingRect ? 1 : 0);
|
||||
mat.SetFloat("_CutOff", CutOff);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,7 @@
|
|||
_StencilReadMask("Stencil Read Mask", Float) = 255
|
||||
|
||||
_ColorMask("Color Mask", Float) = 15
|
||||
|
||||
_Min("Min",Vector) = (0,0,0,0)
|
||||
_Max("Max",Vector) = (1,1,0,0)
|
||||
_AlphaMask("AlphaMask - Must be Wrapped",2D) = "white"{}
|
||||
_AlphaUV("AlphaUV",Vector) = (0,0,0,0)
|
||||
_CutOff("CutOff",Float) = 0
|
||||
[MaterialToggle]
|
||||
_HardBlend("HardBlend",Float) = 0
|
||||
|
@ -73,26 +69,30 @@
|
|||
float4 vertex : SV_POSITION;
|
||||
fixed4 color : COLOR;
|
||||
half2 texcoord : TEXCOORD0;
|
||||
float4 worldPosition : TEXCOORD1;
|
||||
float2 maskTexcoord : TEXCOORD1;
|
||||
};
|
||||
|
||||
inline float UnityGet2DClipping (in float2 position, in float4 clipRect)
|
||||
{
|
||||
float2 inside = step(clipRect.xy, position.xy) * step(position.xy, clipRect.zw);
|
||||
return inside.x * inside.y;
|
||||
}
|
||||
|
||||
fixed4 _Color;
|
||||
fixed4 _TextureSampleAdd;
|
||||
sampler2D _MainTex;
|
||||
|
||||
bool _UseAlphaClip;
|
||||
|
||||
float4 _ProgressColor;
|
||||
float _Value;
|
||||
int _LeftToRight;
|
||||
|
||||
int _FlipAlphaMask = 0;
|
||||
float4 _AlphaMask_ST;
|
||||
sampler2D _AlphaMask;
|
||||
|
||||
v2f vert(appdata_t IN)
|
||||
{
|
||||
v2f OUT;
|
||||
OUT.worldPosition = IN.vertex;
|
||||
OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
|
||||
|
||||
float4 wolrdPos = IN.vertex;
|
||||
OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask);
|
||||
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
|
||||
OUT.texcoord = IN.texcoord;
|
||||
|
||||
#ifdef UNITY_HALF_TEXEL_OFFSET
|
||||
|
@ -103,29 +103,26 @@
|
|||
return OUT;
|
||||
}
|
||||
|
||||
sampler2D _MainTex;
|
||||
sampler2D _AlphaMask;
|
||||
|
||||
float2 _AlphaUV;
|
||||
|
||||
float2 _Min;
|
||||
float2 _Max;
|
||||
|
||||
float _CutOff;
|
||||
|
||||
bool _HardBlend = false;
|
||||
bool _NoOuterClip = false;
|
||||
|
||||
fixed4 frag(v2f IN) : SV_Target
|
||||
{
|
||||
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
|
||||
float4 inMask = float4(
|
||||
step(float2(0.0f, 0.0f), IN.maskTexcoord),
|
||||
step(IN.maskTexcoord, float2(1.0f, 1.0f)) );
|
||||
|
||||
// Do we want to clip the image to the Mask Rectangle?
|
||||
if (!_NoOuterClip && (IN.texcoord.x < _Min.x || IN.texcoord.x > _Max.x || IN.texcoord.y < _Min.y || IN.texcoord.y > _Max.y)) // Yes we do
|
||||
if (_NoOuterClip == false && all(inMask) == false )
|
||||
{
|
||||
color.a = 0;
|
||||
}
|
||||
else // It's in the mask rectangle, so apply the alpha of the mask provided.
|
||||
{
|
||||
float a = tex2D(_AlphaMask, (IN.texcoord - _Min) / _AlphaUV).a;
|
||||
|
||||
float a = tex2D(_AlphaMask, IN.maskTexcoord).a;
|
||||
|
||||
if (a <= _CutOff)
|
||||
a = 0;
|
||||
|
@ -138,13 +135,11 @@
|
|||
if (_FlipAlphaMask == 1)
|
||||
a = 1 - a;
|
||||
|
||||
if(!(IN.texcoord.x < _Min.x || IN.texcoord.x > _Max.x || IN.texcoord.y < _Min.y || IN.texcoord.y > _Max.y))
|
||||
color.a *= a;
|
||||
color.a *= a;
|
||||
}
|
||||
|
||||
if (_UseAlphaClip)
|
||||
clip(color.a - 0.001);
|
||||
|
||||
return color;
|
||||
}
|
||||
ENDCG
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
|
||||
|
||||
Shader "UI Extensions/SoftMaskShaderText"
|
||||
Shader "UI Extensions/SoftMaskShaderText"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
|
@ -14,144 +12,12 @@ Shader "UI Extensions/SoftMaskShaderText"
|
|||
_StencilReadMask("Stencil Read Mask", Float) = 255
|
||||
|
||||
_ColorMask("Color Mask", Float) = 15
|
||||
|
||||
_Min("Min",Vector) = (0,0,0,0)
|
||||
_Max("Max",Vector) = (1,1,0,0)
|
||||
_AlphaMask("AlphaMask - Must be Wrapped",2D) = "white"{}
|
||||
_CutOff("CutOff",Float) = 0
|
||||
[MaterialToggle]
|
||||
_HardBlend("HardBlend",Float) = 0
|
||||
_FlipAlphaMask("Flip Alpha Mask",int) = 0
|
||||
_NoOuterClip("Outer Clip",int) = 0
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags
|
||||
{
|
||||
"Queue" = "Transparent"
|
||||
"IgnoreProjector" = "True"
|
||||
"RenderType" = "Transparent"
|
||||
"PreviewType" = "Plane"
|
||||
"CanUseSpriteAtlas" = "True"
|
||||
}
|
||||
|
||||
Stencil
|
||||
{
|
||||
Ref[_Stencil]
|
||||
Comp[_StencilComp]
|
||||
Pass[_StencilOp]
|
||||
ReadMask[_StencilReadMask]
|
||||
WriteMask[_StencilWriteMask]
|
||||
}
|
||||
|
||||
Cull Off
|
||||
Lighting Off
|
||||
ZWrite Off
|
||||
ZTest[unity_GUIZTestMode]
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
ColorMask[_ColorMask]
|
||||
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members _ProgressColor,_Value,_Min,_Max,_Mul,_CutOff)
|
||||
#pragma exclude_renderers d3d11 xbox360
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
struct appdata_t
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
float4 color : COLOR;
|
||||
float2 texcoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
fixed4 color : COLOR;
|
||||
half2 texcoord : TEXCOORD0;
|
||||
float4 worldPosition : TEXCOORD1;
|
||||
float4 worldPosition2 : COLOR1;
|
||||
|
||||
fixed4 _Color;
|
||||
fixed4 _TextureSampleAdd;
|
||||
|
||||
bool _UseAlphaClip;
|
||||
|
||||
float4 _ProgressColor;
|
||||
float _Value;
|
||||
int _LeftToRight;
|
||||
|
||||
int _HardBlend = false;
|
||||
|
||||
int _FlipAlphaMask = 0;
|
||||
|
||||
sampler2D _MainTex;
|
||||
sampler2D _AlphaMask;
|
||||
|
||||
float2 _Min;
|
||||
float2 _Max;
|
||||
|
||||
float2 _Mul;
|
||||
|
||||
float _CutOff;
|
||||
int _NoOuterClip;
|
||||
v2f vert(appdata_t IN)
|
||||
{
|
||||
v2f OUT;
|
||||
OUT.worldPosition = IN.vertex;
|
||||
OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
|
||||
OUT.worldPosition2 = mul(unity_ObjectToWorld, IN.vertex);
|
||||
|
||||
OUT.texcoord = IN.texcoord;
|
||||
|
||||
#ifdef UNITY_HALF_TEXEL_OFFSET
|
||||
OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
|
||||
OUT.worldPosition2 += (_ScreenParams.zw - 1.0)*float2(-1, 1);
|
||||
#endif
|
||||
|
||||
OUT.color = IN.color * _Color;
|
||||
return OUT;
|
||||
}
|
||||
|
||||
|
||||
|
||||
fixed4 frag(v2f IN) : SV_Target
|
||||
{
|
||||
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
|
||||
|
||||
|
||||
// Do we want to clip the image to the Mask Rectangle?
|
||||
if (IN.worldPosition2.x <= _Min.x || IN.worldPosition2.x >= _Max.x || IN.worldPosition2.y <= _Min.y || IN.worldPosition2.y >= _Max.y)
|
||||
color.a = 0;
|
||||
else // It's in the mask rectangle, so apply the alpha of the mask provided.
|
||||
{
|
||||
float a = tex2D(_AlphaMask, (IN.worldPosition2.xy - _Max) / (_Max-_Min)).a;
|
||||
|
||||
if (a <= _CutOff)
|
||||
a = 0;
|
||||
else
|
||||
{
|
||||
if (_HardBlend)
|
||||
a = 1;
|
||||
}
|
||||
|
||||
if (_FlipAlphaMask == 1)
|
||||
a = 1 - a;
|
||||
|
||||
if(!(IN.worldPosition2.x <= _Min.x || IN.worldPosition2.x >= _Max.x || IN.worldPosition2.y <= _Min.y || IN.worldPosition2.y >= _Max.y))
|
||||
color *= a;
|
||||
}
|
||||
|
||||
if (_UseAlphaClip)
|
||||
clip(color.a - 0.001);
|
||||
|
||||
return color;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
FallBack "UI Extensions/SoftMaskShader"
|
||||
}
|
Loading…
Reference in New Issue