1
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/// <summary>
|
||||
/// Credit - ryanslikesocool
|
||||
/// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
|
||||
/// </summary>
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
|
||||
///Credit where credit is due
|
||||
///https://wiki.unity3d.com/index.php?title=Triangulator
|
||||
[ExecuteInEditMode]
|
||||
public class MeshCreator : MonoBehaviour
|
||||
{
|
||||
public void CreateMesh(List<Vector2> points)
|
||||
{
|
||||
// Create Vector2 vertices
|
||||
Vector2[] vertices2D = points.ToArray();
|
||||
|
||||
// Use the triangulator to get indices for creating triangles
|
||||
Triangulator tr = new Triangulator(vertices2D);
|
||||
int[] indices = tr.Triangulate();
|
||||
|
||||
// Create the Vector3 vertices
|
||||
Vector3[] vertices = new Vector3[vertices2D.Length];
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
|
||||
}
|
||||
|
||||
// Create the mesh
|
||||
Mesh msh = new Mesh();
|
||||
msh.vertices = vertices;
|
||||
msh.triangles = indices;
|
||||
msh.RecalculateNormals();
|
||||
msh.RecalculateBounds();
|
||||
|
||||
// Set up game object with mesh;
|
||||
GetComponent<MeshFilter>().mesh = msh;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c87d7e3a53e4c4bf9ae3657f0dff98b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,91 @@
|
||||
/// <summary>
|
||||
/// Credit - ryanslikesocool
|
||||
/// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
|
||||
/// </summary>
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
///The formula for a basic superellipse is
|
||||
///Mathf.Pow(Mathf.Abs(x / a), n) + Mathf.Pow(Mathf.Abs(y / b), n) = 1
|
||||
[ExecuteInEditMode]
|
||||
public class SuperellipsePoints : MonoBehaviour
|
||||
{
|
||||
public float xLimits = 1f;
|
||||
public float yLimits = 1f;
|
||||
[Range(1f, 96f)]
|
||||
public float superness = 4f;
|
||||
|
||||
private float lastXLim;
|
||||
private float lastYLim;
|
||||
private float lastSuper;
|
||||
|
||||
[Space]
|
||||
[Range(1, 32)]
|
||||
public int levelOfDetail = 4;
|
||||
|
||||
private int lastLoD;
|
||||
|
||||
[Space]
|
||||
public Material material;
|
||||
|
||||
private List<Vector2> pointList = new List<Vector2>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
RecalculateSuperellipse();
|
||||
|
||||
GetComponent<MeshRenderer>().material = material;
|
||||
|
||||
lastXLim = xLimits;
|
||||
lastYLim = yLimits;
|
||||
lastSuper = superness;
|
||||
|
||||
lastLoD = levelOfDetail;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (lastXLim != xLimits || lastYLim != yLimits || lastSuper != superness || lastLoD != levelOfDetail)
|
||||
{
|
||||
RecalculateSuperellipse();
|
||||
}
|
||||
|
||||
lastXLim = xLimits;
|
||||
lastYLim = yLimits;
|
||||
lastSuper = superness;
|
||||
|
||||
lastLoD = levelOfDetail;
|
||||
}
|
||||
|
||||
void RecalculateSuperellipse()
|
||||
{
|
||||
pointList.Clear();
|
||||
|
||||
float realLoD = levelOfDetail * 4;
|
||||
|
||||
for (float i = 0; i < xLimits; i += 1 / realLoD)
|
||||
{
|
||||
float y = Superellipse(xLimits, yLimits, i, superness);
|
||||
Vector2 tempVecTwo = new Vector2(i, y);
|
||||
pointList.Add(tempVecTwo);
|
||||
}
|
||||
pointList.Add(new Vector2(xLimits, 0));
|
||||
pointList.Add(Vector2.zero);
|
||||
|
||||
GetComponent<MeshCreator>().CreateMesh(pointList);
|
||||
}
|
||||
|
||||
float Superellipse(float a, float b, float x, float n)
|
||||
{
|
||||
float alpha = Mathf.Pow((x / a), n);
|
||||
float beta = 1 - alpha;
|
||||
float y = Mathf.Pow(beta, 1 / n) * b;
|
||||
|
||||
return y;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df6beaa1c87204b919c209b770d44bc9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,132 @@
|
||||
/// <summary>
|
||||
/// Credit - ryanslikesocool
|
||||
/// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
|
||||
/// </summary>
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
///Credit where credit is due
|
||||
///https://wiki.unity3d.com/index.php?title=Triangulator
|
||||
[ExecuteInEditMode]
|
||||
public class Triangulator
|
||||
{
|
||||
private List<Vector2> m_points = new List<Vector2>();
|
||||
|
||||
public Triangulator(Vector2[] points)
|
||||
{
|
||||
m_points = new List<Vector2>(points);
|
||||
}
|
||||
|
||||
public int[] Triangulate()
|
||||
{
|
||||
List<int> indices = new List<int>();
|
||||
|
||||
int n = m_points.Count;
|
||||
if (n < 3)
|
||||
return indices.ToArray();
|
||||
|
||||
int[] V = new int[n];
|
||||
if (Area() > 0)
|
||||
{
|
||||
for (int v = 0; v < n; v++)
|
||||
V[v] = v;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int v = 0; v < n; v++)
|
||||
V[v] = (n - 1) - v;
|
||||
}
|
||||
|
||||
int nv = n;
|
||||
int count = 2 * nv;
|
||||
for (int m = 0, v = nv - 1; nv > 2;)
|
||||
{
|
||||
if ((count--) <= 0)
|
||||
return indices.ToArray();
|
||||
|
||||
int u = v;
|
||||
if (nv <= u)
|
||||
u = 0;
|
||||
v = u + 1;
|
||||
if (nv <= v)
|
||||
v = 0;
|
||||
int w = v + 1;
|
||||
if (nv <= w)
|
||||
w = 0;
|
||||
|
||||
if (Snip(u, v, w, nv, V))
|
||||
{
|
||||
int a, b, c, s, t;
|
||||
a = V[u];
|
||||
b = V[v];
|
||||
c = V[w];
|
||||
indices.Add(a);
|
||||
indices.Add(b);
|
||||
indices.Add(c);
|
||||
m++;
|
||||
for (s = v, t = v + 1; t < nv; s++, t++)
|
||||
V[s] = V[t];
|
||||
nv--;
|
||||
count = 2 * nv;
|
||||
}
|
||||
}
|
||||
|
||||
indices.Reverse();
|
||||
return indices.ToArray();
|
||||
}
|
||||
|
||||
private float Area()
|
||||
{
|
||||
int n = m_points.Count;
|
||||
float A = 0.0f;
|
||||
for (int p = n - 1, q = 0; q < n; p = q++)
|
||||
{
|
||||
Vector2 pval = m_points[p];
|
||||
Vector2 qval = m_points[q];
|
||||
A += pval.x * qval.y - qval.x * pval.y;
|
||||
}
|
||||
return (A * 0.5f);
|
||||
}
|
||||
|
||||
private bool Snip(int u, int v, int w, int n, int[] V)
|
||||
{
|
||||
int p;
|
||||
Vector2 A = m_points[V[u]];
|
||||
Vector2 B = m_points[V[v]];
|
||||
Vector2 C = m_points[V[w]];
|
||||
if (Mathf.Epsilon > (((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x))))
|
||||
return false;
|
||||
for (p = 0; p < n; p++)
|
||||
{
|
||||
if ((p == u) || (p == v) || (p == w))
|
||||
continue;
|
||||
Vector2 P = m_points[V[p]];
|
||||
if (InsideTriangle(A, B, C, P))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool InsideTriangle(Vector2 A, Vector2 B, Vector2 C, Vector2 P)
|
||||
{
|
||||
float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
|
||||
float cCROSSap, bCROSScp, aCROSSbp;
|
||||
|
||||
ax = C.x - B.x; ay = C.y - B.y;
|
||||
bx = A.x - C.x; by = A.y - C.y;
|
||||
cx = B.x - A.x; cy = B.y - A.y;
|
||||
apx = P.x - A.x; apy = P.y - A.y;
|
||||
bpx = P.x - B.x; bpy = P.y - B.y;
|
||||
cpx = P.x - C.x; cpy = P.y - C.y;
|
||||
|
||||
aCROSSbp = ax * bpy - ay * bpx;
|
||||
cCROSSap = cx * apy - cy * apx;
|
||||
bCROSScp = bx * cpy - by * cpx;
|
||||
|
||||
return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac33bc8ebf2744ad6ae91f80f8542546
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user