From 08e9ba73d78ba52502a6e795587441061377cd99 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Mon, 30 Dec 2024 02:44:32 +0900 Subject: [PATCH] feat: add `GradationMode.Angle` and `GradationMode.AngleGradient` --- Packages/src/Editor/UIEffectEditor.cs | 12 +- Packages/src/Runtime/Enums.cs | 4 +- Packages/src/Runtime/UIEffect.cs | 28 ++++- Packages/src/Runtime/UIEffectContext.cs | 32 ++++- Packages/src/Runtime/UIEffectTweener.cs | 3 +- .../src/Runtime/Utilities/GradientUtil.cs | 15 ++- .../src/Runtime/Utilities/UIVertexUtil.cs | 112 ++++++++++++++++-- 7 files changed, 186 insertions(+), 20 deletions(-) diff --git a/Packages/src/Editor/UIEffectEditor.cs b/Packages/src/Editor/UIEffectEditor.cs index c2122f4..dc353e3 100644 --- a/Packages/src/Editor/UIEffectEditor.cs +++ b/Packages/src/Editor/UIEffectEditor.cs @@ -66,6 +66,7 @@ namespace Coffee.UIEffects.Editors private SerializedProperty _gradationGradient; private SerializedProperty _gradationOffset; private SerializedProperty _gradationScale; + private SerializedProperty _gradationRotation; private bool _expandOthers; private SerializedProperty _allowToModifyMeshShape; @@ -128,6 +129,7 @@ namespace Coffee.UIEffects.Editors _gradationGradient = serializedObject.FindProperty("m_GradationGradient"); _gradationOffset = serializedObject.FindProperty("m_GradationOffset"); _gradationScale = serializedObject.FindProperty("m_GradationScale"); + _gradationRotation = serializedObject.FindProperty("m_GradationRotation"); _allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape"); } @@ -283,10 +285,10 @@ namespace Coffee.UIEffects.Editors { case GradationMode.HorizontalGradient: case GradationMode.VerticalGradient: + case GradationMode.AngleGradient: EditorGUILayout.PropertyField(_gradationGradient); break; default: - { EditorGUILayout.PropertyField(_gradationColor1); var r = EditorGUILayout.GetControlRect(); r.width -= 20; @@ -302,11 +304,17 @@ namespace Coffee.UIEffects.Editors } break; - } } EditorGUILayout.PropertyField(_gradationOffset); EditorGUILayout.PropertyField(_gradationScale); + + if ((GradationMode)_gradationMode.intValue == GradationMode.Angle + || (GradationMode)_gradationMode.intValue == GradationMode.AngleGradient) + { + EditorGUILayout.PropertyField(_gradationRotation); + } + EditorGUI.indentLevel--; } diff --git a/Packages/src/Runtime/Enums.cs b/Packages/src/Runtime/Enums.cs index 2966952..572d172 100644 --- a/Packages/src/Runtime/Enums.cs +++ b/Packages/src/Runtime/Enums.cs @@ -86,7 +86,9 @@ namespace Coffee.UIEffects RadialFast, RadialDetail, DiagonalToRightBottom, - DiagonalToLeftBottom + DiagonalToLeftBottom, + Angle, + AngleGradient } internal static class BlendTypeConverter diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index c37f346..3c20bd0 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -148,9 +148,11 @@ namespace Coffee.UIEffects protected GradationMode m_GradationMode = GradationMode.None; [SerializeField] + [ColorUsage(true, true)] protected Color m_GradationColor1 = Color.white; [SerializeField] + [ColorUsage(true, true)] protected Color m_GradationColor2 = Color.white; [SerializeField] @@ -164,6 +166,10 @@ namespace Coffee.UIEffects [SerializeField] protected float m_GradationScale = 1; + [SerializeField] + [Range(0, 360)] + private float m_GradationRotation = 0; + [SerializeField] protected bool m_AllowToModifyMeshShape = true; @@ -696,6 +702,18 @@ namespace Coffee.UIEffects } } + public float gradationRotation + { + get => m_GradationRotation; + set + { + value = Mathf.Repeat(value, 360); + if (Mathf.Approximately(m_GradationRotation, value)) return; + context.gradationRotation = m_GradationRotation = value; + SetVerticesDirty(); + } + } + public bool allowToModifyMeshShape { get => m_AllowToModifyMeshShape; @@ -819,6 +837,7 @@ namespace Coffee.UIEffects c.gradationGradient = m_GradationGradient; c.gradationOffset = m_GradationOffset; c.gradationScale = m_GradationScale; + c.gradationRotation = m_GradationRotation; c.allowToModifyMeshShape = m_AllowToModifyMeshShape; } @@ -855,10 +874,16 @@ namespace Coffee.UIEffects transitionRate = rate; } - if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.Gradiation)) + if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.GradiationOffset)) { gradationOffset = Mathf.Lerp(-1f, 1f, rate); } + + if ((gradationMode == GradationMode.Angle || gradationMode == GradationMode.AngleGradient) + && 0 < (mask & UIEffectTweener.CullingMask.GradiationRotation)) + { + gradationRotation = Mathf.Lerp(0f, 360f, rate); + } } public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) @@ -993,6 +1018,7 @@ namespace Coffee.UIEffects m_GradationGradient = c.gradationGradient; m_GradationOffset = c.gradationOffset; m_GradationScale = c.gradationScale; + m_GradationRotation = c.gradationRotation; m_AllowToModifyMeshShape = c.allowToModifyMeshShape; diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index 9e06bdd..d380136 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -181,8 +181,8 @@ namespace Coffee.UIEffects public Gradient gradationGradient; public float gradationOffset; public float gradationScale; + public float gradationRotation; private List _keyTimes; - private List _splitTimes; public bool allowToModifyMeshShape; @@ -253,6 +253,7 @@ namespace Coffee.UIEffects gradationGradient = preset.gradationGradient; gradationOffset = preset.gradationOffset; gradationScale = preset.gradationScale; + gradationRotation = preset.gradationRotation; allowToModifyMeshShape = preset.allowToModifyMeshShape; } @@ -442,6 +443,7 @@ namespace Coffee.UIEffects var offset = gradationOffset; var scale = gradationScale; var grad = gradationGradient; + var rot = gradationRotation; switch (gradationMode) { case GradationMode.Horizontal: @@ -462,6 +464,10 @@ namespace Coffee.UIEffects case GradationMode.RadialDetail: GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 12); break; + case GradationMode.Angle: + m = Matrix4x4.Rotate(Quaternion.Euler(0, 0, rot)) * m; + GradientUtil.DoHorizontalGradient(verts, a, b, offset, scale, rect, m); + break; case GradationMode.HorizontalGradient: { if (_keyTimes == null) @@ -503,6 +509,30 @@ namespace Coffee.UIEffects { GradientUtil.DoVerticalGradient(verts, grad, offset, scale, rect, m); } + + break; + } + case GradationMode.AngleGradient: + { + if (_keyTimes == null) + { + _keyTimes = InternalListPool.Rent(); + GradientUtil.GetKeyTimes(grad, _keyTimes); + } + + m = Matrix4x4.Rotate(Quaternion.Euler(0, 0, rot)) * m; + if (allowToModifyMeshShape) + { + var splitTimes = InternalListPool.Rent(); + GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale); + GradientUtil.DoAngleGradient(verts, grad, splitTimes, offset, scale, rect, m); + InternalListPool.Return(ref splitTimes); + } + else + { + GradientUtil.DoHorizontalGradient(verts, grad, offset, scale, rect, m); + } + break; } } diff --git a/Packages/src/Runtime/UIEffectTweener.cs b/Packages/src/Runtime/UIEffectTweener.cs index eb8bce7..77e9c79 100644 --- a/Packages/src/Runtime/UIEffectTweener.cs +++ b/Packages/src/Runtime/UIEffectTweener.cs @@ -16,7 +16,8 @@ namespace Coffee.UIEffects Color = 1 << 1, Sampling = 1 << 2, Transition = 1 << 3, - Gradiation = 1 << 5 + GradiationOffset = 1 << 5, + GradiationRotation = 1 << 6 } public enum UpdateMode diff --git a/Packages/src/Runtime/Utilities/GradientUtil.cs b/Packages/src/Runtime/Utilities/GradientUtil.cs index 9851e80..9fd5038 100644 --- a/Packages/src/Runtime/Utilities/GradientUtil.cs +++ b/Packages/src/Runtime/Utilities/GradientUtil.cs @@ -27,7 +27,7 @@ namespace Coffee.UIEffects UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb); if (i + 5 < count) { - UIVertexUtil.SplitHorizontal(verts, splitTimes, lb, lt, ref rb, ref rt, rect, m); + UIVertexUtil.SplitHorizontalFast(verts, splitTimes, lb, lt, ref rb, ref rt, rect, m); } lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).x + 0.0005f, offset, scale); @@ -51,7 +51,7 @@ namespace Coffee.UIEffects Rect rect, Matrix4x4 m) { var count = verts.Count; - for (var i = 0; i < count; i ++) + for (var i = 0; i < count; i++) { var vt = verts[i]; vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).x, offset, scale); @@ -90,7 +90,7 @@ namespace Coffee.UIEffects UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb); if (i + 5 < count) { - UIVertexUtil.SplitVertical(verts, splitTimes, lb, ref lt, rb, ref rt, rect, m); + UIVertexUtil.SplitVerticalFast(verts, splitTimes, lb, ref lt, rb, ref rt, rect, m); } lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).y + 0.0005f, offset, scale); @@ -114,7 +114,7 @@ namespace Coffee.UIEffects Rect rect, Matrix4x4 m) { var count = verts.Count; - for (var i = 0; i < count; i ++) + for (var i = 0; i < count; i++) { var vt = verts[i]; vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).y, offset, scale); @@ -215,6 +215,13 @@ namespace Coffee.UIEffects } } + public static void DoAngleGradient(List verts, Gradient gradient, List splitTimes, + float offset, float scale, Rect rect, Matrix4x4 m) + { + UIVertexUtil.SplitAngle(verts, splitTimes, rect, m); + DoHorizontalGradient(verts, gradient, offset, scale, rect, m); + } + public static void GetKeyTimes(Gradient gradient, List results) { results.Clear(); diff --git a/Packages/src/Runtime/Utilities/UIVertexUtil.cs b/Packages/src/Runtime/Utilities/UIVertexUtil.cs index 5e97fbd..1bf9a7f 100644 --- a/Packages/src/Runtime/Utilities/UIVertexUtil.cs +++ b/Packages/src/Runtime/Utilities/UIVertexUtil.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Coffee.UIEffectInternal; using UnityEngine; namespace Coffee.UIEffects @@ -79,14 +80,16 @@ namespace Coffee.UIEffects public static UIVertex VertexLerp(UIVertex a, UIVertex b, float t) { - var vt = new UIVertex(); - vt.position = Vector3.Lerp(a.position, b.position, t); - vt.normal = Vector3.Lerp(a.normal, b.normal, t); - vt.tangent = Vector4.Lerp(a.tangent, b.tangent, t); - vt.color = Color.Lerp(a.color, b.color, t); - vt.uv0 = Vector4.Lerp(a.uv0, b.uv0, t); - vt.uv1 = Vector4.Lerp(a.uv1, b.uv1, t); - vt.uv2 = Vector4.Lerp(a.uv2, b.uv2, t); + var vt = new UIVertex + { + position = Vector3.Lerp(a.position, b.position, t), + normal = Vector3.Lerp(a.normal, b.normal, t), + tangent = Vector4.Lerp(a.tangent, b.tangent, t), + color = Color.Lerp(a.color, b.color, t), + uv0 = Vector4.Lerp(a.uv0, b.uv0, t), + uv1 = Vector4.Lerp(a.uv1, b.uv1, t), + uv2 = Vector4.Lerp(a.uv2, b.uv2, t) + }; if (onLerpVertex != null) { @@ -143,7 +146,7 @@ namespace Coffee.UIEffects verts[i + 4] = rb; } - public static void SplitHorizontal(List verts, List times, UIVertex lb, UIVertex lt, + public static void SplitHorizontalFast(List verts, List times, UIVertex lb, UIVertex lt, ref UIVertex rb, ref UIVertex rt, Rect rect, Matrix4x4 m) { if (times == null || times.Count == 0) return; @@ -176,7 +179,7 @@ namespace Coffee.UIEffects rb = mb; } - public static void SplitVertical(List verts, List times, UIVertex lb, ref UIVertex lt, + public static void SplitVerticalFast(List verts, List times, UIVertex lb, ref UIVertex lt, UIVertex rb, ref UIVertex rt, Rect rect, Matrix4x4 m) { if (times == null || times.Count == 0) return; @@ -206,5 +209,94 @@ namespace Coffee.UIEffects lt = lm; rt = rm; } + + public static void SplitAngle(List verts, List times, Rect rect, Matrix4x4 m) + { + if (times == null || times.Count == 0) return; + + for (var i = 0; i < times.Count; i++) + { + var x = Mathf.Lerp(rect.xMin, rect.xMax, times[i]); + Split(verts, new Vector2(x, 0), new Vector2(0, 1), m); + } + + return; + + static void Split(List verts, Vector2 p, Vector2 dir, Matrix4x4 m) + { + // Each triangle + var intersections = InternalListPool.Rent(); + var count = verts.Count; + for (var i = 0; i < count; i += 3) + { + intersections.Clear(); + var n = -1; + // Each edge + for (var j = 0; j < 3; j++) + { + var start = verts[i + j]; + var end = verts[i + (j + 1) % 3]; + + if (DoesIntersect(start, end, p, dir, m, out var intersection)) + { + intersections.Add(intersection); + } + else + { + n = j; + } + } + + if (intersections.Count == 2) + { + var vt0 = verts[i + (n + 2) % 3]; + var vt1 = verts[i + (n + 0) % 3]; + var vt2 = verts[i + (n + 1) % 3]; + var it0 = intersections[0]; + var it1 = intersections[1]; + var it2 = intersections[(n + 1) % 2]; + verts[i + 0] = vt0; + verts[i + 1] = it0; + verts[i + 2] = it1; + verts.Add(it2); + verts.Add(vt1); + verts.Add(vt2); + verts.Add(vt2); + verts.Add(it0); + verts.Add(it1); + } + } + + InternalListPool.Return(ref intersections); + } + + static bool DoesIntersect(UIVertex start, UIVertex end, Vector2 p, Vector2 dir, Matrix4x4 m, + out UIVertex intersection) + { + intersection = default; + var startPosition = (Vector2)m.MultiplyPoint3x4(start.position); + var endPosition = (Vector2)m.MultiplyPoint3x4(end.position); + var segDir = endPosition - startPosition; + var cross = Vector3.Cross(segDir, dir); + + // Parallel. + if (cross.magnitude < Mathf.Epsilon) + { + return false; // 線と線分が平行 + } + + var diff = p - startPosition; + var determinant = Vector3.Dot(cross, cross); + var t = Vector3.Dot(Vector3.Cross(diff, dir), cross) / determinant; + + if (0 <= t && t <= 1) + { + intersection = VertexLerp(start, end, t); + return true; + } + + return false; + } + } } }