Skip to content

Commit bdbf293

Browse files
committed
fix: TransitionFilter and GradationMode do not work correctly when the initial scale is 0
close #311
1 parent 8155f94 commit bdbf293

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

Packages/src/Runtime/UIEffectBase.cs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.CompilerServices;
23
using Coffee.UIEffectInternal;
34
using UnityEngine;
@@ -21,6 +22,7 @@ public abstract class UIEffectBase : UIBehaviour, IMeshModifier, IMaterialModifi
2122
private Graphic _graphic;
2223
private Material _material;
2324
private UIEffectContext _context;
25+
private Action _setVerticesDirtyIfVisible;
2426

2527
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();
2628
public virtual uint effectId => (uint)GetInstanceID();
@@ -52,13 +54,20 @@ protected override void OnEnable()
5254

5355
protected override void OnDisable()
5456
{
57+
if (_setVerticesDirtyIfVisible != null)
58+
{
59+
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible;
60+
}
61+
5562
MaterialRepository.Release(ref _material);
5663
SetMaterialDirty();
5764
SetVerticesDirty();
5865
}
5966

6067
protected override void OnDestroy()
6168
{
69+
_setVerticesDirtyIfVisible = null;
70+
_graphic = null;
6271
s_ContextPool.Return(ref _context);
6372
}
6473

@@ -70,7 +79,27 @@ public virtual void ModifyMesh(VertexHelper vh)
7079
{
7180
if (!isActiveAndEnabled || context == null) return;
7281

73-
context.ModifyMesh(graphic, transitionRoot, vh, canModifyShape);
82+
if (CanModifyMesh())
83+
{
84+
context.ModifyMesh(graphic, transitionRoot, vh, canModifyShape);
85+
}
86+
// If you can't modify the mesh, retry next frame.
87+
else
88+
{
89+
UIExtraCallbacks.onBeforeCanvasRebuild += _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible;
90+
}
91+
}
92+
93+
private bool CanModifyMesh()
94+
{
95+
// The transitionRoot is same as the transform => true.
96+
var root = transitionRoot;
97+
if (transform == root) return true;
98+
99+
// The transitionRoot is not visible => false.
100+
var scale1 = root.lossyScale;
101+
var scale2 = transform.lossyScale;
102+
return !Mathf.Approximately(scale1.x * scale1.y * scale2.x * scale2.y, 0);
74103
}
75104

76105
public virtual Material GetModifiedMaterial(Material baseMaterial)
@@ -148,6 +177,15 @@ public virtual void SetVerticesDirty()
148177
}
149178
}
150179

180+
private void SetVerticesDirtyIfVisible()
181+
{
182+
if (CanModifyMesh())
183+
{
184+
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible;
185+
SetVerticesDirty();
186+
}
187+
}
188+
151189
public virtual void SetMaterialDirty()
152190
{
153191
if (graphic)

Packages/src/Runtime/UIEffectContext.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,12 @@ public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelp
462462
if (transitionRoot)
463463
{
464464
rect = transitionRoot.rect;
465-
rectMatrix = transitionRoot.worldToLocalMatrix
466-
* graphic.transform.localToWorldMatrix;
465+
if (transitionRoot != graphic.transform)
466+
{
467+
rectMatrix = transitionRoot.worldToLocalMatrix
468+
* graphic.transform.localToWorldMatrix;
469+
}
470+
467471
rot *= Matrix4x4.Scale(new Vector3(1 / multiplier, 1 / multiplier, 1))
468472
* rectMatrix;
469473
}

0 commit comments

Comments
 (0)