/Scripts/SplineController.cs

http://acid-and-base.googlecode.com/ · C# · 133 lines · 98 code · 26 blank · 9 comment · 20 complexity · d6288f5b75ac018c4ad32832e1099077 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public enum eOrientationMode { NODE = 0, TANGENT }
  5. [AddComponentMenu("Splines/Spline Controller")]
  6. [RequireComponent(typeof(SplineInterpolator))]
  7. public class SplineController : MonoBehaviour
  8. {
  9. public GameObject SplineRoot;
  10. public float Duration = 10;
  11. public eOrientationMode OrientationMode = eOrientationMode.NODE;
  12. public eWrapMode WrapMode = eWrapMode.ONCE;
  13. public bool AutoStart = true;
  14. public bool AutoClose = true;
  15. public bool HideOnExecute = true;
  16. SplineInterpolator mSplineInterp;
  17. Transform[] mTransforms;
  18. void OnDrawGizmos()
  19. {
  20. Transform[] trans = GetTransforms();
  21. if (trans.Length < 2)
  22. return;
  23. SplineInterpolator interp = GetComponent(typeof(SplineInterpolator)) as SplineInterpolator;
  24. SetupSplineInterpolator(interp, trans);
  25. interp.StartInterpolation(null, false, WrapMode);
  26. Vector3 prevPos = trans[0].position;
  27. for (int c = 1; c <= 100; c++)
  28. {
  29. float currTime = c * Duration / 100;
  30. Vector3 currPos = interp.GetHermiteAtTime(currTime);
  31. float mag = (currPos-prevPos).magnitude * 2;
  32. Gizmos.color = new Color(mag, 0, 0, 1);
  33. Gizmos.DrawLine(prevPos, currPos);
  34. prevPos = currPos;
  35. }
  36. }
  37. void Start()
  38. {
  39. mSplineInterp = GetComponent(typeof(SplineInterpolator)) as SplineInterpolator;
  40. mTransforms = GetTransforms();
  41. if (HideOnExecute)
  42. DisableTransforms();
  43. if (AutoStart)
  44. FollowSpline();
  45. }
  46. void SetupSplineInterpolator(SplineInterpolator interp, Transform[] trans)
  47. {
  48. interp.Reset();
  49. float step = (AutoClose) ? Duration / trans.Length :
  50. Duration / (trans.Length - 1);
  51. int c;
  52. for (c = 0; c < trans.Length; c++)
  53. {
  54. if (OrientationMode == eOrientationMode.NODE)
  55. {
  56. interp.AddPoint(trans[c].position, trans[c].rotation, step * c, new Vector2(0, 1));
  57. }
  58. else if (OrientationMode == eOrientationMode.TANGENT)
  59. {
  60. Quaternion rot;
  61. if (c != trans.Length - 1)
  62. rot = Quaternion.LookRotation(trans[c + 1].position - trans[c].position, trans[c].up);
  63. else if (AutoClose)
  64. rot = Quaternion.LookRotation(trans[0].position - trans[c].position, trans[c].up);
  65. else
  66. rot = trans[c].rotation;
  67. interp.AddPoint(trans[c].position, rot, step * c, new Vector2(0, 1));
  68. }
  69. }
  70. if (AutoClose)
  71. interp.SetAutoCloseMode(step * c);
  72. }
  73. /// <summary>
  74. /// Returns children transforms, sorted by name.
  75. /// </summary>
  76. Transform[] GetTransforms()
  77. {
  78. if (SplineRoot == null)
  79. return new Transform[] { };
  80. List<Component> components = new List<Component>(SplineRoot.GetComponentsInChildren(typeof(Transform)));
  81. List<Transform> transforms = components.ConvertAll(c => (Transform)c);
  82. transforms.Remove(SplineRoot.transform);
  83. transforms.Sort(delegate(Transform a, Transform b) { return a.name.CompareTo(b.name); });
  84. return transforms.ToArray();
  85. }
  86. /// <summary>
  87. /// Disables the spline objects, we don't need them outside design-time.
  88. /// </summary>
  89. void DisableTransforms()
  90. {
  91. if (SplineRoot != null)
  92. {
  93. SplineRoot.SetActiveRecursively(false);
  94. }
  95. }
  96. /// <summary>
  97. /// Starts the interpolation
  98. /// </summary>
  99. void FollowSpline()
  100. {
  101. if (mTransforms.Length > 0)
  102. {
  103. SetupSplineInterpolator(mSplineInterp, mTransforms);
  104. mSplineInterp.StartInterpolation(null, true, WrapMode);
  105. }
  106. }
  107. }