PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/SplineController.cs

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