void SplitAtPoint(int index) { RecordUndo("Split Spline"); SplinePoint[] splitPoints = new SplinePoint[spline.pointCount - index]; for (int i = 0; i < splitPoints.Length; i++) { splitPoints[i] = spline.GetPoint(index + i); } SplineComputer newSpline = CreateNewSpline(); newSpline.SetPoints(splitPoints); HandleNodes(newSpline, index); SplineUser[] users = newSpline.GetSubscribers(); for (int i = 0; i < users.Length; i++) { users[i].clipFrom = DMath.InverseLerp((double)index / (spline.pointCount - 1), 1.0, users[i].clipFrom); users[i].clipTo = DMath.InverseLerp((double)index / (spline.pointCount - 1), 1.0, users[i].clipTo); } splitPoints = new SplinePoint[index + 1]; for (int i = 0; i <= index; i++) { splitPoints[i] = spline.GetPoint(i); } spline.SetPoints(splitPoints); users = spline.GetSubscribers(); for (int i = 0; i < users.Length; i++) { users[i].clipFrom = DMath.InverseLerp(0.0, ((double)index) / (spline.pointCount - 1), users[i].clipFrom); users[i].clipTo = DMath.InverseLerp(0.0, ((double)index) / (spline.pointCount - 1), users[i].clipTo); } }
void SplitAtPercent(double percent) { RecordUndo("Split Spline"); float pointValue = (spline.pointCount - 1) * (float)percent; int lastPointIndex = Mathf.FloorToInt(pointValue); int nextPointIndex = Mathf.CeilToInt(pointValue); SplinePoint[] splitPoints = new SplinePoint[spline.pointCount - lastPointIndex]; float lerpPercent = Mathf.InverseLerp(lastPointIndex, nextPointIndex, pointValue); SplinePoint splitPoint = SplinePoint.Lerp(spline.GetPoint(lastPointIndex), spline.GetPoint(nextPointIndex), lerpPercent); splitPoint.SetPosition(spline.EvaluatePosition(percent)); splitPoints[0] = splitPoint; for (int i = 1; i < splitPoints.Length; i++) { splitPoints[i] = spline.GetPoint(lastPointIndex + i); } SplineComputer newSpline = CreateNewSpline(); newSpline.SetPoints(splitPoints); HandleNodes(newSpline, lastPointIndex); SplineUser[] users = newSpline.GetSubscribers(); for (int i = 0; i < users.Length; i++) { users[i].clipFrom = DMath.InverseLerp(percent, 1.0, users[i].clipFrom); users[i].clipTo = DMath.InverseLerp(percent, 1.0, users[i].clipTo); } splitPoints = new SplinePoint[lastPointIndex + 2]; for (int i = 0; i <= lastPointIndex; i++) { splitPoints[i] = spline.GetPoint(i); } splitPoints[splitPoints.Length - 1] = splitPoint; spline.SetPoints(splitPoints); users = spline.GetSubscribers(); for (int i = 0; i < users.Length; i++) { users[i].clipFrom = DMath.InverseLerp(0.0, percent, users[i].clipFrom); users[i].clipTo = DMath.InverseLerp(0.0, percent, users[i].clipTo); } }
void Merge(int index, MergeSide mergingSide) { RecordUndo("Merge Splines"); SplineComputer mergedSpline = availableMergeComputers[index]; SplinePoint[] mergedPoints = mergedSpline.GetPoints(); SplinePoint[] original = spline.GetPoints(); List <SplinePoint> pointsList = new List <SplinePoint>(); SplinePoint[] points; if (!mergeEndpoints) { points = new SplinePoint[mergedPoints.Length + original.Length]; } else { points = new SplinePoint[mergedPoints.Length + original.Length - 1]; } if (mergeSide == MergeSide.End) { if (mergingSide == MergeSide.Start) { for (int i = 0; i < original.Length; i++) { pointsList.Add(original[i]); } for (int i = mergeEndpoints ? 1 : 0; i < mergedPoints.Length; i++) { pointsList.Add(mergedPoints[i]); } } else { for (int i = 0; i < original.Length; i++) { pointsList.Add(original[i]); } for (int i = 0; i < mergedPoints.Length - (mergeEndpoints ? 1 : 0); i++) { pointsList.Add(mergedPoints[(mergedPoints.Length - 1) - i]); } } } else { if (mergingSide == MergeSide.Start) { for (int i = 0; i < mergedPoints.Length - (mergeEndpoints ? 1 : 0); i++) { pointsList.Add(mergedPoints[(mergedPoints.Length - 1) - i]); } for (int i = 0; i < original.Length; i++) { pointsList.Add(original[i]); } } else { for (int i = mergeEndpoints ? 1 : 0; i < mergedPoints.Length; i++) { pointsList.Add(mergedPoints[i]); } for (int i = 0; i < original.Length; i++) { pointsList.Add(original[i]); } } } points = pointsList.ToArray(); double mergedPercent = (double)(mergedPoints.Length - 1) / (points.Length - 1); double from = 0.0; double to = 1.0; if (mergeSide == MergeSide.End) { from = 1.0 - mergedPercent; to = 1.0; } else { from = 0.0; to = mergedPercent; } List <Node> mergedNodes = new List <Node>(); List <int> mergedIndices = new List <int>(); for (int i = 0; i < mergedSpline.pointCount; i++) { Node node = mergedSpline.GetNode(i); if (node != null) { mergedNodes.Add(node); mergedIndices.Add(i); Undo.RecordObject(node, "Disconnect Node"); mergedSpline.DisconnectNode(i); i--; } } SplineUser[] subs = mergedSpline.GetSubscribers(); for (int i = 0; i < subs.Length; i++) { mergedSpline.Unsubscribe(subs[i]); subs[i].spline = spline; subs[i].clipFrom = DMath.Lerp(from, to, subs[i].clipFrom); subs[i].clipTo = DMath.Lerp(from, to, subs[i].clipTo); } spline.SetPoints(points); if (mergeSide == MergeSide.Start) { spline.ShiftNodes(0, spline.pointCount - 1, mergedSpline.pointCount); for (int i = 0; i < mergedNodes.Count; i++) { spline.ConnectNode(mergedNodes[i], mergedIndices[i]); } } else { for (int i = 0; i < mergedNodes.Count; i++) { int connectIndex = mergedIndices[i] + original.Length; if (mergeEndpoints) { connectIndex--; } spline.ConnectNode(mergedNodes[i], connectIndex); } } if (EditorUtility.DisplayDialog("Keep merged computer's GameObject?", "Do you want to keep the merged computer's game object?", "Yes", "No")) { Undo.DestroyObjectImmediate(mergedSpline); } else { for (int i = 0; i < mergedNodes.Count; i++) { if (TransformUtility.IsParent(mergedNodes[i].transform, mergedSpline.transform)) { Undo.SetTransformParent(mergedNodes[i].transform, mergedSpline.transform.parent, "Reparent Node"); } } Undo.DestroyObjectImmediate(mergedSpline.gameObject); } FindAvailableComputers(); }