diff --git a/Scripts/Effects/SoftMaskScript.cs b/Scripts/Effects/SoftMaskScript.cs index a9dd9e3..2d13b56 100644 --- a/Scripts/Effects/SoftMaskScript.cs +++ b/Scripts/Effects/SoftMaskScript.cs @@ -46,19 +46,13 @@ namespace UnityEngine.UI.Extensions MaskArea = myRect; } - if (GetComponent() != null) - { - mat = new Material(Shader.Find("UI Extensions/SoftMaskShader")); - GetComponent().material = mat; - } - - if (GetComponent()) + var text = GetComponent(); + if (text != null) { isText = true; mat = new Material(Shader.Find("UI Extensions/SoftMaskShaderText")); - GetComponent().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(); transform.parent.GetComponent().enabled = false; + return; } - } - void GetCanvas() - { - Transform t = transform; - - int lvlLimit = 100; - int lvl = 0; - - while (cachedCanvas == null && lvl < lvlLimit) + var graphic = GetComponent(); + if (graphic != null) { - cachedCanvas = t.gameObject.GetComponent(); - 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); diff --git a/Shaders/SoftMaskShader.shader b/Shaders/SoftMaskShader.shader index 6bce499..0accab5 100644 --- a/Shaders/SoftMaskShader.shader +++ b/Shaders/SoftMaskShader.shader @@ -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 }