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()
var graphic = GetComponent<Graphic>();
if (graphic != null)
{
Transform t = transform;
int lvlLimit = 100;
int lvl = 0;
while (cachedCanvas == null && lvl < lvlLimit)
{
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)
@ -92,19 +79,21 @@ namespace UnityEngine.UI.Extensions
}
void Update()
{
if (cachedCanvas != null)
{
SetMask();
}
}
void SetMask()
{
var maskRectXform = MaskArea;
var worldRect = RectTransformUtility.PixelAdjustRect(MaskArea, cachedCanvas);
MaskArea.GetWorldCorners(worldCorners);
var size = (worldCorners[2] - worldCorners[0]);
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,7 +90,7 @@
v2f vert(appdata_t IN)
{
v2f OUT;
float4 wolrdPos = mul(_Object2World, IN.vertex);
float4 wolrdPos = IN.vertex;
OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask);
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
@ -103,11 +103,7 @@
return OUT;
}
float _CutOff;
bool _HardBlend = false;
bool _NoOuterClip = false;
@ -144,7 +140,6 @@
if (_UseAlphaClip)
clip(color.a - 0.001);
return color;
}
ENDCG