Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 16, 2024
1 parent 8f7323c commit 1ed0a26
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 203 deletions.
3 changes: 2 additions & 1 deletion Packages/src/Editor/MaskingShapeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public override void OnInspectorGUI()
var current = target as MaskingShape;
if (!current) return;

Utils.GetStencilDepthAndMask(current.transform, false, out var mask);
var useStencil = UISoftMaskProjectSettings.useStencil;
Utils.GetStencilBits(current.transform, false, useStencil, out var mask, out var _);
var maskingMode = mask is SoftMask softMask ? softMask.GetActualMaskingMode() : SoftMask.MaskingMode.Normal;
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.EnumPopup(new GUIContent("Masking Mode"), maskingMode);
Expand Down
36 changes: 23 additions & 13 deletions Packages/src/Editor/SoftMaskEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,30 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_maskingMode);
OpenProjectSettings(current);

if (_maskingMode.intValue == (int)SoftMask.MaskingMode.SoftMasking)
switch ((SoftMask.MaskingMode)_maskingMode.intValue)
{
EditorGUILayout.PropertyField(_showMaskGraphic);
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_downSamplingRate);
EditorGUILayout.PropertyField(_softMaskingRange);

FixUiMaskIssue(current); // Fix 'UIMask' issue.
DrawSoftMaskBuffer(); // Preview soft mask buffer.
}
else
{
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_antiAliasingThreshold);
case SoftMask.MaskingMode.SoftMasking:
{
EditorGUILayout.PropertyField(_showMaskGraphic);
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_downSamplingRate);
EditorGUILayout.PropertyField(_softMaskingRange);

FixUiMaskIssue(current); // Fix 'UIMask' issue.
DrawSoftMaskBuffer(); // Preview soft mask buffer.
break;
}
case SoftMask.MaskingMode.AntiAliasing:
{
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_antiAliasingThreshold);
break;
}
case SoftMask.MaskingMode.Normal:
{
EditorGUILayout.PropertyField(_showMaskGraphic);
break;
}
}

serializedObject.ApplyModifiedProperties();
Expand Down
81 changes: 81 additions & 0 deletions Packages/src/Runtime/Internal/Extensions/MeshExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using UnityEngine;

namespace Coffee.UISoftMaskInternal
{
internal static class MeshExtensions
{
internal static readonly ObjectPool<Mesh> s_MeshPool = new ObjectPool<Mesh>(
() =>
{
var mesh = new Mesh
{
hideFlags = HideFlags.DontSave | HideFlags.NotEditable
};
mesh.MarkDynamic();
return mesh;
},
mesh => mesh,
mesh =>
{
if (mesh)
{
mesh.Clear();
}
});

public static Mesh Rent()
{
return s_MeshPool.Rent();
}

public static void Return(ref Mesh mesh)
{
s_MeshPool.Return(ref mesh);
}

public static void CopyTo(this Mesh self, Mesh dst)
{
if (!self || !dst) return;

var vector3List = ListPool<Vector3>.Rent();
var vector4List = ListPool<Vector4>.Rent();
var color32List = ListPool<Color32>.Rent();
var intList = ListPool<int>.Rent();

dst.Clear(false);

self.GetVertices(vector3List);
dst.SetVertices(vector3List);

self.GetTriangles(intList, 0);
dst.SetTriangles(intList, 0);

self.GetNormals(vector3List);
dst.SetNormals(vector3List);

self.GetTangents(vector4List);
dst.SetTangents(vector4List);

self.GetColors(color32List);
dst.SetColors(color32List);

self.GetUVs(0, vector4List);
dst.SetUVs(0, vector4List);

self.GetUVs(1, vector4List);
dst.SetUVs(1, vector4List);

self.GetUVs(2, vector4List);
dst.SetUVs(2, vector4List);

self.GetUVs(3, vector4List);
dst.SetUVs(3, vector4List);

dst.RecalculateBounds();
ListPool<Vector3>.Return(ref vector3List);
ListPool<Vector4>.Return(ref vector4List);
ListPool<Color32>.Return(ref color32List);
ListPool<int>.Return(ref intList);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 23 additions & 21 deletions Packages/src/Runtime/MaskingShape/MaskingShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum MaskingMethod
public bool m_AlphaHitTest;

[Tooltip("Enable anti-alias masking.")] [SerializeField] [Range(0f, 1f)]
public float m_AntiAliasingThreshold = 0.5f;
public float m_AntiAliasingThreshold;

[Tooltip("The range for soft masking.")]
[SerializeField]
Expand All @@ -47,7 +47,7 @@ public enum MaskingMethod
private Matrix4x4 _prevTransformMatrix;
private UnityAction _setContainerDirty;
private bool _shouldRecalculateStencil;
private int _stencilDepth;
private int _stencilBits;
private Action _updateAntiAliasing;
private UnityAction _updateContainer;

Expand Down Expand Up @@ -136,7 +136,7 @@ protected override void OnDisable()
StencilMaterial.Remove(_maskMaterial);
_maskMaterial = null;

SoftMaskUtils.meshPool.Return(ref _mesh);
MeshExtensions.Return(ref _mesh);
SoftMaskUtils.materialPropertyBlockPool.Return(ref _mpb);

SetContainerDirty();
Expand Down Expand Up @@ -220,26 +220,24 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)

// Not in mask.
RecalculateStencilIfNeeded();
if (_stencilDepth <= 0)
if (_stencilBits == 0 && !_mask)
{
StencilMaterial.Remove(_maskMaterial);
_maskMaterial = null;
return null;
}

var colorMask = m_ShowMaskGraphic ? ColorWriteMask.All : 0;
var stencilBit = 1 << (_stencilDepth - 1);

// Mask material
Material maskMat = null;
if (SoftMaskingEnabled())
var colorMask = m_ShowMaskGraphic ? ColorWriteMask.All : 0;
if (SoftMaskingEnabled() && !UISoftMaskProjectSettings.useStencil)
{
if (m_ShowMaskGraphic)
{
Profiler.BeginSample(
"(SM4UI)[MaskingShape)] GetModifiedMaterial > StencilMaterial.Add for SoftMask");
maskMat = StencilMaterial.Add(baseMaterial, stencilBit, StencilOp.Keep, CompareFunction.Equal,
colorMask, stencilBit, stencilBit);
maskMat = StencilMaterial.Add(baseMaterial, _stencilBits, StencilOp.Keep, CompareFunction.Equal,
colorMask, _stencilBits, _stencilBits);
Profiler.EndSample();
}
}
Expand All @@ -249,12 +247,12 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
switch (maskingMethod)
{
case MaskingMethod.Additive:
maskMat = StencilMaterial.Add(baseMaterial, stencilBit, StencilOp.Replace,
CompareFunction.NotEqual, colorMask, stencilBit, stencilBit);
maskMat = StencilMaterial.Add(baseMaterial, _stencilBits, StencilOp.Replace,
CompareFunction.NotEqual, colorMask, _stencilBits, _stencilBits);
break;
case MaskingMethod.Subtract:
maskMat = StencilMaterial.Add(baseMaterial, stencilBit, StencilOp.Invert,
CompareFunction.Equal, colorMask, stencilBit, stencilBit);
maskMat = StencilMaterial.Add(baseMaterial, _stencilBits, StencilOp.Invert,
CompareFunction.Equal, colorMask, _stencilBits, _stencilBits);
break;
}

Expand All @@ -278,7 +276,7 @@ void IMeshModifier.ModifyMesh(VertexHelper verts)
Profiler.BeginSample("(SM4UI)[MaskingShape)] ModifyMesh");
if (!_mesh)
{
_mesh = SoftMaskUtils.meshPool.Rent();
_mesh = MeshExtensions.Rent();
}

_mesh.Clear(false);
Expand Down Expand Up @@ -306,13 +304,14 @@ private void RecalculateStencilIfNeeded()
if (!isActiveAndEnabled)
{
_mask = null;
_stencilDepth = -1;
_stencilBits = 0;
return;
}

if (!_shouldRecalculateStencil) return;
_shouldRecalculateStencil = false;
_stencilDepth = Utils.GetStencilDepthAndMask(transform, true, out _mask);
var useStencil = UISoftMaskProjectSettings.useStencil;
_stencilBits = Utils.GetStencilBits(transform, true, useStencil, out _mask, out var _);
}

private void SetContainerDirty()
Expand All @@ -328,7 +327,8 @@ private void UpdateContainer()
Mask mask = null;
if (isActiveAndEnabled)
{
Utils.GetStencilDepthAndMask(transform, false, out mask);
var useStencil = UISoftMaskProjectSettings.useStencil;
Utils.GetStencilBits(transform, false, useStencil, out mask, out var _);
}

var newContainer = mask.GetOrAddComponent<MaskingShapeContainer>();
Expand Down Expand Up @@ -373,7 +373,9 @@ internal bool IsInside(Vector2 sp, Camera eventCamera, float threshold = 0.01f)

internal void DrawSoftMaskBuffer(CommandBuffer cb, int depth)
{
if (!_mesh) return;
var texture = graphic.mainTexture;
var mesh = _mesh;
if (!mesh) return;
if (!graphic.IsInScreen()) return;

Profiler.BeginSample("(SM4UI)[MaskingShape)] DrawSoftMaskBuffer");
Expand All @@ -382,10 +384,10 @@ internal void DrawSoftMaskBuffer(CommandBuffer cb, int depth)
_mpb = SoftMaskUtils.materialPropertyBlockPool.Rent();
}

SoftMaskUtils.ApplyMaterialPropertyBlock(_mpb, depth, graphic.mainTexture, softnessRange);
SoftMaskUtils.ApplyMaterialPropertyBlock(_mpb, depth, texture, softnessRange);
var softMaterial = SoftMaskUtils.GetSoftMaskingMaterial(maskingMethod);

cb.DrawMesh(_mesh, transform.localToWorldMatrix, softMaterial, 0, 0, _mpb);
cb.DrawMesh(mesh, transform.localToWorldMatrix, softMaterial, 0, 0, _mpb);
Profiler.EndSample();
}

Expand Down
41 changes: 32 additions & 9 deletions Packages/src/Runtime/MaskingShape/TerminalMaskingShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ namespace Coffee.UISoftMask
[RequireComponent(typeof(CanvasRenderer))]
[DisallowMultipleComponent]
[AddComponentMenu("")]
public class TerminalMaskingShape : MaskableGraphic, ILayoutElement, ILayoutIgnorer
public class TerminalMaskingShape : MaskableGraphic, ILayoutElement, ILayoutIgnorer, IMaskable
{
private static Material s_SharedTerminalMaterial;
private Mask _mask;
private Mask _parentMask;
private bool _shouldRecalculateStencil;
private int _stencilBits;

public override bool raycastTarget
{
Expand All @@ -33,17 +36,18 @@ protected override void OnEnable()
}

material = s_SharedTerminalMaterial;
transform.parent.TryGetComponent(out _mask);
transform.parent.TryGetComponent(out _parentMask);
_shouldRecalculateStencil = true;

base.OnEnable();
}

protected override void OnDisable()
{
base.OnDisable();
if (_mask && _mask.MaskEnabled())
if (_parentMask && _parentMask.MaskEnabled())
{
_mask.graphic.SetMaterialDirty();
_parentMask.graphic.SetMaterialDirty();
}
}

Expand Down Expand Up @@ -71,6 +75,11 @@ void ILayoutElement.CalculateLayoutInputVertical()

bool ILayoutIgnorer.ignoreLayout => true;

void IMaskable.RecalculateMasking()
{
_shouldRecalculateStencil = true;
}

public override Material GetModifiedMaterial(Material baseMaterial)
{
if (!IsActive())
Expand All @@ -80,17 +89,16 @@ public override Material GetModifiedMaterial(Material baseMaterial)
return null;
}

var stencilDepth = Utils.GetStencilDepthAndMask(transform, false, out var mask);
if (stencilDepth <= 0 || _mask != mask)
RecalculateStencilIfNeeded();
if ((_stencilBits == 0 && !_mask) || _parentMask != _mask)
{
StencilMaterial.Remove(m_MaskMaterial);
m_MaskMaterial = null;
return null;
}

var desiredStencilBit = 1 << (stencilDepth - 1);
var maskMat = StencilMaterial.Add(baseMaterial, desiredStencilBit, StencilOp.Zero,
CompareFunction.Equal, 0, desiredStencilBit, desiredStencilBit);
var maskMat = StencilMaterial.Add(baseMaterial, _stencilBits, StencilOp.Zero, CompareFunction.Equal, 0,
_stencilBits, _stencilBits);

StencilMaterial.Remove(m_MaskMaterial);
m_MaskMaterial = maskMat;
Expand All @@ -114,6 +122,21 @@ protected override void OnPopulateMesh(VertexHelper vh)
vh.AddTriangle(2, 3, 0);
Profiler.EndSample();
}

private void RecalculateStencilIfNeeded()
{
if (!isActiveAndEnabled)
{
_mask = null;
_stencilBits = 0;
return;
}

if (!_shouldRecalculateStencil) return;
_shouldRecalculateStencil = false;
var useStencil = UISoftMaskProjectSettings.useStencil;
_stencilBits = Utils.GetStencilBits(transform, false, useStencil, out _mask, out var _);
}
}

#if UNITY_EDITOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static bool softMaskEnabled
}
}

public static bool useStencil =>
false;
#if UNITY_MODULE_VR
public static bool stereoEnabled => softMaskEnabled && instance.m_StereoEnabled && XRSettings.enabled;
#else
Expand Down
Loading

0 comments on commit 1ed0a26

Please sign in to comment.