feat: custom transform for transition and gradation

This commit is contained in:
mob-sakai
2025-03-11 19:40:15 +09:00
parent 6542543422
commit 045db9c3b7
4 changed files with 72 additions and 39 deletions
+3
View File
@@ -101,6 +101,7 @@ namespace Coffee.UIEffects.Editors
private SerializedProperty _patternArea;
private SerializedProperty _allowToModifyMeshShape;
private SerializedProperty _customRoot;
private static bool expandOthers
{
@@ -183,6 +184,7 @@ namespace Coffee.UIEffects.Editors
_gradationRotation = serializedObject.FindProperty("m_GradationRotation");
_allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape");
_customRoot = serializedObject.FindProperty("m_CustomRoot");
}
public override void OnInspectorGUI()
@@ -413,6 +415,7 @@ namespace Coffee.UIEffects.Editors
{
EditorGUILayout.PropertyField(_samplingScale);
EditorGUILayout.PropertyField(_allowToModifyMeshShape);
EditorGUILayout.PropertyField(_customRoot);
}
}
+27
View File
@@ -216,6 +216,9 @@ namespace Coffee.UIEffects
[SerializeField]
protected PatternArea m_PatternArea = PatternArea.Inner;
[SerializeField]
protected RectTransform m_CustomRoot = null;
/// <summary>
/// Tone filter for rendering.
/// </summary>
@@ -928,6 +931,21 @@ namespace Coffee.UIEffects
}
}
public RectTransform customRoot
{
get => m_CustomRoot;
set
{
if (m_CustomRoot == value) return;
m_CustomRoot = value;
SetVerticesDirty();
}
}
public override RectTransform transitionRoot => m_CustomRoot
? m_CustomRoot
: transform as RectTransform;
public List<UIEffectReplica> replicas => _replicas ??= InternalListPool<UIEffectReplica>.Rent();
private List<UIEffectReplica> _replicas;
@@ -958,6 +976,15 @@ namespace Coffee.UIEffects
}
#endif
protected override void OnBeforeCanvasRebuild()
{
base.OnBeforeCanvasRebuild();
if (CheckTransformChangedWith(customRoot))
{
SetVerticesDirty();
}
}
public override void SetVerticesDirty()
{
base.SetVerticesDirty();
+26 -19
View File
@@ -22,7 +22,9 @@ namespace Coffee.UIEffects
private Graphic _graphic;
private Material _material;
private UIEffectContext _context;
private Action _setVerticesDirtyIfVisible;
private Action _onBeforeCanvasRebuild;
private bool _canModifyMesh;
private Matrix4x4 _prevTransformHash;
public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent<Graphic>();
public virtual uint effectId => (uint)GetInstanceID();
@@ -47,6 +49,8 @@ namespace Coffee.UIEffects
protected override void OnEnable()
{
_onBeforeCanvasRebuild ??= OnBeforeCanvasRebuild;
UIExtraCallbacks.onBeforeCanvasRebuild += _onBeforeCanvasRebuild;
UpdateContext(context);
SetMaterialDirty();
SetVerticesDirty();
@@ -54,9 +58,9 @@ namespace Coffee.UIEffects
protected override void OnDisable()
{
if (_setVerticesDirtyIfVisible != null)
if (_onBeforeCanvasRebuild != null)
{
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible;
UIExtraCallbacks.onBeforeCanvasRebuild -= _onBeforeCanvasRebuild;
}
MaterialRepository.Release(ref _material);
@@ -66,11 +70,19 @@ namespace Coffee.UIEffects
protected override void OnDestroy()
{
_setVerticesDirtyIfVisible = null;
_onBeforeCanvasRebuild = null;
_graphic = null;
s_ContextPool.Return(ref _context);
}
protected virtual void OnBeforeCanvasRebuild()
{
if (!_canModifyMesh && CanModifyMesh())
{
SetVerticesDirty();
}
}
public void ModifyMesh(Mesh mesh)
{
}
@@ -79,15 +91,11 @@ namespace Coffee.UIEffects
{
if (!isActiveAndEnabled || context == null) return;
if (CanModifyMesh())
_canModifyMesh = CanModifyMesh();
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()
@@ -170,15 +178,6 @@ namespace Coffee.UIEffects
}
}
private void SetVerticesDirtyIfVisible()
{
if (CanModifyMesh())
{
UIExtraCallbacks.onBeforeCanvasRebuild -= _setVerticesDirtyIfVisible ??= SetVerticesDirtyIfVisible;
SetVerticesDirty();
}
}
public virtual void SetMaterialDirty()
{
if (graphic)
@@ -188,6 +187,14 @@ namespace Coffee.UIEffects
}
}
protected bool CheckTransformChangedWith(Transform root)
{
var sensitivity = UIEffectProjectSettings.transformSensitivity;
return root
&& root != transform
&& transform.HasChanged(root, ref _prevTransformHash, sensitivity);
}
internal void ReleaseMaterial()
{
MaterialRepository.Release(ref _material);
+16 -20
View File
@@ -17,7 +17,6 @@ namespace Coffee.UIEffects
protected bool m_AllowToModifyMeshShape = true;
private UIEffect _currentTarget;
private Matrix4x4 _prevTransformHash;
public UIEffect target
{
@@ -69,18 +68,21 @@ namespace Coffee.UIEffects
protected override void OnEnable()
{
RefreshTarget(target);
UIExtraCallbacks.onBeforeCanvasRebuild += SetVerticesDirtyIfTransformChanged;
CheckTransform();
base.OnEnable();
}
protected override void OnDisable()
{
RefreshTarget(null);
UIExtraCallbacks.onBeforeCanvasRebuild -= SetVerticesDirtyIfTransformChanged;
base.OnDisable();
}
protected override void OnDestroy()
{
_currentTarget = null;
base.OnDestroy();
}
#if UNITY_EDITOR
protected override void OnValidate()
{
@@ -104,6 +106,16 @@ namespace Coffee.UIEffects
}
}
protected override void OnBeforeCanvasRebuild()
{
base.OnBeforeCanvasRebuild();
if (useTargetTransform && target && CheckTransformChangedWith(target.transform))
{
SetVerticesDirty();
}
}
protected override void UpdateContext(UIEffectContext c)
{
}
@@ -123,21 +135,5 @@ namespace Coffee.UIEffects
{
return !target || target.IsRaycastLocationValid(sp, eventCamera);
}
private bool CheckTransform()
{
return useTargetTransform
&& target
&& transform.HasChanged(target.transform, ref _prevTransformHash,
UIEffectProjectSettings.transformSensitivity);
}
private void SetVerticesDirtyIfTransformChanged()
{
if (CheckTransform())
{
SetVerticesDirty();
}
}
}
}