fix: TransitionFilter and GradationMode do not work correctly when the initial scale is 0

close #311
This commit is contained in:
mob-sakai
2025-02-27 09:29:51 +09:00
parent 8155f94943
commit bdbf293c14
2 changed files with 45 additions and 3 deletions
+39 -1
View File
@@ -1,3 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using Coffee.UIEffectInternal;
using UnityEngine;
@@ -21,6 +22,7 @@ namespace Coffee.UIEffects
private Graphic _graphic;
private Material _material;
private UIEffectContext _context;
private Action _setVerticesDirtyIfVisible;
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();
public virtual uint effectId => (uint)GetInstanceID();
@@ -52,6 +54,11 @@ namespace Coffee.UIEffects
protected override void OnDisable()
{
if (_setVerticesDirtyIfVisible != null)
{
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible;
}
MaterialRepository.Release(ref _material);
SetMaterialDirty();
SetVerticesDirty();
@@ -59,6 +66,8 @@ namespace Coffee.UIEffects
protected override void OnDestroy()
{
_setVerticesDirtyIfVisible = null;
_graphic = null;
s_ContextPool.Return(ref _context);
}
@@ -70,7 +79,27 @@ namespace Coffee.UIEffects
{
if (!isActiveAndEnabled || context == null) return;
context.ModifyMesh(graphic, transitionRoot, vh, canModifyShape);
if (CanModifyMesh())
{
context.ModifyMesh(graphic, transitionRoot, vh, canModifyShape);
}
// If you can't modify the mesh, retry next frame.
else
{
UIExtraCallbacks.onBeforeCanvasRebuild += _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible;
}
}
private bool CanModifyMesh()
{
// The transitionRoot is same as the transform => true.
var root = transitionRoot;
if (transform == root) return true;
// The transitionRoot is not visible => false.
var scale1 = root.lossyScale;
var scale2 = transform.lossyScale;
return !Mathf.Approximately(scale1.x * scale1.y * scale2.x * scale2.y, 0);
}
public virtual Material GetModifiedMaterial(Material baseMaterial)
@@ -148,6 +177,15 @@ namespace Coffee.UIEffects
}
}
private void SetVerticesDirtyIfVisible()
{
if (CanModifyMesh())
{
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible;
SetVerticesDirty();
}
}
public virtual void SetMaterialDirty()
{
if (graphic)
+6 -2
View File
@@ -462,8 +462,12 @@ namespace Coffee.UIEffects
if (transitionRoot)
{
rect = transitionRoot.rect;
rectMatrix = transitionRoot.worldToLocalMatrix
* graphic.transform.localToWorldMatrix;
if (transitionRoot != graphic.transform)
{
rectMatrix = transitionRoot.worldToLocalMatrix
* graphic.transform.localToWorldMatrix;
}
rot *= Matrix4x4.Scale(new Vector3(1 / multiplier, 1 / multiplier, 1))
* rectMatrix;
}