Skip to content

Commit

Permalink
feat: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jun 5, 2020
1 parent 4d9f0c6 commit 018ec78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Packages/SoftMaskForUGUI/Scripts/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,9 @@ static void UpdateMaskTextures()
}

s_PreviousViewProjectionMatrices.Clear();
foreach (var id in s_NowViewProjectionMatrices.Keys)
foreach (var kv in s_NowViewProjectionMatrices)
{
s_PreviousViewProjectionMatrices[id] = s_NowViewProjectionMatrices[id];
s_PreviousViewProjectionMatrices.Add(kv.Key, kv.Value);
}

s_NowViewProjectionMatrices.Clear();
Expand Down
11 changes: 7 additions & 4 deletions Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
mat.SetTexture(s_SoftMaskTexId, softMask.softMaskBuffer);
mat.SetInt(s_StencilCompId,
m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always);

var root = MaskUtilities.FindRootSortOverrideCanvas(transform);
var stencil = MaskUtilities.GetStencilDepth(transform, root);
mat.SetVector(s_MaskInteractionId, new Vector4(
(m_MaskInteraction & 0x3),
((m_MaskInteraction >> 2) & 0x3),
((m_MaskInteraction >> 4) & 0x3),
((m_MaskInteraction >> 6) & 0x3)
1 <= stencil ? (m_MaskInteraction >> 0 & 0x3) : 0,
2 <= stencil ? (m_MaskInteraction >> 2 & 0x3) : 0,
3 <= stencil ? (m_MaskInteraction >> 4 & 0x3) : 0,
4 <= stencil ? (m_MaskInteraction >> 6 & 0x3) : 0
));
});

Expand Down
27 changes: 3 additions & 24 deletions Packages/SoftMaskForUGUI/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ float4x4 _GameVP;
float4x4 _GameTVP;
half4 _MaskInteraction;

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))))))))))))))),
0.0000001);
}

float GetMaskAlpha(float alpha, int stencilId, float interaction)
{
fixed onStencil = step(stencilId, _Stencil);
alpha = lerp(1, alpha, onStencil * step(1, interaction));
return lerp(alpha, 1 - alpha, onStencil * step(2, interaction));
}

#if SOFTMASK_EDITOR
float SoftMaskInternal(float4 clipPos, float4 wpos)
#else
Expand All @@ -33,7 +15,7 @@ float SoftMaskInternal(float4 clipPos)
{
half2 view = clipPos.xy/_ScreenParams.xy;
#if SOFTMASK_EDITOR
fixed isSceneView = 1 - Approximately(UNITY_MATRIX_VP, _GameVP);
fixed isSceneView = any(UNITY_MATRIX_VP - _GameVP);
float4 cpos = mul(_GameTVP, mul(UNITY_MATRIX_M, wpos));
view = lerp(view, cpos.xy / cpos.w * 0.5 + 0.5, isSceneView);
#if UNITY_UV_STARTS_AT_TOP
Expand All @@ -44,16 +26,13 @@ float SoftMaskInternal(float4 clipPos)
#endif

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)
* GetMaskAlpha(mask.w, 15, _MaskInteraction.w)
half4 alpha = saturate(lerp(fixed4(1, 1, 1, 1), lerp(mask, 1 - mask, _MaskInteraction - 1), _MaskInteraction))
#if SOFTMASK_EDITOR
* step(0, view.x) * step(view.x, 1) * step(0, view.y) * step(view.y, 1)
#endif
;

return alpha;
return alpha.x * alpha.y * alpha.z * alpha.w;
}

#if SOFTMASK_EDITOR
Expand Down

0 comments on commit 018ec78

Please sign in to comment.