com.unity.uiextensions/Runtime/Scripts/Primitives/UISquircle.cs

141 lines
3.6 KiB
C#
Raw Normal View History

2020-08-18 23:21:17 +08:00
/// Credit Soprachev Andrei
using System.Collections.Generic;
2020-08-18 07:48:54 +08:00
using System.Linq;
using UnityEditor;
namespace UnityEngine.UI.Extensions
{
[AddComponentMenu("UI/Extensions/Primitives/Squircle")]
public class UISquircle : UIPrimitiveBase
{
const float C = 1.0f;
public enum Type
{
Classic,
Scaled
}
[Space]
2020-08-18 23:21:17 +08:00
public Type squircleType = Type.Scaled;
2020-08-18 07:48:54 +08:00
[Range(1, 40)]
public float n = 4;
[Min(0.1f)]
2020-08-18 23:21:17 +08:00
public float delta = 5f;
2020-08-18 07:48:54 +08:00
public float quality = 0.1f;
[Min(0)]
public float radius = 1000;
private float a, b;
private List<Vector2> vert = new List<Vector2>();
2020-08-18 23:21:17 +08:00
private float SquircleFunc(float t, bool xByY)
2020-08-18 07:48:54 +08:00
{
if (xByY)
return (float)System.Math.Pow(C - System.Math.Pow(t / a, n), 1f / n) * b;
return (float)System.Math.Pow(C - System.Math.Pow(t / b, n), 1f / n) * a;
}
2020-08-18 23:21:17 +08:00
2020-08-18 07:48:54 +08:00
protected override void OnPopulateMesh(VertexHelper vh)
{
float dx = 0;
float dy = 0;
float width = rectTransform.rect.width / 2;
float height = rectTransform.rect.height / 2;
if (squircleType == Type.Classic)
{
a = width;
b = height;
}
else
{
a = Mathf.Min(width, height, radius);
b = a;
dx = width - a;
dy = height - a;
}
float x = 0;
float y = 1;
vert.Clear();
vert.Add(new Vector2(0, height));
while (x < y)
{
2020-08-18 23:21:17 +08:00
y = SquircleFunc(x, true);
2020-08-18 07:48:54 +08:00
vert.Add(new Vector2(dx + x, dy + y));
x += delta;
}
if (float.IsNaN(vert.Last().y))
{
vert.RemoveAt(vert.Count - 1);
}
while (y > 0)
{
2020-08-18 23:21:17 +08:00
x = SquircleFunc(y, false);
2020-08-18 07:48:54 +08:00
vert.Add(new Vector2(dx + x, dy + y));
y -= delta;
}
vert.Add(new Vector2(width, 0));
for (int i = 1; i < vert.Count - 1; i++)
{
if (vert[i].x < vert[i].y)
{
if (vert[i - 1].y - vert[i].y < quality)
{
vert.RemoveAt(i);
i -= 1;
}
}
else
{
if (vert[i].x - vert[i - 1].x < quality)
{
vert.RemoveAt(i);
i -= 1;
}
}
}
vert.AddRange(vert.AsEnumerable().Reverse().Select(t => new Vector2(t.x, -t.y)));
vert.AddRange(vert.AsEnumerable().Reverse().Select(t => new Vector2(-t.x, t.y)));
vh.Clear();
for (int i = 0; i < vert.Count - 1; i++)
{
vh.AddVert(vert[i], color, Vector2.zero);
vh.AddVert(vert[i + 1], color, Vector2.zero);
vh.AddVert(Vector2.zero, color, Vector2.zero);
vh.AddTriangle(i * 3, i * 3 + 1, i * 3 + 2);
}
}
2020-08-19 06:48:53 +08:00
#if UNITY_EDITOR
2020-08-18 07:48:54 +08:00
[CustomEditor(typeof(UISquircle))]
2020-08-18 08:15:18 +08:00
public class UISquircleEditor : Editor
2020-08-18 07:48:54 +08:00
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
2020-08-18 23:21:17 +08:00
UISquircle script = (UISquircle)target;
2020-08-18 07:48:54 +08:00
GUILayout.Label("Vertex count: " + script.vert.Count().ToString());
}
}
2020-08-19 06:48:53 +08:00
#endif
2020-08-18 07:48:54 +08:00
}
}