Skip to content

Commit

Permalink
feat: support non full-rect graphics for some effect
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 13, 2024
1 parent b744eff commit 1b0ec9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
12 changes: 12 additions & 0 deletions Packages/src/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class UIEffect2Editor : Editor
private SerializedProperty _gradationColor2;
private SerializedProperty _gradationGradient;

private bool _expandOthers;
private SerializedProperty _allowExtendVertex;

private void OnEnable()
{
if (target == null) return;
Expand Down Expand Up @@ -118,6 +121,8 @@ private void OnEnable()
_gradationColor1 = serializedObject.FindProperty("m_GradationColor1");
_gradationColor2 = serializedObject.FindProperty("m_GradationColor2");
_gradationGradient = serializedObject.FindProperty("m_GradationGradient");

_allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex");
}

public override void OnInspectorGUI()
Expand Down Expand Up @@ -277,6 +282,13 @@ public void DrawProperties()

EditorGUI.indentLevel--;
}

DrawSeparator();
_expandOthers = EditorGUILayout.BeginFoldoutHeaderGroup(_expandOthers, "Others");
if (_expandOthers)
{
EditorGUILayout.PropertyField(_allowExtendVertex);
}
}

private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter)
Expand Down
17 changes: 17 additions & 0 deletions Packages/src/Runtime/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public class UIEffect : UIEffectBase
[SerializeField]
private Gradient m_GradationGradient = new Gradient();

[SerializeField]
protected bool m_AllowExtendVertex = true;

public ToneFilter toneFilter
{
get => m_ToneFilter;
Expand Down Expand Up @@ -608,6 +611,17 @@ public Gradient gradationGradient
}
}

public bool allowExtendVertex
{
get => m_AllowExtendVertex;
set
{
if (m_AllowExtendVertex == value) return;
context.allowExtendVertex = m_AllowExtendVertex = value;
SetVerticesDirty();
}
}

public List<UIEffectReplica> replicas => _replicas ??= ListPool<UIEffectReplica>.Rent();
private List<UIEffectReplica> _replicas;

Expand Down Expand Up @@ -697,6 +711,7 @@ protected override void UpdateContext(UIEffectContext c)
c.gradationColor1 = m_GradationColor1;
c.gradationColor2 = m_GradationColor2;
c.gradationGradient = m_GradationGradient;
c.allowExtendVertex = m_AllowExtendVertex;
}

public override void ApplyContextToMaterial()
Expand Down Expand Up @@ -851,6 +866,8 @@ internal void CopyFrom(UIEffectContext c)
m_GradationColor2 = c.gradationColor2;
m_GradationGradient = c.gradationGradient;

m_AllowExtendVertex = c.allowExtendVertex;

UpdateContext(context);
ApplyContextToMaterial();
SetVerticesDirty();
Expand Down
39 changes: 15 additions & 24 deletions Packages/src/Runtime/UIEffectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public class UIEffectContext
public Color gradationColor1;
public Color gradationColor2;
public Gradient gradationGradient;
public bool allowExtendVertex;

public bool willModifyMaterial => samplingFilter != SamplingFilter.None
|| transitionFilter != TransitionFilter.None
Expand Down Expand Up @@ -202,6 +203,8 @@ public void CopyFrom(UIEffectContext preset)
gradationColor1 = preset.gradationColor1;
gradationColor2 = preset.gradationColor2;
gradationGradient = preset.gradationGradient;

allowExtendVertex = preset.allowExtendVertex;
}

public void ApplyToMaterial(Material material, float actualSamplingScale = 1f)
Expand Down Expand Up @@ -465,6 +468,8 @@ private int GetVertexCountMultiply()

private Vector2 GetExpandSize()
{
if (!allowExtendVertex) return Vector2.zero;

var expandSize = Vector2.zero;
switch (samplingFilter)
{
Expand Down Expand Up @@ -544,36 +549,22 @@ private static void GetBounds(List<UIVertex> verts, int start, int count, out Re
for (var i = start; i < start + count; i++)
{
var vt = verts[i];
var uv = vt.uv0;
var pos = vt.position;

// Left-Bottom
if (minPos.x >= pos.x && minPos.y >= pos.y)
{
minPos = pos;
}
// Right-Top
else if (maxPos.x <= pos.x && maxPos.y <= pos.y)
{
maxPos = pos;
}

// Left-Bottom
if (minUV.x >= uv.x && minUV.y >= uv.y)
{
minUV = uv;
}
// Right-Top
else if (maxUV.x <= uv.x && maxUV.y <= uv.y)
{
maxUV = uv;
}
UpdateMinMax(ref minPos, ref maxPos, vt.position);
UpdateMinMax(ref minUV, ref maxUV, vt.uv0);
}

// Shrink coordinate to avoid uv edge
posBounds = new Rect(minPos.x + 0.001f, minPos.y + 0.001f,
maxPos.x - minPos.x - 0.002f, maxPos.y - minPos.y - 0.002f);
uvBounds = new Rect(minUV.x, minUV.y, maxUV.x - minUV.x, maxUV.y - minUV.y);
}

private static void UpdateMinMax(ref Vector2 min, ref Vector2 max, Vector2 value)
{
if (value.x < min.x) min.x = value.x; // Left
if (max.x < value.x) max.x = value.x; // Right
if (value.y < min.y) min.y = value.y; // Bottom
if (max.y < value.y) max.y = value.y; // Top
}
}
}

0 comments on commit 1b0ec9e

Please sign in to comment.