public void AddNewPoint(SPData SPData, Vector3 position, addPathPointPos addPathPointPos, int targetIndex) { Undo.RecordObject(SPData.SplinePlus, "Node added"); var branch = SPData.DictBranches[SPData.Selections._BranchKey]; SPData.EditSpline = false; var node = new Node(); node._Type = SPData.NodeType; node.Point = CreatePoint(SPData, "p" + SPData.PathPointCount, position, Quaternion.identity, SPData.DataParent.transform); if (addPathPointPos == addPathPointPos.Beginning) { branch.Nodes.Insert(0, node); node.Point1 = CreatePoint(SPData, "p1", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); node.Point2 = CreatePoint(SPData, "p2", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); if (branch.Nodes.Count > 1) { node.Point2.position = Vector3.Lerp(branch.Nodes[targetIndex + 1].Point.position, node.Point.position, 0.5f); node.Point1.localPosition = -node.Point2.localPosition; } else { node.Point1.localPosition = new Vector3(5, 0, 0); node.Point2.localPosition = -node.Point1.localPosition; } } else if (addPathPointPos == addPathPointPos.End)//end { branch.Nodes.Add(node); targetIndex = branch.Nodes.Count - 1; node.Point1 = CreatePoint(SPData, "p1", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); node.Point2 = CreatePoint(SPData, "p2", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); node.Point1.position = Vector3.Lerp(branch.Nodes[targetIndex - 1].Point.position, node.Point.position, 0.5f); node.Point2.localPosition = -node.Point1.localPosition; } else if (addPathPointPos == addPathPointPos.Middle)//middle { var a = (targetIndex - 1) < 0 ? branch.Nodes.Count - 1 : targetIndex - 1; var b = (targetIndex + 1) >= branch.Nodes.Count ? 0 : (targetIndex + 1); branch.Nodes.Insert(targetIndex, node); node.Point1 = CreatePoint(SPData, "p1", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); node.Point2 = CreatePoint(SPData, "p2", branch.Nodes[targetIndex].Point.position, Quaternion.identity, node.Point); var dir = (branch.Nodes[a].Point.position - branch.Nodes[b].Point.position).normalized; node.Point1.localPosition = dir * Vector3.Distance(branch.Nodes[a].Point.position, branch.Nodes[b].Point.position) * 0.2f; node.Point2.localPosition = -node.Point1.localPosition; } BranchesClass.BranchWeldSt(SPData); EditorUtility.SetDirty(SPData.SplinePlus); SPData.SplinePlus.SplineCreationClass.UpdateAllBranches(SPData); SPData.PathPointCount++; }
public void WeldBranch(SPData SPData, Node node, int n) { var branch = SPData.DictBranches[SPData.Selections._BranchKey]; Undo.RecordObject(SPData.SplinePlus, "branch welded"); if (n == 0) { branch.Nodes.Insert(0, node); } else { branch.Nodes.Add(node); } if (SPData.NodeType == NodeType.Free) { node._Type = NodeType.Free; } else { node._Type = NodeType.Smooth; } BranchesClass.BreakBranch(SPData, node); BranchesClass.AddRefreshSharedNode(SPData, node); BranchesClass.BranchWeldSt(SPData); EditorUtility.SetDirty(SPData.SplinePlus); SPData.SplinePlus.SplineCreationClass.UpdateBranch(SPData, branch); }
void BranchEdit(SPData SPData) { if (!SPData.IsLooped && SPData.ObjectType != SPType.Extrude) { AddBranch(SPData); } if (SPData.DictBranches.Count == 0) { return; } if (GUI.Button(new Rect(272, 30, 30, 30), new GUIContent(SplinePlusEditor.Delete.image, "Delete the selected node "), GUIStyle.none) && EditorUtility.DisplayDialog("Branch deletion?", "Are you sure you want to delete this branch? " , "Yes", "No")) { BranchesClass.DeleteBranch(SPData, SPData.Selections._BranchKey); SceneView.RepaintAll(); } if (GUI.Button(new Rect(304, 30, 30, 30), new GUIContent(SplinePlusEditor.FlipHandles.image, "Flips the handles of the selected node "), GUIStyle.none)) { Undo.RecordObject(SPData.SplinePlus, "handles Fliped"); BranchesClass.FlipHandles(SPData, SPData.Selections._BranchKey, SPData.Selections._LocalNodeIndex); EditorUtility.SetDirty(SPData.SplinePlus); } var rec = new Rect(336, 30, 30, 30); if (SPData.ObjectType != SPType.Extrude && !SPData.IsLooped) { rec = new Rect(368, 30, 30, 30); if (GUI.Button(new Rect(336, 30, 30, 30), new GUIContent(SplinePlusEditor.BreakAt.image, "Break the selected branch at the selected node"), GUIStyle.none)) { if (!SPData.Selections._PathPoint.Equals(null)) { Undo.RecordObject(SPData.SplinePlus, "Break at branch"); var node = SPData.Selections._PathPoint; BranchesClass.BreakBranch(SPData, node); EditorUtility.SetDirty(SPData.SplinePlus); } else { EditorUtility.DisplayDialog("Error", "You need to have a node selected where you want your branch to be broken at " , "Ok"); } } } else { rec = new Rect(336, 30, 30, 30); } if (GUI.Button(rec, new GUIContent(SplinePlusEditor.Reverse.image, "Reverse the direction of the selected branch "), GUIStyle.none)) { Undo.RecordObject(SPData.SplinePlus, "branch Reversed"); BranchesClass.ReverseBranch(SPData, SPData.Selections._BranchKey); EditorUtility.SetDirty(SPData.SplinePlus); } }
void SelectBranch(SPData SPData) { int n = 0; foreach (var branch in SPData.DictBranches) { for (int i = 0; i < branch.Value.Vertices.Count - 1; i++) { var dist = Vector2.Distance(HandleUtility.WorldToGUIPoint(branch.Value.Vertices[i]), Event.current.mousePosition); if (dist < 20) { Undo.RecordObject(SPData.SplinePlus, "Branch selected"); if (SPData.Selections._BranchKey == branch.Key) { return; } SPData.Selections._BranchKey = branch.Key; SPData.Selections._BranchIndex = n; BranchesClass.BranchWeldSt(SPData); //init mesh layer selection when branch selection is changed to avoid index out of range exception if (SPData.ObjectType == SPType.MeshDeformer) { //Change branch selection delegete if (BranchSelectionChangedDel != null) { BranchSelectionChangedDel(); } } //keep path point selection when changing branches selection if (SPData.Selections._SharedPathPointIndex != -1) { if (!SPData.SharedNodes[SPData.Selections._SharedPathPointIndex].ConnectedBranches.Contains(branch.Key)) { SPData.Selections._PathPoint = new Node(); } else { SPData.Selections._LocalNodeIndex = SPData.SharedNodes[SPData.Selections._SharedPathPointIndex].Node.LocalIndex(SPData, SPData.Selections._BranchKey); SPData.Selections._PathPoint = SPData.DictBranches[SPData.Selections._BranchKey].Nodes[SPData.Selections._LocalNodeIndex]; } } else { SPData.Selections._PathPoint = new Node(); } EditorUtility.SetDirty(SPData.SplinePlus); return; } } n++; } }
public static void SmoothSharedPathPoint(SPData SPData, SharedNode sharedNode, float radius) { SPData.SmoothData.InitNodePos = sharedNode.Node.Point.position; SPData.SmoothData.Nodes = new Node[sharedNode.ConnectedBranches.Count]; SPData.SmoothData.BranchesIndices = sharedNode.ConnectedBranches.ToArray(); //shared path point breaking for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++) { var branchKey = sharedNode.ConnectedBranches[i]; var localIndex = sharedNode.Node.LocalIndex(SPData, branchKey); if (localIndex != 0) { BranchesClass.ReverseBranch(SPData, branchKey); } var duplicate = DuplicatePathPoint(SPData, SPData.DictBranches[branchKey].Nodes[0]); SPData.SmoothData.Nodes[i] = duplicate; SPData.DictBranches[branchKey].Nodes[0] = duplicate; } for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++) { var branchKey = SPData.SmoothData.BranchesIndices[i]; BranchesClass.FlipHandles(SPData, branchKey, 0); var mid = Vector3.Lerp(SPData.DictBranches[branchKey].Nodes[0].Point.position, SPData.DictBranches[branchKey].Nodes[1].Point.position, 0.5f); SPData.SmoothData.Nodes[i].Point.position = Vector3.Lerp(SPData.SmoothData.InitNodePos, mid, radius); SPData.SmoothData.Nodes[i].Point2.position = SPData.SmoothData.InitNodePos; SPData.SmoothData.Nodes[i].Point1.localPosition = -SPData.SmoothData.Nodes[i].Point2.localPosition; } // path points welding for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++)// path points created after chamfer, { for (int n = i; n < SPData.SmoothData.Nodes.Length; n++) { if (n == i) { continue; } ConnectTwoNodes(SPData, SPData.SmoothData.Nodes[n], SPData.SmoothData.Nodes[i]); } } // remove initiale shared path point SPData.SharedNodes.Remove(sharedNode); MonoBehaviour.DestroyImmediate(sharedNode.Node.Point.gameObject); }
public static void IsSharedPathPoint(SPData SPData, Node node, int currentBranchIndex) { foreach (var branch in SPData.DictBranches) { if (branch.Key == currentBranchIndex) { continue; } if (branch.Value.Nodes.Exists(x => x.Equals(node))) { BranchesClass.AddRefreshSharedNode(SPData, node); } } }
public void SmoothSharedPathPoint(SPData SPData) { var node = SPData.Selections._PathPoint; //cach data for restore procedure SPData.SmoothData.InitNodePos = node.Point.position; var sharedPathPointIndex = SPData.Selections._SharedPathPointIndex; SPData.SmoothData.Nodes = new Node[SPData.SharedNodes[sharedPathPointIndex].ConnectedBranches.Count]; SPData.SmoothData.BranchesIndices = SPData.SharedNodes[sharedPathPointIndex].ConnectedBranches.ToArray(); //shared path point breaking for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++) { var branchKey = SPData.SharedNodes[sharedPathPointIndex].ConnectedBranches[i]; var localIndex = node.LocalIndex(SPData, branchKey); var duplicate = SplinePlusAPI.DuplicatePathPoint(SPData, node); SPData.SmoothData.Nodes[i] = duplicate; SPData.DictBranches[branchKey].Nodes[localIndex] = duplicate; if (localIndex != 0) { BranchesClass.FlipHandles(SPData, branchKey, localIndex); } } SPData.SmoothData.newBranchesIndices = new List <int>(); //path points welding for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++)// path points created after chamfer, { for (int n = i; n < SPData.SmoothData.Nodes.Length; n++) { if (n == i) { continue; } //UpdateAllLinks var t = SplinePlusAPI.ConnectTwoNodes(SPData, SPData.SmoothData.Nodes[n], SPData.SmoothData.Nodes[i]); SPData.SmoothData.newBranchesIndices.Add(t); BranchesClass.FlipHandles(SPData, t, 0); BranchesClass.FlipHandles(SPData, t, 1); } } EditSmoothNodePoint(SPData, SPData.SmoothRadius); }
public void ResetSmoothSharedPathPoint(SPData SPData) { SPData.SmoothData.SmoothNode = false; for (int i = 0; i < SPData.SmoothData.Nodes.Length; i++) { var branchKey = SPData.SmoothData.BranchesIndices[i]; var localIndex = SPData.SmoothData.Nodes[i].LocalIndex(SPData, branchKey); SPData.DictBranches[branchKey].Nodes[localIndex] = SPData.SharedNodes[SPData.Selections._SharedPathPointIndex].Node;//restore old center path point } BranchesClass.AddRefreshSharedNode(SPData, SPData.SharedNodes[SPData.Selections._SharedPathPointIndex].Node); //delete created branches for (int i = 0; i < SPData.SmoothData.newBranchesIndices.Count; i++) { BranchesClass.DeleteBranch(SPData, SPData.SmoothData.newBranchesIndices[i]); } SPData.SplinePlus.SplineCreationClass.UpdateAllBranches(SPData); }
public static int ConnectTwoNodes(SPData SPData, Node pathPoint1, Node pathPoint2) { foreach (var branch in SPData.DictBranches) {// avoid creating duplicate branches if (branch.Value.Nodes[0].Equals(pathPoint1) && branch.Value.Nodes[branch.Value.Nodes.Count - 1].Equals(pathPoint2)) { return(branch.Key); } else if (branch.Value.Nodes[0].Equals(pathPoint2) && branch.Value.Nodes[branch.Value.Nodes.Count - 1].Equals(pathPoint1)) { return(branch.Key); } } var newBranch = new Branch(); newBranch.Nodes.Add(pathPoint1); newBranch.Nodes.Add(pathPoint2); SPData.DictBranches.Add(SPData.BranchesCount, newBranch); SPData.Selections._BranchKey = SPData.BranchesCount; SPData.BranchesCount++; BranchesClass.FlipHandles(SPData, SPData.Selections._BranchKey, 1); // detect if newly created branch has shared path points IsSharedPathPoint(SPData, pathPoint1, SPData.Selections._BranchKey); IsSharedPathPoint(SPData, pathPoint2, SPData.Selections._BranchKey); SPData.SplinePlus.SplineCreationClass.UpdateBranch(SPData, newBranch); SPData.Selections._PathPoint = SPData.DictBranches[SPData.Selections._BranchKey].Nodes[0]; return(SPData.Selections._BranchKey); }
void AddBranch(SPData SPData) { if (GUI.Button(new Rect(240, 30, 30, 30), new GUIContent(SplinePlusEditor.Add.image, "Add a new branch at the selected node"), GUIStyle.none)) { if (!SPData.Selections._PathPoint.Equals(null)) { Undo.RecordObject(SPData.SplinePlus, "new branch added"); BranchesClass.AddBranch(SPData); if (SceneView.sceneViews.Count > 0) { SceneView sceneView = (SceneView)SceneView.sceneViews[0]; sceneView.Focus(); } EditorUtility.SetDirty(SPData.SplinePlus); } else { EditorUtility.DisplayDialog("Error", "You need to have a node selected before adding a branch" , "Ok"); } } }