feat: gradation feature

close #277, close #66
This commit is contained in:
mob-sakai
2024-12-26 17:45:24 +09:00
parent e9522d3f0e
commit bbe57df7a0
8 changed files with 517 additions and 11 deletions
+36
View File
@@ -60,6 +60,13 @@ namespace Coffee.UIEffects.Editors
private SerializedProperty _shadowColor;
private SerializedProperty _shadowColorGlow;
private SerializedProperty _gradationMode;
private SerializedProperty _gradationColor1;
private SerializedProperty _gradationColor2;
private SerializedProperty _gradationGradient;
private SerializedProperty _gradationOffset;
private SerializedProperty _gradationScale;
private bool _expandOthers;
private SerializedProperty _allowExtendVertex;
@@ -115,6 +122,13 @@ namespace Coffee.UIEffects.Editors
_shadowColor = serializedObject.FindProperty("m_ShadowColor");
_shadowColorGlow = serializedObject.FindProperty("m_ShadowColorGlow");
_gradationMode = serializedObject.FindProperty("m_GradationMode");
_gradationColor1 = serializedObject.FindProperty("m_GradationColor1");
_gradationColor2 = serializedObject.FindProperty("m_GradationColor2");
_gradationGradient = serializedObject.FindProperty("m_GradationGradient");
_gradationOffset = serializedObject.FindProperty("m_GradationOffset");
_gradationScale = serializedObject.FindProperty("m_GradationScale");
_allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex");
}
@@ -260,6 +274,28 @@ namespace Coffee.UIEffects.Editors
EditorGUI.indentLevel--;
}
// Gradient
DrawSeparator();
if (DrawHeaderPopup(_gradationMode))
{
EditorGUI.indentLevel++;
switch ((GradationMode)_gradationMode.intValue)
{
case GradationMode.HorizontalGradient:
case GradationMode.VerticalGradient:
EditorGUILayout.PropertyField(_gradationGradient);
break;
default:
EditorGUILayout.PropertyField(_gradationColor1);
EditorGUILayout.PropertyField(_gradationColor2);
break;
}
EditorGUILayout.PropertyField(_gradationOffset);
EditorGUILayout.PropertyField(_gradationScale);
EditorGUI.indentLevel--;
}
DrawSeparator();
_expandOthers = EditorGUILayout.BeginFoldoutHeaderGroup(_expandOthers, "Others");
if (_expandOthers)
+13
View File
@@ -76,6 +76,19 @@ namespace Coffee.UIEffects
Mirror
}
public enum GradationMode
{
None = 0,
Horizontal,
HorizontalGradient,
Vertical,
VerticalGradient,
RadialFast,
RadialDetail,
DiagonalToRightBottom,
DiagonalToLeftBottom
}
internal static class BlendTypeConverter
{
public static (BlendMode, BlendMode) Convert(this (BlendType type, BlendMode src, BlendMode dst) self)
+101
View File
@@ -144,6 +144,26 @@ namespace Coffee.UIEffects
[SerializeField]
protected bool m_ShadowColorGlow = false;
[SerializeField]
protected GradationMode m_GradationMode = GradationMode.None;
[SerializeField]
protected Color m_GradationColor1 = Color.white;
[SerializeField]
protected Color m_GradationColor2 = Color.white;
[SerializeField]
private Gradient m_GradationGradient = new Gradient();
[Range(-1, 1)]
[SerializeField]
protected float m_GradationOffset = 0;
[PowerRange(0.01f, 10, 10)]
[SerializeField]
protected float m_GradationScale = 1;
[SerializeField]
protected bool m_AllowExtendVertex = true;
@@ -597,6 +617,61 @@ namespace Coffee.UIEffects
}
}
public GradationMode gradationMode
{
get => m_GradationMode;
set
{
if (m_GradationMode == value) return;
context.gradationMode = m_GradationMode = value;
SetVerticesDirty();
}
}
public Color gradationColor1
{
get => m_GradationColor1;
set
{
if (m_GradationColor1 == value) return;
context.gradationColor1 = m_GradationColor1 = value;
SetVerticesDirty();
}
}
public Color gradationColor2
{
get => m_GradationColor2;
set
{
if (m_GradationColor2 == value) return;
context.gradationColor2 = m_GradationColor2 = value;
SetVerticesDirty();
}
}
public float gradationOffset
{
get => m_GradationOffset;
set
{
if (Mathf.Approximately(m_GradationOffset, value)) return;
context.gradationOffset = m_GradationOffset = value;
SetVerticesDirty();
}
}
public float gradationScale
{
get => m_GradationScale;
set
{
if (Mathf.Approximately(m_GradationScale, value)) return;
context.gradationScale = m_GradationScale = value;
SetVerticesDirty();
}
}
public bool allowExtendVertex
{
get => m_AllowExtendVertex;
@@ -633,6 +708,7 @@ namespace Coffee.UIEffects
protected override void OnValidate()
{
(m_SrcBlendMode, m_DstBlendMode) = (m_BlendType, m_SrcBlendMode, m_DstBlendMode).Convert();
context?.SetGradationDirty();
base.OnValidate();
}
#endif
@@ -657,6 +733,13 @@ namespace Coffee.UIEffects
});
}
public void SetGradientKeys(GradientColorKey[] colorKeys, GradientAlphaKey[] alphaKeys)
{
m_GradationGradient ??= new Gradient();
m_GradationGradient.SetKeys(colorKeys, alphaKeys);
context?.SetGradationDirty();
}
protected override void UpdateContext(UIEffectContext c)
{
c.toneFilter = m_ToneFilter;
@@ -696,6 +779,12 @@ namespace Coffee.UIEffects
c.shadowColorFilter = m_ShadowColorFilter;
c.shadowColor = m_ShadowColor;
c.shadowColorGlow = m_ShadowColorGlow;
c.gradationMode = m_GradationMode;
c.gradationColor1 = m_GradationColor1;
c.gradationColor2 = m_GradationColor2;
c.gradationGradient = m_GradationGradient;
c.gradationOffset = m_GradationOffset;
c.gradationScale = m_GradationScale;
c.allowExtendVertex = m_AllowExtendVertex;
}
@@ -730,6 +819,11 @@ namespace Coffee.UIEffects
{
transitionRate = rate;
}
if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.Gradiation))
{
gradationOffset = Mathf.Lerp(-1f, 1f, rate);
}
}
public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
@@ -852,6 +946,13 @@ namespace Coffee.UIEffects
m_ShadowColor = c.shadowColor;
m_ShadowColorGlow = c.shadowColorGlow;
m_GradationMode = c.gradationMode;
m_GradationColor1 = c.gradationColor1;
m_GradationColor2 = c.gradationColor2;
m_GradationGradient = c.gradationGradient;
m_GradationOffset = c.gradationOffset;
m_GradationScale = c.gradationScale;
m_AllowExtendVertex = c.allowExtendVertex;
UpdateContext(context);
+87 -2
View File
@@ -175,6 +175,15 @@ namespace Coffee.UIEffects
public Color shadowColor;
public bool shadowColorGlow;
public GradationMode gradationMode;
public Color gradationColor1;
public Color gradationColor2;
public Gradient gradationGradient;
public float gradationOffset;
public float gradationScale;
private List<float> _keyTimes;
private List<float> _splitTimes;
public bool allowExtendVertex;
@@ -188,6 +197,7 @@ namespace Coffee.UIEffects
public void Reset()
{
InternalListPool<float>.Return(ref _keyTimes);
CopyFrom(s_DefaultContext);
}
@@ -237,9 +247,21 @@ namespace Coffee.UIEffects
shadowColor = preset.shadowColor;
shadowColorGlow = preset.shadowColorGlow;
gradationMode = preset.gradationMode;
gradationColor1 = preset.gradationColor1;
gradationColor2 = preset.gradationColor2;
gradationGradient = preset.gradationGradient;
gradationOffset = preset.gradationOffset;
gradationScale = preset.gradationScale;
allowExtendVertex = preset.allowExtendVertex;
}
public void SetGradationDirty()
{
InternalListPool<float>.Return(ref _keyTimes);
}
public void ApplyToMaterial(Material material, float actualSamplingScale = 1f)
{
if (!material) return;
@@ -353,6 +375,7 @@ namespace Coffee.UIEffects
// Get the rectangle to calculate the normalized position.
vh.GetUIVertexStream(verts);
var bundleSize = isText ? 6 : count;
var rectMatrix = Matrix4x4.identity;
var rot = Matrix4x4.Rotate(Quaternion.Euler(0, 0, transitionRotation));
var v1 = rot.MultiplyPoint3x4(new Vector3(1, 1, 0));
var multiplier = Mathf.Max(Mathf.Abs(v1.x), Mathf.Abs(v1.y));
@@ -361,9 +384,10 @@ namespace Coffee.UIEffects
if (transitionRoot)
{
rect = transitionRoot.rect;
rectMatrix = transitionRoot.worldToLocalMatrix
* graphic.transform.localToWorldMatrix;
rot *= Matrix4x4.Scale(new Vector3(1 / multiplier, 1 / multiplier, 1))
* transitionRoot.worldToLocalMatrix
* graphic.rectTransform.localToWorldMatrix;
* rectMatrix;
}
else
{
@@ -401,6 +425,9 @@ namespace Coffee.UIEffects
}
}
// Apply gradation.
ApplyGradation(verts, transitionRoot.rect, rectMatrix);
// Apply shadow.
ApplyShadow(transitionRoot, verts);
@@ -408,6 +435,64 @@ namespace Coffee.UIEffects
vh.AddUIVertexTriangleStream(verts);
}
private void ApplyGradation(List<UIVertex> verts, Rect rect, Matrix4x4 m)
{
var a = gradationColor1;
var b = gradationColor2;
var offset = gradationOffset;
var scale = gradationScale;
var grad = gradationGradient;
switch (gradationMode)
{
case GradationMode.Horizontal:
GradientUtil.DoHorizontalGradient(verts, a, b, offset, scale, rect, m);
break;
case GradationMode.Vertical:
GradientUtil.DoVerticalGradient(verts, a, b, offset, scale, rect, m);
break;
case GradationMode.DiagonalToRightBottom:
GradientUtil.DoDiagonalGradientToRightBottom(verts, a, b, offset, scale, rect, m);
break;
case GradationMode.DiagonalToLeftBottom:
GradientUtil.DoDiagonalGradientToLeftBottom(verts, a, b, offset, scale, rect, m);
break;
case GradationMode.RadialFast:
GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 4);
break;
case GradationMode.RadialDetail:
GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 12);
break;
case GradationMode.HorizontalGradient:
{
if (_keyTimes == null)
{
_keyTimes = InternalListPool<float>.Rent();
GradientUtil.GetKeyTimes(grad, _keyTimes);
}
var splitTimes = InternalListPool<float>.Rent();
GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale);
GradientUtil.DoHorizontalGradient(verts, grad, splitTimes, offset, scale, rect, m);
InternalListPool<float>.Return(ref splitTimes);
break;
}
case GradationMode.VerticalGradient:
{
if (_keyTimes == null)
{
_keyTimes = InternalListPool<float>.Rent();
GradientUtil.GetKeyTimes(grad, _keyTimes);
}
var splitTimes = InternalListPool<float>.Rent();
GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale);
GradientUtil.DoVerticalGradient(verts, grad, splitTimes, offset, scale, rect, m);
InternalListPool<float>.Return(ref splitTimes);
break;
}
}
}
private void ApplyShadow(RectTransform transitionRoot, List<UIVertex> verts)
{
switch (shadowMode)
+2 -1
View File
@@ -14,7 +14,8 @@ namespace Coffee.UIEffects
Tone = 1 << 0,
Color = 1 << 1,
Sampling = 1 << 2,
Transition = 1 << 3
Transition = 1 << 3,
Gradiation = 1 << 5
}
public enum UpdateMode
@@ -0,0 +1,259 @@
using System.Collections.Generic;
using UnityEngine;
namespace Coffee.UIEffects
{
internal static class GradientUtil
{
public static void DoHorizontalGradient(List<UIVertex> verts, Color a, Color b, float offset, float scale,
Rect rect, Matrix4x4 m)
{
for (var i = 0; i < verts.Count; i++)
{
var vt = verts[i];
var x = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(vt.position).x);
var t = Mathf.Clamp01(offset + (x - 0.5f) * scale + 0.5f);
vt.color *= Color.Lerp(a, b, t);
verts[i] = vt;
}
}
public static void DoHorizontalGradient(List<UIVertex> verts, Gradient gradient, List<float> splitTimes,
float offset, float scale, Rect rect, Matrix4x4 m)
{
var count = verts.Count;
for (var i = 0; i < verts.Count; i += 6)
{
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);
}
lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).x + 0.0005f, offset, scale);
lt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lt.position).x + 0.0005f, offset, scale);
rt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(rt.position).x - 0.0005f, offset, scale);
rb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(rb.position).x - 0.0005f, offset, scale);
UIVertexUtil.SetQuad(verts, i, lb, lt, rt, rb);
}
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<UIVertex> verts, Color a, Color b, float offset, float scale,
Rect rect, Matrix4x4 m)
{
for (var i = 0; i < verts.Count; i++)
{
var vt = verts[i];
var y = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(vt.position).y);
var t = Mathf.Clamp01(offset + (0.5f - y) * scale + 0.5f);
vt.color *= Color.Lerp(a, b, t);
verts[i] = vt;
}
}
public static void DoVerticalGradient(List<UIVertex> verts, Gradient gradient, List<float> splitTimes,
float offset, float scale, Rect rect, Matrix4x4 m)
{
var count = verts.Count;
for (var i = 0; i < verts.Count; i += 6)
{
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);
}
lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).y + 0.0005f, offset, scale);
lt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lt.position).y - 0.0005f, offset, scale);
rt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(rt.position).y - 0.0005f, offset, scale);
rb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(rb.position).y + 0.0005f, offset, scale);
UIVertexUtil.SetQuad(verts, i, lb, lt, rt, rb);
}
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<UIVertex> verts, Color a, Color b, float offset,
float scale, Rect rect, Matrix4x4 m)
{
for (var i = 0; i < verts.Count; i++)
{
var vt = verts[i];
var x = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(vt.position).x);
var y = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(vt.position).y);
var t = Mathf.Clamp01(0.5f * scale + 0.5f + offset - (1 - x + y) * scale / 2);
vt.color *= Color.Lerp(a, b, t);
verts[i] = vt;
}
}
public static void DoDiagonalGradientToLeftBottom(List<UIVertex> verts, Color a, Color b, float offset,
float scale, Rect rect, Matrix4x4 m)
{
for (var i = 0; i < verts.Count; i++)
{
var vt = verts[i];
var x = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(vt.position).x);
var y = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(vt.position).y);
var t = Mathf.Clamp01(0.5f * scale + 0.5f + offset - (x + y) * scale / 2);
vt.color *= Color.Lerp(a, b, t);
verts[i] = vt;
}
}
public static void DoRadialGradient(List<UIVertex> verts, Color a, Color b, float offset, float scale,
Rect rect, Matrix4x4 m, int divide)
{
var count = verts.Count;
for (var i = 0; i < count; i += 6)
{
UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb);
for (var j = 1; j < divide; j++)
{
var min = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(lb.position).x);
var max = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(rt.position).x);
var time = 1f - (float)j / divide;
if (time <= min || max <= time) continue;
UIVertexUtil.SplitHorizontal(verts, lb, lt, ref rb, ref rt, Mathf.InverseLerp(min, max, time));
}
UIVertexUtil.SetQuad(verts, i, lb, lt, rt, rb);
}
count = verts.Count;
for (var i = 0; i < verts.Count; i += 6)
{
UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb);
if (i + 5 < count)
{
for (var j = 1; j < divide; j++)
{
var min = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(lb.position).y);
var max = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(rt.position).y);
var time = 1f - (float)j / divide;
if (time <= min || max <= time) continue;
UIVertexUtil.SplitVertical(verts, lb, ref lt, rb, ref rt, Mathf.InverseLerp(min, max, time));
}
}
lb.color *= DoGradient(lb, a, b, rect, m, offset, scale);
lt.color *= DoGradient(lt, a, b, rect, m, offset, scale);
rt.color *= DoGradient(rt, a, b, rect, m, offset, scale);
rb.color *= DoGradient(rb, a, b, rect, m, offset, scale);
UIVertexUtil.SetQuad(verts, i, lb, lt, rt, rb);
}
return;
static Color DoGradient(UIVertex vt, Color a, Color b, Rect rect, Matrix4x4 m, float offset, float scale)
{
var x = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(vt.position).x) - 0.5f;
var y = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(vt.position).y) - 0.5f;
var t = Mathf.Clamp01(offset + Mathf.Sqrt(x * x + y * y) * 2 * scale);
return Color.Lerp(a, b, t);
}
}
public static void GetKeyTimes(Gradient gradient, List<float> results)
{
results.Clear();
var colors = gradient.colorKeys;
for (var i = 0; i < colors.Length; i++)
{
AddKeyTime(results, colors[i].time);
}
var alphas = gradient.alphaKeys;
for (var i = 0; i < alphas.Length; i++)
{
AddKeyTime(results, alphas[i].time);
}
AddKeyTime(results, 0);
AddKeyTime(results, 1);
results.Sort((a, b) => b.CompareTo(a));
return;
static void AddKeyTime(List<float> results, float time)
{
if (!Contains(results, time))
{
results.Add(time);
}
}
static bool Contains(List<float> list, float v)
{
for (var i = 0; i < list.Count; i++)
{
if (Mathf.Abs(list[i] - v) < 0.001f) return true;
}
return false;
}
}
public static void SplitKeyTimes(List<float> keyTimes, List<float> results, float offset, float scale)
{
results.Clear();
for (var i = 0; i < keyTimes.Count; i++)
{
AddSplitTime(results, keyTimes[i], offset, scale);
}
results.Sort((a, b) => b.CompareTo(a));
return;
static void AddSplitTime(List<float> results, float time, float offset, float scale)
{
time = time * scale + offset * scale + (1 - scale) / 2;
while (0 < time)
{
time -= scale;
}
while (time < 1f)
{
if (0 < time && !Contains(results, time))
{
results.Add(time);
}
time += scale;
}
}
static bool Contains(List<float> list, float v)
{
for (var i = 0; i < list.Count; i++)
{
if (Mathf.Abs(list[i] - v) < 0.001f) return true;
}
return false;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23cc1f211cd264050a641c01c4a6d77a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -143,18 +143,18 @@ namespace Coffee.UIEffects
verts[i + 4] = rb;
}
public static void SplitHorizontal(List<UIVertex> verts, UIVertex lb, UIVertex lt, ref UIVertex rb,
ref UIVertex rt, Rect rect, List<float> times)
public static void SplitHorizontal(List<UIVertex> verts, List<float> times, UIVertex lb, UIVertex lt,
ref UIVertex rb, ref UIVertex rt, Rect rect, Matrix4x4 m)
{
if (times == null || times.Count == 0) return;
var min = Mathf.InverseLerp(rect.xMin, rect.xMax, lb.position.x);
var min = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(lb.position).x);
for (var i = 0; i < times.Count; i++)
{
var time = times[i];
if (time <= 0 || 1 <= time) continue;
var max = Mathf.InverseLerp(rect.xMin, rect.xMax, rt.position.x);
var max = Mathf.InverseLerp(rect.xMin, rect.xMax, m.MultiplyPoint3x4(rt.position).x);
if (time <= min || max <= time) continue;
SplitHorizontal(verts, lb, lt, ref rb, ref rt, Mathf.InverseLerp(min, max, time));
@@ -176,16 +176,16 @@ namespace Coffee.UIEffects
rb = mb;
}
public static void SplitVertical(List<UIVertex> verts, UIVertex lb, ref UIVertex lt, UIVertex rb,
ref UIVertex rt, Rect rect, List<float> times)
public static void SplitVertical(List<UIVertex> verts, List<float> times, UIVertex lb, ref UIVertex lt,
UIVertex rb, ref UIVertex rt, Rect rect, Matrix4x4 m)
{
if (times == null || times.Count == 0) return;
var min = Mathf.InverseLerp(rect.yMin, rect.yMax, lb.position.y);
var min = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(lb.position).y);
for (var i = 0; i < times.Count; i++)
{
var time = times[i];
var max = Mathf.InverseLerp(rect.yMin, rect.yMax, rt.position.y);
var max = Mathf.InverseLerp(rect.yMin, rect.yMax, m.MultiplyPoint3x4(rt.position).y);
if (time <= min || max <= time) continue;
SplitVertical(verts, lb, ref lt, rb, ref rt, Mathf.InverseLerp(min, max, time));