Fixed softmask in scree-space overlay mode of Canvas

release
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; MaskArea = myRect;
} }
if (GetComponent<Graphic>() != null) var text = GetComponent<Text>();
{ if (text != null)
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
GetComponent<Graphic>().material = mat;
}
if (GetComponent<Text>())
{ {
isText = true; isText = true;
mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText")); mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText"));
GetComponent<Text>().material = mat; text.material = mat;
cachedCanvas = text.canvas;
GetCanvas();
// For some reason, having the mask control on the parent and disabled stops the mouse interacting // 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. // 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.gameObject.AddComponent<Mask>();
transform.parent.GetComponent<Mask>().enabled = false; transform.parent.GetComponent<Mask>().enabled = false;
} return;
} }
void GetCanvas() var graphic = GetComponent<Graphic>();
if (graphic != null)
{ {
Transform t = transform; mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
graphic.material = mat;
int lvlLimit = 100; cachedCanvas = graphic.canvas;
int lvl = 0;
while (cachedCanvas == null && lvl < lvlLimit)
{
cachedCanvas = t.gameObject.GetComponent<Canvas>();
if (cachedCanvas == null)
t = GetParentTranform(t);
lvl++;
} }
} }
Transform GetParentTranform(Transform t) Transform GetParentTranform(Transform t)
@ -92,19 +79,21 @@ namespace UnityEngine.UI.Extensions
} }
void Update() void Update()
{
if (cachedCanvas != null)
{ {
SetMask(); SetMask();
} }
}
void SetMask() void SetMask()
{ {
var maskRectXform = MaskArea; var maskRectXform = MaskArea;
var worldRect = RectTransformUtility.PixelAdjustRect(MaskArea, cachedCanvas);
MaskArea.GetWorldCorners(worldCorners); var size = worldRect.size;
var size = (worldCorners[2] - worldCorners[0]);
maskScale.Set(1.0f / size.x, 1.0f / size.y); maskScale.Set(1.0f / size.x, 1.0f / size.y);
maskOffset = -worldCorners[0]; maskOffset = -worldRect.min;
maskOffset.Scale(maskScale); maskOffset.Scale(maskScale);
mat.SetTextureOffset("_AlphaMask", maskOffset); mat.SetTextureOffset("_AlphaMask", maskOffset);

View File

@ -90,7 +90,7 @@
v2f vert(appdata_t IN) v2f vert(appdata_t IN)
{ {
v2f OUT; v2f OUT;
float4 wolrdPos = mul(_Object2World, IN.vertex); float4 wolrdPos = IN.vertex;
OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask); OUT.maskTexcoord = TRANSFORM_TEX(wolrdPos.xy, _AlphaMask);
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord; OUT.texcoord = IN.texcoord;
@ -103,11 +103,7 @@
return OUT; return OUT;
} }
float _CutOff; float _CutOff;
bool _HardBlend = false; bool _HardBlend = false;
bool _NoOuterClip = false; bool _NoOuterClip = false;
@ -144,7 +140,6 @@
if (_UseAlphaClip) if (_UseAlphaClip)
clip(color.a - 0.001); clip(color.a - 0.001);
return color; return color;
} }
ENDCG ENDCG