5 using System.Collections.Generic;
12 protected const float MinRotationMagnitude = 0.0001f;
14 #region fields & properties 16 public float UnclampedWorldLength
20 return GetUnclampedWorldLengthInternal();
24 [Header(
"Basic Settings")]
25 [Tooltip(
"Clamps the line's normalized start point. This setting will affect line renderers.")]
27 public float LineStartClamp = 0f;
29 [Tooltip(
"Clamps the line's normalized end point. This setting will affect line renderers.")]
30 public float LineEndClamp = 1f;
32 public virtual bool Loops
41 [Tooltip(
"The rotation mode used in the GetRotation function. You can visualize rotations by checking Draw Rotations under Editor Settings.")]
44 [Tooltip(
"Reverses up vector when determining rotation along line")]
45 public bool FlipUpVector =
false;
47 [Tooltip(
"Local space offset to transform position. Used to determine rotation along line in RelativeToOrigin rotation mode")]
48 public Vector3 OriginOffset = Vector3.zero;
50 [Tooltip(
"The weight of manual up vectors in Velocity rotation mode")]
52 public float ManualUpVectorBlend = 0f;
54 [Tooltip(
"These vectors are used with ManualUpVectorBlend to determine rotation along the line in Velocity rotation mode. Vectors are distributed along the normalized length of the line.")]
55 public Vector3[] ManualUpVectors =
new Vector3[] { Vector3.up, Vector3.up, Vector3.up };
57 [Tooltip(
"Used in Velocity rotation mode. Smaller values are more accurate but more expensive")]
58 [Range(0.0001f, 0.1f)]
59 public float VelocitySearchRange = 0.02f;
61 public float VelocityBlend = 0.5f;
63 [Header (
"Distortion")]
64 [Tooltip(
"NormalizedLength mode uses the DistortionStrength curve for distortion strength, Uniform uses UniformDistortionStrength along entire line")]
66 public AnimationCurve DistortionStrength = AnimationCurve.Linear(0f, 1f, 1f, 1f);
68 public float UniformDistortionStrength = 1f;
71 [Tooltip(
"A list of distorters that apply to this line")]
72 protected List<Distorter> distorters =
new List<Distorter>();
74 [Tooltip(
"Controls whether this line loops (Note: some classes override this setting)")]
76 protected bool loops =
false;
82 public abstract int NumPoints {
get; }
84 protected abstract void SetPointInternal(
int pointIndex, Vector3 point);
92 protected abstract Vector3 GetPointInternal(
float normalizedLength);
100 protected abstract Vector3 GetPointInternal(
int pointIndex);
109 return transform.forward;
116 protected abstract float GetUnclampedWorldLengthInternal();
123 public Vector3 FirstPoint
135 public Vector3 LastPoint
139 return GetPoint(NumPoints - 1);
143 SetPoint(NumPoints - 1, value);
149 if (!distorters.Contains(newDistorter))
151 distorters.Add(newDistorter);
162 Vector3 startPosition = GetPoint(0);
163 Vector3 endPosition = GetPoint(NumPoints - 1);
164 for (
int i = 1; i < NumPoints - 2; i++)
166 SetPoint(i, Vector3.Lerp(startPosition, endPosition, (1f / NumPoints * 1)));
180 Vector3 lastPoint = GetUnclampedPoint(0f);
181 Vector3 currentPoint = Vector3.zero;
182 float normalizedLength = 0f;
183 float distanceSoFar = 0f;
185 for (
int i = 1; i < searchResolution; i++)
188 normalizedLength = (1f / searchResolution) * i;
189 currentPoint = GetUnclampedPoint(normalizedLength);
190 distanceSoFar += Vector3.Distance(lastPoint, currentPoint);
191 lastPoint = currentPoint;
193 if (distanceSoFar >= worldLength)
200 return Mathf.Clamp01 (normalizedLength);
210 Vector3 velocity = Vector3.zero;
211 if (normalizedLength < VelocitySearchRange)
213 Vector3 currentPos = GetPoint(normalizedLength);
214 Vector3 nextPos = GetPoint(normalizedLength + VelocitySearchRange);
215 velocity = (nextPos - currentPos).normalized;
219 Vector3 currentPos = GetPoint(normalizedLength);
220 Vector3 prevPos = GetPoint(normalizedLength - VelocitySearchRange);
221 velocity = (currentPos - prevPos).normalized;
234 rotationType = (rotationType !=
RotationTypeEnum.None) ? rotationType : RotationType;
235 Vector3 rotationVector = Vector3.zero;
237 switch (rotationType)
244 rotationVector = GetVelocity(normalizedLength);
248 Vector3 point = GetPoint(normalizedLength);
249 Vector3 origin = transform.TransformPoint(OriginOffset);
250 rotationVector = (point - origin).normalized;
254 if (rotationVector.magnitude < MinRotationMagnitude)
256 return transform.rotation;
259 Vector3 upVector = GetUpVectorInternal(normalizedLength);
261 if (ManualUpVectorBlend > 0f)
264 upVector = Vector3.Lerp(upVector, manualUpVector, manualUpVector.magnitude);
269 upVector = -upVector;
272 return Quaternion.LookRotation(rotationVector, upVector);
283 return GetRotation((
float)pointIndex / NumPoints, (rotationType !=
RotationTypeEnum.None) ? rotationType : RotationType);
293 normalizedLength = ClampedLength(normalizedLength);
294 return DistortPoint (transform.TransformPoint(GetPointInternal(normalizedLength)), normalizedLength);
304 normalizedLength = Mathf.Clamp01(normalizedLength);
305 return DistortPoint(transform.TransformPoint(GetPointInternal(normalizedLength)), normalizedLength);
315 if (pointIndex < 0 || pointIndex >= NumPoints)
317 throw new System.IndexOutOfRangeException();
320 return transform.TransformPoint(GetPointInternal(pointIndex));
329 public void SetPoint (
int pointIndex, Vector3 point)
331 if (pointIndex < 0 || pointIndex >= NumPoints)
333 throw new System.IndexOutOfRangeException();
336 SetPointInternal(pointIndex, transform.InverseTransformPoint(point));
346 #region private & protected 354 private Vector3 DistortPoint (Vector3 point,
float normalizedLength)
356 float strength = UniformDistortionStrength;
357 switch (DistortionType)
364 strength = DistortionStrength.Evaluate(normalizedLength);
368 for (
int i = 0; i < distorters.Count; i++)
371 if (distorters[i] != null)
373 point = distorters[i].DistortPoint(point, strength);
379 private float ClampedLength(
float normalizedLength)
381 return Mathf.Lerp(Mathf.Max (LineStartClamp, 0.0001f), Mathf.Min (LineEndClamp, 0.9999f), Mathf.Clamp01(normalizedLength));
387 protected virtual void OnDrawGizmos()
392 if (Application.isPlaying)
397 if (
UnityEditor.Selection.activeGameObject ==
this.gameObject)
404 if (lineRenderer != null)
409 Vector3 firstPos = GetPoint(0f);
410 Vector3 lastPos = firstPos;
411 Gizmos.color = Color.Lerp (LineBaseEditor.DefaultDisplayLineColor, Color.clear, 0.25f);
414 for (
int i = 1; i < numSteps; i++)
416 float normalizedLength = (1f / (numSteps - 1)) * i;
417 Vector3 currentPos = GetPoint(normalizedLength);
418 Gizmos.DrawLine(lastPos, currentPos);
419 lastPos = currentPos;
424 Gizmos.DrawLine(lastPos, firstPos);