Softmask fix

maskRect in canvas space not in world space

--HG--
branch : develop_5.3
pull/413/head
valtain@gmail.com 2016-12-24 18:03:52 +09:00
parent 5ed1e48cb2
commit af8abe9551
1 changed files with 21 additions and 4 deletions

View File

@ -8,7 +8,6 @@ namespace UnityEngine.UI.Extensions
public class SoftMaskScript : MonoBehaviour public class SoftMaskScript : MonoBehaviour
{ {
Material mat; Material mat;
Canvas cachedCanvas= null;
[Tooltip("The area that is to be used as the container.")] [Tooltip("The area that is to be used as the container.")]
public RectTransform MaskArea; public RectTransform MaskArea;
@ -53,7 +52,7 @@ namespace UnityEngine.UI.Extensions
mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText")); mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText"));
text.material = mat; text.material = mat;
cachedCanvas = text.canvas; cachedCanvas = text.canvas;
cachedCanvasTransform = cachedCanvas.transform;
// 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.
if (transform.parent.GetComponent<Mask>() == null) if (transform.parent.GetComponent<Mask>() == null)
@ -69,6 +68,7 @@ namespace UnityEngine.UI.Extensions
mat = new Material(Shader.Find("UI Extensions/SoftMaskShader")); mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
graphic.material = mat; graphic.material = mat;
cachedCanvas = graphic.canvas; cachedCanvas = graphic.canvas;
cachedCanvasTransform = cachedCanvas.transform;
} }
} }
@ -89,8 +89,7 @@ namespace UnityEngine.UI.Extensions
void SetMask() void SetMask()
{ {
var maskRectXform = MaskArea; var maskRectXform = MaskArea;
var worldRect = RectTransformUtility.PixelAdjustRect(MaskArea, cachedCanvas); var worldRect = GetCanvasRect();
var size = worldRect.size; var size = worldRect.size;
maskScale.Set(1.0f / size.x, 1.0f / size.y); maskScale.Set(1.0f / size.x, 1.0f / size.y);
maskOffset = -worldRect.min; maskOffset = -worldRect.min;
@ -105,5 +104,23 @@ namespace UnityEngine.UI.Extensions
mat.SetInt("_NoOuterClip", DontClipMaskScalingRect ? 1 : 0); mat.SetInt("_NoOuterClip", DontClipMaskScalingRect ? 1 : 0);
mat.SetFloat("_CutOff", CutOff); mat.SetFloat("_CutOff", CutOff);
} }
Canvas cachedCanvas = null;
Transform cachedCanvasTransform = null;
readonly Vector3[] m_WorldCorners = new Vector3[4];
readonly Vector3[] m_CanvasCorners = new Vector3[4];
public Rect GetCanvasRect()
{
if (cachedCanvas == null)
return new Rect();
MaskArea.GetWorldCorners(m_WorldCorners);
var canvasTransform = cachedCanvasTransform;
for (int i = 0; i < 4; ++i)
m_CanvasCorners[i] = canvasTransform.InverseTransformPoint(m_WorldCorners[i]);
return new Rect(m_CanvasCorners[0].x, m_CanvasCorners[0].y, m_CanvasCorners[2].x - m_CanvasCorners[0].x, m_CanvasCorners[2].y - m_CanvasCorners[0].y);
}
} }
} }