Fixed softmask in scree-space overlay mode of Canvas

pull/413/head
valtain@gmail.com 2016-12-24 04:57:23 +09:00
parent 9df9bcc044
commit 7c800a07d6
2 changed files with 46 additions and 62 deletions

View File

@ -46,19 +46,13 @@ namespace UnityEngine.UI.Extensions
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.
@ -66,24 +60,17 @@ namespace UnityEngine.UI.Extensions
transform.parent.gameObject.AddComponent<Mask>();
transform.parent.GetComponent<Mask>().enabled = false;
return;
}
}
void GetCanvas()
{
Transform t = transform;
int lvlLimit = 100;
int lvl = 0;
while (cachedCanvas == null && lvl < lvlLimit)
var graphic = GetComponent<Graphic>();
if (graphic != null)
{
cachedCanvas = t.gameObject.GetComponent<Canvas>();
if (cachedCanvas == null)
t = GetParentTranform(t);
lvl++;
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
graphic.material = mat;
cachedCanvas = graphic.canvas;
}
}
Transform GetParentTranform(Transform t)
@ -93,18 +80,20 @@ namespace UnityEngine.UI.Extensions
void Update()
{
SetMask();
if (cachedCanvas != null)
{
SetMask();
}
}
void SetMask()
{
var maskRectXform = MaskArea;
MaskArea.GetWorldCorners(worldCorners);
var size = (worldCorners[2] - worldCorners[0]);
var worldRect = RectTransformUtility.PixelAdjustRect(MaskArea, cachedCanvas);
var size = worldRect.size;
maskScale.Set(1.0f / size.x, 1.0f / size.y);
maskOffset = -worldCorners[0];
maskOffset = -worldRect.min;
maskOffset.Scale(maskScale);
mat.SetTextureOffset("_AlphaMask", maskOffset);

View File

@ -90,8 +90,8 @@
v2f vert(appdata_t IN)
{
v2f OUT;
float4 wolrdPos = mul(_Object2World, IN.vertex);
OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask);
float4 wolrdPos = IN.vertex;
OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask);
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
@ -103,49 +103,44 @@
return OUT;
}
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)) );
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 == false && all(inMask) == false )
{
color.a = 0;
}
else // It's in the mask rectangle, so apply the alpha of the mask provided.
{
// Do we want to clip the image to the Mask Rectangle?
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.maskTexcoord).a;
float a = tex2D(_AlphaMask, IN.maskTexcoord).a;
if (a <= _CutOff)
a = 0;
else
{
if(_HardBlend)
a = 1;
}
if (a <= _CutOff)
a = 0;
else
{
if(_HardBlend)
a = 1;
}
if (_FlipAlphaMask == 1)
a = 1 - a;
if (_FlipAlphaMask == 1)
a = 1 - a;
color.a *= a;
}
color.a *= a;
}
if (_UseAlphaClip)
clip(color.a - 0.001);
return color;
if (_UseAlphaClip)
clip(color.a - 0.001);
return color;
}
ENDCG
}