-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
LTBezier.cs
78 lines (67 loc) · 1.34 KB
/
LTBezier.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
using UnityEngine;
public class LTBezier
{
public float length;
private Vector3 a;
private Vector3 aa;
private Vector3 bb;
private Vector3 cc;
private float len;
private float[] arcLengths;
public LTBezier(Vector3 a, Vector3 b, Vector3 c, Vector3 d, float precision)
{
this.a = a;
aa = -a + 3f * (b - c) + d;
bb = 3f * (a + c) - 6f * b;
cc = 3f * (b - a);
len = 1f / precision;
arcLengths = new float[(int)len + 1];
arcLengths[0] = 0f;
Vector3 vector = a;
float num = 0f;
for (int i = 1; (float)i <= len; i++)
{
Vector3 vector2 = bezierPoint((float)i * precision);
num += (vector - vector2).magnitude;
arcLengths[i] = num;
vector = vector2;
}
length = num;
}
private float map(float u)
{
float num = u * arcLengths[(int)len];
int num2 = 0;
int num3 = (int)len;
int num4 = 0;
while (num2 < num3)
{
num4 = num2 + ((int)((float)(num3 - num2) / 2f) | 0);
if (arcLengths[num4] < num)
{
num2 = num4 + 1;
}
else
{
num3 = num4;
}
}
if (arcLengths[num4] > num)
{
num4--;
}
if (num4 < 0)
{
num4 = 0;
}
return ((float)num4 + (num - arcLengths[num4]) / (arcLengths[num4 + 1] - arcLengths[num4])) / len;
}
private Vector3 bezierPoint(float t)
{
return ((aa * t + bb) * t + cc) * t + a;
}
public Vector3 point(float t)
{
return bezierPoint(map(t));
}
}