From bdbf293c1489c0b248078969b37ec66890620d44 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Thu, 27 Feb 2025 09:29:51 +0900 Subject: [PATCH] fix: `TransitionFilter` and `GradationMode` do not work correctly when the initial scale is 0 close #311 --- Packages/src/Runtime/UIEffectBase.cs | 40 ++++++++++++++++++++++++- Packages/src/Runtime/UIEffectContext.cs | 8 +++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Packages/src/Runtime/UIEffectBase.cs b/Packages/src/Runtime/UIEffectBase.cs index 39c7150..585febe 100644 --- a/Packages/src/Runtime/UIEffectBase.cs +++ b/Packages/src/Runtime/UIEffectBase.cs @@ -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(); 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) diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index be7ecdf..c202499 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -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; }