Add squircle priminive
parent
214b4a1a69
commit
dfa0d963df
|
@ -1208,6 +1208,14 @@ namespace UnityEditor.UI
|
|||
Selection.activeGameObject = go;
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Primitives/UI Squircle", false)]
|
||||
static public void AddUISquircle(MenuCommand menuCommand)
|
||||
{
|
||||
GameObject go = CreateUIElementRoot("UI Squircle", menuCommand, s_ImageGUIElementSize);
|
||||
go.AddComponent<UISquircle>();
|
||||
Selection.activeGameObject = go;
|
||||
}
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Primitives/UI Circle", false)]
|
||||
static public void AddUICircle(MenuCommand menuCommand)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
using System.Collections.Generic;
|
||||
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]
|
||||
public Type squircleType;
|
||||
[Range(1, 40)]
|
||||
public float n = 4;
|
||||
[Min(0.1f)]
|
||||
public float delta = 10f;
|
||||
public float quality = 0.1f;
|
||||
[Min(0)]
|
||||
public float radius = 1000;
|
||||
|
||||
|
||||
private float a, b;
|
||||
private List<Vector2> vert = new List<Vector2>();
|
||||
|
||||
|
||||
float Func(float t, bool xByY)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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)
|
||||
{
|
||||
y = Func(x, true);
|
||||
vert.Add(new Vector2(dx + x, dy + y));
|
||||
x += delta;
|
||||
}
|
||||
|
||||
if (float.IsNaN(vert.Last().y))
|
||||
{
|
||||
vert.RemoveAt(vert.Count - 1);
|
||||
}
|
||||
|
||||
while (y > 0)
|
||||
{
|
||||
x = Func(y, false);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[CustomEditor(typeof(UISquircle))]
|
||||
public class EditorFoo : Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
var script = (UISquircle)target;
|
||||
GUILayout.Label("Vertex count: " + script.vert.Count().ToString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d7948ca2f24604c4aa5340622512a976
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue