-
Notifications
You must be signed in to change notification settings - Fork 56
/
PolyMesh.cs
329 lines (291 loc) · 7.89 KB
/
PolyMesh.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter))]
public class PolyMesh : MonoBehaviour
{
public List<Vector3> keyPoints = new List<Vector3>();
public List<Vector3> curvePoints = new List<Vector3>();
public List<bool> isCurve = new List<bool>();
public MeshCollider meshCollider;
[Range(0.01f, 1)] public float curveDetail = 0.1f;
public float colliderDepth = 1;
public bool buildColliderEdges = true;
public bool buildColliderFront;
public Vector2 uvPosition;
public float uvScale = 1;
public float uvRotation;
public List<Vector3> GetEdgePoints()
{
//Build the point list and calculate curves
var points = new List<Vector3>();
for (int i = 0; i < keyPoints.Count; i++)
{
if (isCurve[i])
{
//Get the curve control point
var a = keyPoints[i];
var c = keyPoints[(i + 1) % keyPoints.Count];
var b = Bezier.Control(a, c, curvePoints[i]);
//Build the curve
var count = Mathf.Ceil(1 / curveDetail);
for (int j = 0; j < count; j++)
{
var t = (float)j / count;
points.Add(Bezier.Curve(a, b, c, t));
}
}
else
points.Add(keyPoints[i]);
}
return points;
}
public void BuildMesh()
{
var points = GetEdgePoints();
var vertices = points.ToArray();
//Build the index array
var indices = new List<int>();
while (indices.Count < points.Count)
indices.Add(indices.Count);
//Build the triangle array
var triangles = Triangulate.Points(points);
//Build the uv array
var scale = uvScale != 0 ? (1 / uvScale) : 0;
var matrix = Matrix4x4.TRS(-uvPosition, Quaternion.Euler(0, 0, uvRotation), new Vector3(scale, scale, 1));
var uv = new Vector2[points.Count];
for (int i = 0; i < uv.Length; i++)
{
var p = matrix.MultiplyPoint(points[i]);
uv[i] = new Vector2(p.x, p.y);
}
//Find the mesh (create it if it doesn't exist)
var meshFilter = GetComponent<MeshFilter>();
var mesh = meshFilter.sharedMesh;
if (mesh == null)
{
mesh = new Mesh();
mesh.name = "PolySprite_Mesh";
meshFilter.mesh = mesh;
}
//Update the mesh
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.Optimize();
//Update collider after the mesh is updated
UpdateCollider(points, triangles);
}
void UpdateCollider(List<Vector3> points, int[] tris)
{
//Update the mesh collider if there is one
if (meshCollider != null)
{
var vertices = new List<Vector3>();
var triangles = new List<int>();
if (buildColliderEdges)
{
//Build vertices array
var offset = new Vector3(0, 0, colliderDepth / 2);
for (int i = 0; i < points.Count; i++)
{
vertices.Add(points[i] + offset);
vertices.Add(points[i] - offset);
}
//Build triangles array
for (int a = 0; a < vertices.Count; a += 2)
{
var b = (a + 1) % vertices.Count;
var c = (a + 2) % vertices.Count;
var d = (a + 3) % vertices.Count;
triangles.Add(a);
triangles.Add(c);
triangles.Add(b);
triangles.Add(c);
triangles.Add(d);
triangles.Add(b);
}
}
if (buildColliderFront)
{
for (int i = 0; i < tris.Length; i++)
tris[i] += vertices.Count;
vertices.AddRange(points);
triangles.AddRange(tris);
}
//Find the mesh (create it if it doesn't exist)
var mesh = meshCollider.sharedMesh;
if (mesh == null)
{
mesh = new Mesh();
mesh.name = "PolySprite_Collider";
}
//Update the mesh
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
mesh.Optimize();
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;
}
}
bool IsRightTurn(List<Vector3> points, int a, int b, int c)
{
var ab = points[b] - points[a];
var bc = points[c] - points[b];
return (ab.x * bc.y - ab.y * bc.x) < 0;
}
bool IntersectsExistingLines(List<Vector3> points, Vector3 a, Vector3 b)
{
for (int i = 0; i < points.Count; i++)
if (LinesIntersect(points, a, b, points[i], points[(i + 1) % points.Count]))
return true;
return false;
}
bool LinesIntersect(List<Vector3> points, Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4)
{
if (point1 == point3 || point1 == point4 || point2 == point3 || point2 == point4)
return false;
float ua = (point4.x - point3.x) * (point1.y - point3.y) - (point4.y - point3.y) * (point1.x - point3.x);
float ub = (point2.x - point1.x) * (point1.y - point3.y) - (point2.y - point1.y) * (point1.x - point3.x);
float denominator = (point4.y - point3.y) * (point2.x - point1.x) - (point4.x - point3.x) * (point2.y - point1.y);
if (Mathf.Abs(denominator) <= 0.00001f)
{
if (Mathf.Abs(ua) <= 0.00001f && Mathf.Abs(ub) <= 0.00001f)
return true;
}
else
{
ua /= denominator;
ub /= denominator;
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
return true;
}
return false;
}
}
public static class Bezier
{
public static float Curve(float from, float control, float to, float t)
{
return from * (1 - t) * (1 - t) + control * 2 * (1 - t) * t + to * t * t;
}
public static Vector3 Curve(Vector3 from, Vector3 control, Vector3 to, float t)
{
from.x = Curve(from.x, control.x, to.x, t);
from.y = Curve(from.y, control.y, to.y, t);
from.z = Curve(from.z, control.z, to.z, t);
return from;
}
public static Vector3 Control(Vector3 from, Vector3 to, Vector3 curve)
{
//var center = Vector3.Lerp(from, to, 0.5f);
//return center + (curve - center) * 2;
var axis = Vector3.Normalize(to - from);
var dot = Vector3.Dot(axis, curve - from);
var linePoint = from + axis * dot;
return linePoint + (curve - linePoint) * 2;
}
}
public static class Triangulate
{
public static int[] Points(List<Vector3> points)
{
var indices = new List<int>();
int n = points.Count;
if (n < 3)
return indices.ToArray();
int[] V = new int[n];
if (Area(points) > 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(points, 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();
}
static float Area(List<Vector3> points)
{
int n = points.Count;
float A = 0.0f;
for (int p = n - 1, q = 0; q < n; p = q++)
{
Vector3 pval = points[p];
Vector3 qval = points[q];
A += pval.x * qval.y - qval.x * pval.y;
}
return (A * 0.5f);
}
static bool Snip(List<Vector3> points, int u, int v, int w, int n, int[] V)
{
int p;
Vector3 A = points[V[u]];
Vector3 B = points[V[v]];
Vector3 C = 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;
Vector3 P = points[V[p]];
if (InsideTriangle(A, B, C, P))
return false;
}
return true;
}
static 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));
}
}