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;
int lvlLimit = 100;
int lvl = 0;
while (cachedCanvas == null && lvl < lvlLimit)
{ {
cachedCanvas = t.gameObject.GetComponent<Canvas>(); mat = new Material(Shader.Find("UI Extensions/SoftMaskShader"));
if (cachedCanvas == null) graphic.material = mat;
t = GetParentTranform(t); cachedCanvas = graphic.canvas;
lvl++;
} }
} }
Transform GetParentTranform(Transform t) Transform GetParentTranform(Transform t)
@ -93,18 +80,20 @@ namespace UnityEngine.UI.Extensions
void Update() void Update()
{ {
SetMask(); if (cachedCanvas != null)
{
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,49 +103,44 @@
return OUT; return OUT;
} }
float _CutOff; float _CutOff;
bool _HardBlend = false; bool _HardBlend = false;
bool _NoOuterClip = false; bool _NoOuterClip = false;
fixed4 frag(v2f IN) : SV_Target fixed4 frag(v2f IN) : SV_Target
{ {
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color; half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
float4 inMask = float4( float4 inMask = float4(
step(float2(0.0f, 0.0f), IN.maskTexcoord), step(float2(0.0f, 0.0f), IN.maskTexcoord),
step(IN.maskTexcoord, float2(1.0f, 1.0f)) ); step(IN.maskTexcoord, float2(1.0f, 1.0f)) );
// Do we want to clip the image to the Mask Rectangle? // Do we want to clip the image to the Mask Rectangle?
if (_NoOuterClip == false && all(inMask) == false ) if (_NoOuterClip == false && all(inMask) == false )
{ {
color.a = 0; color.a = 0;
} }
else // It's in the mask rectangle, so apply the alpha of the mask provided. 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) if (a <= _CutOff)
a = 0; a = 0;
else else
{ {
if(_HardBlend) if(_HardBlend)
a = 1; a = 1;
} }
if (_FlipAlphaMask == 1) if (_FlipAlphaMask == 1)
a = 1 - a; a = 1 - a;
color.a *= a; color.a *= a;
} }
if (_UseAlphaClip) if (_UseAlphaClip)
clip(color.a - 0.001); clip(color.a - 0.001);
return color;
return color;
} }
ENDCG ENDCG
} }