26 public float T0 {
get {
return t0; } }
27 public float T1 {
get {
return t1; } }
28 public float T2 {
get {
return t2; } }
29 public float T3 {
get {
return t3; } }
39 private float[] sampleParameters;
44 private float[] sampleLengths;
49 private bool isSampleDirty;
54 private int lastSampleSize = 100;
101 isSampleDirty =
true;
116 isSampleDirty =
true;
131 isSampleDirty =
true;
153 isSampleDirty =
true;
158 private void CalculateKnots()
160 var x = alpha * 0.5f;
162 t1 = t0 + Mathf.Pow((p1 - p0).sqrMagnitude, x);
163 t2 = t1 + Mathf.Pow((p2 - p1).sqrMagnitude, x);
164 t3 = t2 + Mathf.Pow((p3 - p2).sqrMagnitude, x);
174 u = Mathf.Clamp(u, 0, 1);
175 float t = t1 * (1 - u) + t2 * u;
177 Vector3 a1 = (t1 - t) / (t1 - t0) * p0 + ((t - t0) / (t1 - t0)) * p1;
178 Vector3 a2 = (t2 - t) / (t2 - t1) * p1 + ((t - t1) / (t2 - t1)) * p2;
179 Vector3 a3 = (t3 - t) / (t3 - t2) * p2 + ((t - t2) / (t3 - t2)) * p3;
181 Vector3 b1 = (t2 - t) / (t2 - t0) * a1 + ((t - t0) / (t2 - t0)) * a2;
182 Vector3 b2 = (t3 - t) / (t3 - t1) * a2 + ((t - t1) / (t3 - t1)) * a3;
184 Vector3 c = (t2 - t) / (t2 - t1) * b1 + ((t - t1) / (t2 - t1)) * b2;
196 u = Mathf.Clamp(u, 0, 1);
197 float t = t1 * (1 - u) + t2 * u;
199 Vector3 a1 = (t1 - t) / (t1 - t0) * p0 + ((t - t0) / (t1 - t0)) * p1;
200 Vector3 a2 = (t2 - t) / (t2 - t1) * p1 + ((t - t1) / (t2 - t1)) * p2;
201 Vector3 a3 = (t3 - t) / (t3 - t2) * p2 + ((t - t2) / (t3 - t2)) * p3;
203 Vector3 b1 = (t2 - t) / (t2 - t0) * a1 + ((t - t0) / (t2 - t0)) * a2;
204 Vector3 b2 = (t3 - t) / (t3 - t1) * a2 + ((t - t1) / (t3 - t1)) * a3;
206 Vector3 c = (t2 - t) / (t2 - t1) * b1 + ((t - t1) / (t2 - t1)) * b2;
208 Vector3 da1 = (1.0f / (t1 - t0)) * (p1 - p0);
209 Vector3 da2 = (1.0f / (t2 - t1)) * (p2 - p1);
210 Vector3 da3 = (1.0f / (t3 - t2)) * (p3 - p2);
212 Vector3 db1 = (t2 - t) / (t2 - t0) * da1 + ((t - t0) / (t2 - t0)) * da2 +
213 (1.0f / (t2 - t0)) * (a2 - a1);
214 Vector3 db2 = (t3 - t) / (t3 - t1) * da2 + ((t - t1) / (t3 - t1)) * da3 +
215 (1.0f / (t3 - t1)) * (a3 - a2);
217 Vector3 dc = (t2 - t) / (t2 - t1) * db1 + ((t - t1) / (t2 - t1)) * db2 +
218 (1.0f / (t2 - t1)) * (b2 - b1);
239 var sample =
new Vector3[n + 2];
240 var delta = 1.0f / (n + 1.0f);
243 sampleParameters =
new float[n + 2];
244 sampleLengths =
new float[n + 2];
246 for (var i = 0; i < (n + 2); i++)
249 sampleParameters[i] = i * delta;
253 length += (sample[i] - sample[i - 1]).magnitude;
256 sampleLengths[i] = length;
259 isSampleDirty =
false;
261 return (Vector3[])sample.Clone();
271 if (isSampleDirty || lastSampleSize != n)
291 for (var i = 0; i < (sampleParameters.Length - 1); i++)
293 if (s >= sampleLengths[i] && s <= sampleLengths[i + 1])
295 var a = (s - sampleLengths[i]) / (sampleLengths[i + 1] - sampleLengths[i]);
296 return (1 - a) * sampleParameters[i] + a * sampleParameters[i + 1];