private void CreateCurves() { curves.Clear(); for (int i = 0, len = nodes.Count - 1; i < len; i++) { SplineNode n = nodes[i]; SplineNode next = nodes[i + 1]; CubicBezierCurve curve = new CubicBezierCurve(n, next, m_CurveResolution); curves.Add(curve); } m_NeedsUpdate = true; }
/// <summary> /// Adds a node at the end of the spline. /// </summary> /// <param name="node"></param> public virtual int AddNode(SplineNode node) { nodes.Add(node); int numNodes = nodes.Count; if (numNodes != 1) { SplineNode previousNode = nodes[numNodes - 2]; CubicBezierCurve curve = new CubicBezierCurve(previousNode, node, m_CurveResolution); curves.Add(curve); } m_NeedsUpdate = true; return(numNodes); }
/// <summary> /// Insert the given node in the spline at index. Index must be greater than 0 and less than node count. /// </summary> /// <param name="index"></param> /// <param name="node"></param> public void InsertNode(int index, SplineNode node) { if (index == 0) { throw new Exception("Can't insert a node at index 0"); } SplineNode previousNode = nodes[index - 1]; SplineNode nextNode = nodes[index]; nodes.Insert(index, node); curves[index - 1].ConnectEnd(node); CubicBezierCurve curve = new CubicBezierCurve(node, nextNode, m_CurveResolution); curves.Insert(index, curve); m_NeedsUpdate = true; }
/// <summary> /// Returns the tangent of spline at distance. Distance must be between 0 and spline length. /// </summary> /// <param name="d"></param> /// <returns></returns> public Vector3 GetTangentAlongSplineAtDistance(float d) { d = Mathf.Clamp(d, 0, length); for (int i = 0, numCurves = curves.Count; i < numCurves; i++) { CubicBezierCurve curve = curves[i]; if (d > curve.Length) { d -= curve.Length; } else { return(curve.GetTangentAtDistance(d)); } } throw new Exception("Something went wrong with GetTangentAlongSplineAtDistance"); }
public void Update() { if (NeedsUpdate) { m_NeedsUpdate = false; length = 0; for (int i = 0, numCurves = curves.Count; i < numCurves; i++) { CubicBezierCurve curve = curves[i]; curve.Update(); length += curve.Length; } if (CurveChanged != null) { CurveChanged.Invoke(); } } }
/// <summary> /// Remove the given node from the spline. The given node must exist and the spline must have more than 2 nodes. /// </summary> /// <param name="node"></param> public void RemoveNode(SplineNode node) { int index = nodes.IndexOf(node); if (nodes.Count <= 2) { throw new Exception("Can't remove the node because a spline needs at least 2 nodes."); } CubicBezierCurve toRemove = index == nodes.Count - 1 ? curves[index - 1] : curves[index]; if (index != 0 && index != nodes.Count - 1) { SplineNode nextNode = nodes[index + 1]; curves[index - 1].ConnectEnd(nextNode); } nodes.RemoveAt(index); curves.Remove(toRemove); m_NeedsUpdate = true; }
public CubicBezierCurve.CurveSample Iterate() { if (m_IterCurveIndex >= curves.Count) { return(null); } CubicBezierCurve curve = curves[m_IterCurveIndex]; CubicBezierCurve.CurveSample sample = curve.Iterate(); if (sample == null) { m_IterCurveIndex++; if (m_IterCurveIndex < curves.Count) { sample = curves[m_IterCurveIndex].NewIteration(); } } return(sample); }