diff --git a/Packages/src/Editor/UIEffectEditor.cs b/Packages/src/Editor/UIEffectEditor.cs index 0c326a8..c2122f4 100644 --- a/Packages/src/Editor/UIEffectEditor.cs +++ b/Packages/src/Editor/UIEffectEditor.cs @@ -68,7 +68,7 @@ namespace Coffee.UIEffects.Editors private SerializedProperty _gradationScale; private bool _expandOthers; - private SerializedProperty _allowExtendVertex; + private SerializedProperty _allowToModifyMeshShape; private void OnEnable() { @@ -129,7 +129,7 @@ namespace Coffee.UIEffects.Editors _gradationOffset = serializedObject.FindProperty("m_GradationOffset"); _gradationScale = serializedObject.FindProperty("m_GradationScale"); - _allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex"); + _allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape"); } public override void OnInspectorGUI() @@ -314,7 +314,7 @@ namespace Coffee.UIEffects.Editors _expandOthers = EditorGUILayout.BeginFoldoutHeaderGroup(_expandOthers, "Others"); if (_expandOthers) { - EditorGUILayout.PropertyField(_allowExtendVertex); + EditorGUILayout.PropertyField(_allowToModifyMeshShape); } } diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index 5baef84..c37f346 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -165,7 +165,7 @@ namespace Coffee.UIEffects protected float m_GradationScale = 1; [SerializeField] - protected bool m_AllowExtendVertex = true; + protected bool m_AllowToModifyMeshShape = true; /// /// Tone filter for rendering. @@ -696,13 +696,13 @@ namespace Coffee.UIEffects } } - public bool allowExtendVertex + public bool allowToModifyMeshShape { - get => m_AllowExtendVertex; + get => m_AllowToModifyMeshShape; set { - if (m_AllowExtendVertex == value) return; - context.allowExtendVertex = m_AllowExtendVertex = value; + if (m_AllowToModifyMeshShape == value) return; + context.allowToModifyMeshShape = m_AllowToModifyMeshShape = value; SetVerticesDirty(); } } @@ -819,7 +819,8 @@ namespace Coffee.UIEffects c.gradationGradient = m_GradationGradient; c.gradationOffset = m_GradationOffset; c.gradationScale = m_GradationScale; - c.allowExtendVertex = m_AllowExtendVertex; + + c.allowToModifyMeshShape = m_AllowToModifyMeshShape; } public override void ApplyContextToMaterial() @@ -993,7 +994,7 @@ namespace Coffee.UIEffects m_GradationOffset = c.gradationOffset; m_GradationScale = c.gradationScale; - m_AllowExtendVertex = c.allowExtendVertex; + m_AllowToModifyMeshShape = c.allowToModifyMeshShape; UpdateContext(context); ApplyContextToMaterial(); diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index 927ce65..9e06bdd 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -184,7 +184,7 @@ namespace Coffee.UIEffects private List _keyTimes; private List _splitTimes; - public bool allowExtendVertex; + public bool allowToModifyMeshShape; public bool willModifyMaterial => samplingFilter != SamplingFilter.None @@ -254,7 +254,7 @@ namespace Coffee.UIEffects gradationOffset = preset.gradationOffset; gradationScale = preset.gradationScale; - allowExtendVertex = preset.allowExtendVertex; + allowToModifyMeshShape = preset.allowToModifyMeshShape; } public void SetGradationDirty() @@ -470,10 +470,18 @@ namespace Coffee.UIEffects GradientUtil.GetKeyTimes(grad, _keyTimes); } - var splitTimes = InternalListPool.Rent(); - GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale); - GradientUtil.DoHorizontalGradient(verts, grad, splitTimes, offset, scale, rect, m); - InternalListPool.Return(ref splitTimes); + if (allowToModifyMeshShape) + { + var splitTimes = InternalListPool.Rent(); + GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale); + GradientUtil.DoHorizontalGradient(verts, grad, splitTimes, offset, scale, rect, m); + InternalListPool.Return(ref splitTimes); + } + else + { + GradientUtil.DoHorizontalGradient(verts, grad, offset, scale, rect, m); + } + break; } case GradationMode.VerticalGradient: @@ -484,10 +492,17 @@ namespace Coffee.UIEffects GradientUtil.GetKeyTimes(grad, _keyTimes); } - var splitTimes = InternalListPool.Rent(); - GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale); - GradientUtil.DoVerticalGradient(verts, grad, splitTimes, offset, scale, rect, m); - InternalListPool.Return(ref splitTimes); + if (allowToModifyMeshShape) + { + var splitTimes = InternalListPool.Rent(); + GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale); + GradientUtil.DoVerticalGradient(verts, grad, splitTimes, offset, scale, rect, m); + InternalListPool.Return(ref splitTimes); + } + else + { + GradientUtil.DoVerticalGradient(verts, grad, offset, scale, rect, m); + } break; } } @@ -512,7 +527,7 @@ namespace Coffee.UIEffects private Vector2 GetExpandSize() { - if (!allowExtendVertex) return Vector2.zero; + if (!allowToModifyMeshShape) return Vector2.zero; var expandSize = Vector2.zero; switch (samplingFilter) diff --git a/Packages/src/Runtime/Utilities/GradientUtil.cs b/Packages/src/Runtime/Utilities/GradientUtil.cs index c717477..9851e80 100644 --- a/Packages/src/Runtime/Utilities/GradientUtil.cs +++ b/Packages/src/Runtime/Utilities/GradientUtil.cs @@ -47,6 +47,27 @@ namespace Coffee.UIEffects } } + public static void DoHorizontalGradient(List verts, Gradient gradient, float offset, float scale, + Rect rect, Matrix4x4 m) + { + var count = verts.Count; + for (var i = 0; i < count; i ++) + { + var vt = verts[i]; + vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).x, offset, scale); + verts[i] = vt; + } + + return; + + static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float scale) + { + t = Mathf.InverseLerp(rect.xMin, rect.xMax, t); // 0-1 + t = Mathf.Repeat((t - offset * scale - (1 - scale) / 2) / scale, 1); // Revert + return gradient.Evaluate(t); + } + } + public static void DoVerticalGradient(List verts, Color a, Color b, float offset, float scale, Rect rect, Matrix4x4 m) { @@ -89,6 +110,27 @@ namespace Coffee.UIEffects } } + public static void DoVerticalGradient(List verts, Gradient gradient, float offset, float scale, + Rect rect, Matrix4x4 m) + { + var count = verts.Count; + for (var i = 0; i < count; i ++) + { + var vt = verts[i]; + vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).y, offset, scale); + verts[i] = vt; + } + + return; + + static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float scale) + { + t = Mathf.InverseLerp(rect.yMin, rect.yMax, t); // 0-1 + t = Mathf.Repeat((t - offset * scale - (1 - scale) / 2) / scale, 1); // Revert + return gradient.Evaluate(t); + } + } + public static void DoDiagonalGradientToRightBottom(List verts, Color a, Color b, float offset, float scale, Rect rect, Matrix4x4 m) {