2018-11-20 07:49:14 +08:00
|
|
|
#ifndef UI_SOFTMASK_INCLUDED
|
|
|
|
#define UI_SOFTMASK_INCLUDED
|
|
|
|
|
|
|
|
sampler2D _SoftMaskTex;
|
|
|
|
float _Stencil;
|
2019-02-01 15:27:56 +08:00
|
|
|
float4x4 _SceneV;
|
|
|
|
float4x4 _SceneP;
|
|
|
|
float4x4 _GameVP;
|
2019-01-26 20:30:41 +08:00
|
|
|
half4 _MaskInteraction;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
fixed Approximately(float4x4 a, float4x4 b)
|
|
|
|
{
|
|
|
|
float4x4 d = abs(a - b);
|
|
|
|
return step(
|
|
|
|
max(d._m00,max(d._m01,max(d._m02,max(d._m03,
|
|
|
|
max(d._m10,max(d._m11,max(d._m12,max(d._m13,
|
|
|
|
max(d._m20,max(d._m21,max(d._m22,max(d._m23,
|
|
|
|
max(d._m30,max(d._m31,max(d._m32,d._m33))))))))))))))),
|
2019-02-01 15:27:56 +08:00
|
|
|
1);
|
2018-11-20 07:49:14 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 08:21:37 +08:00
|
|
|
float GetMaskAlpha(float alpha, int stencilId, float interaction)
|
2019-01-26 20:30:41 +08:00
|
|
|
{
|
|
|
|
fixed onStencil = step(stencilId, _Stencil);
|
|
|
|
alpha = lerp(1, alpha, onStencil * step(1, interaction));
|
|
|
|
return lerp(alpha, 1 - alpha, onStencil * step(2, interaction));
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:21:37 +08:00
|
|
|
#if SOFTMASK_EDITOR
|
|
|
|
float SoftMaskInternal(float4 clipPos, float4 wpos)
|
|
|
|
#else
|
|
|
|
float SoftMaskInternal(float4 clipPos)
|
|
|
|
#endif
|
2018-11-20 07:49:14 +08:00
|
|
|
{
|
|
|
|
half2 view = clipPos.xy/_ScreenParams.xy;
|
2019-02-01 15:27:56 +08:00
|
|
|
#if SOFTMASK_EDITOR
|
|
|
|
fixed isSceneView = max(Approximately(UNITY_MATRIX_V, _SceneV), Approximately(UNITY_MATRIX_P, _SceneP));
|
|
|
|
float4 cpos = mul(_GameVP, mul(UNITY_MATRIX_M, wpos));
|
|
|
|
view = lerp(view, cpos.xy / cpos.w * 0.5 + 0.5, isSceneView);
|
|
|
|
#endif
|
|
|
|
|
2019-01-11 20:31:44 +08:00
|
|
|
#if UNITY_UV_STARTS_AT_TOP
|
2019-01-11 20:36:28 +08:00
|
|
|
view.y = 1.0 - view.y;
|
|
|
|
#endif
|
2018-11-20 07:49:14 +08:00
|
|
|
|
2019-01-26 20:30:41 +08:00
|
|
|
fixed4 mask = tex2D(_SoftMaskTex, view);
|
|
|
|
half alpha = GetMaskAlpha(mask.x, 1, _MaskInteraction.x)
|
|
|
|
* GetMaskAlpha(mask.y, 3, _MaskInteraction.y)
|
|
|
|
* GetMaskAlpha(mask.z, 7, _MaskInteraction.z)
|
2019-02-05 08:21:37 +08:00
|
|
|
* GetMaskAlpha(mask.w, 15, _MaskInteraction.w)
|
2019-02-05 08:27:25 +08:00
|
|
|
#if SOFTMASK_EDITOR
|
|
|
|
* step(0, view.x) * step(view.x, 1) * step(0, view.y) * step(view.y, 1)
|
|
|
|
#endif
|
2019-02-05 08:21:37 +08:00
|
|
|
;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
return alpha;
|
|
|
|
}
|
|
|
|
|
2019-02-05 08:21:37 +08:00
|
|
|
#if SOFTMASK_EDITOR
|
|
|
|
#define SOFTMASK_EDITOR_ONLY(x) x
|
|
|
|
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos, worldPosition)
|
|
|
|
#else
|
|
|
|
#define SOFTMASK_EDITOR_ONLY(x)
|
|
|
|
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos)
|
|
|
|
#endif
|
|
|
|
|
2019-01-26 20:30:41 +08:00
|
|
|
#endif // UI_SOFTMASK_INCLUDED
|