5 using System.Collections.Generic;
65 #region offset and rotation functions 67 public static readonly Vector3 DefaultUpVector = Vector3.up;
78 float value = step * offsetValue;
79 return repeat ? Mathf.Repeat(value, 1f) : Mathf.Clamp01(value);
92 float value = offsetCurve.Evaluate((
float)step / numSteps);
93 return repeat ? Mathf.Repeat(value, 1f) : Mathf.Clamp01(value);
104 if (vectorCollection.Length == 0)
108 else if (vectorCollection.Length == 1)
110 return vectorCollection[0];
113 float arrayValueLength = 1f / vectorCollection.Length;
114 int indexA = Mathf.FloorToInt(normalizedLength * vectorCollection.Length);
115 if (indexA >= vectorCollection.Length)
117 indexA = repeat ? 0 : vectorCollection.Length - 1;
120 int indexB = indexA + 1;
121 if (indexB >= vectorCollection.Length)
123 indexB = repeat ? 0 : vectorCollection.Length - 1;
126 float blendAmount = (normalizedLength - (arrayValueLength * indexA)) / arrayValueLength;
127 Vector3 blendedVector = Vector3.Lerp(vectorCollection[indexA], vectorCollection[indexB], blendAmount);
128 return blendedVector;
131 public static float ClosestTo(
this IEnumerable<float> collection,
float target)
135 var closest =
float.MaxValue;
136 var minDifference =
float.MaxValue;
137 foreach (var element
in collection)
139 var difference = Mathf.Abs(element - target);
140 if (minDifference > difference)
142 minDifference = difference;
152 #region line drawing functions 154 public static Vector3
GetPointAlongParabola(Vector3 start, Vector3 end, Vector3 up,
float height,
float normalizedLength)
156 float parabolaTime = normalizedLength * 2 - 1;
157 Vector3 direction = end - start;
158 Vector3 pos = start + normalizedLength * direction;
159 pos += ((-parabolaTime * parabolaTime + 1) * height) * up.normalized;
165 int pointIndex = (Mathf.RoundToInt(normalizedLength * points.Length));
166 float subnormalizedLength = normalizedLength - ((float)pointIndex / points.Length);
168 if (pointIndex + 3 >= points.Length)
170 return points[points.Length - 1].
Point;
174 return points[0].
Point;
177 Vector3 point1 = points[pointIndex].
Point;
178 Vector3 point2 = points[pointIndex + 1].
Point;
179 Vector3 point3 = points[pointIndex + 2].
Point;
180 Vector3 point4 = points[pointIndex + 3].
Point;
182 switch (interpolation)
186 return InterpolateBezeirPoints(point1, point2, point3, point4, subnormalizedLength);
189 return InterpolateCatmullRomPoints(point1, point2, point3, point4, subnormalizedLength);
195 float arrayValueLength = 1f / points.Length;
196 int indexA = Mathf.FloorToInt(normalizedLength * points.Length);
197 if (indexA >= points.Length)
202 int indexB = indexA + 1;
203 if (indexB >= points.Length)
208 float blendAmount = (normalizedLength - (arrayValueLength * indexA)) / arrayValueLength;
210 return Vector3.Lerp(points[indexA], points[indexB], blendAmount);
215 Vector3 p1 = 2f * point2;
216 Vector3 p2 = point3 - point1;
217 Vector3 p3 = 2f * point1 - 5f * point2 + 4f * point3 - point4;
218 Vector3 p4 = -point1 + 3f * point2 - 3f * point3 + point4;
220 Vector3 point = 0.5f * (p1 + (p2 * normalizedLength) + (p3 * normalizedLength * normalizedLength) + (p4 * normalizedLength * normalizedLength * normalizedLength));
225 public static Vector3
InterpolateBezeirPoints(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4,
float normalizedLength)
227 float invertedDistance = 1f - normalizedLength;
229 invertedDistance * invertedDistance * invertedDistance * point1 +
230 3f * invertedDistance * invertedDistance * normalizedLength * point2 +
231 3f * invertedDistance * normalizedLength * normalizedLength * point3 +
232 normalizedLength * normalizedLength * normalizedLength * point4;
235 public static Vector3
InterpolateHermitePoints(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4,
float normalizedLength)
237 float invertedDistance = 1f - normalizedLength;
239 invertedDistance * invertedDistance * invertedDistance * point1 +
240 3f * invertedDistance * invertedDistance * normalizedLength * point2 +
241 3f * invertedDistance * normalizedLength * normalizedLength * point3 +
242 normalizedLength * normalizedLength * normalizedLength * point4;
247 return new Vector3(radiusX * Mathf.Cos(angle), radiusY * Mathf.Sin(angle), 0.0f);