private void ShowControlPoints(CableSpline cable) { foreach (var point in cable.ControlPoints) { ShowControlPoint(point, cable); } }
private void AddContolPoint(Event currentEvent, CableSpline cable, InScenePosition inScenePosition) { var lastPoint = cable.ControlPoints.Last(); var lastPointPosition = cable.transform.position + lastPoint.Position; if (inScenePosition.IsInCable) { if (currentEvent.type == EventType.MouseDown) { Undo.RecordObject(cable, "Inserted control point"); cable.InsertControlPoint(inScenePosition.ControlPoints, inScenePosition.Position); currentEvent.Use(); } } else { Handles.DrawLine(lastPointPosition, inScenePosition.Position); if (currentEvent.type == EventType.MouseDown) { Undo.RecordObject(cable, "Added additional control point"); cable.AddControlPoint(inScenePosition.Position); currentEvent.Use(); } } }
private void RemoveControlPoint(Event currentEvent, CableSpline cable, InScenePosition inScenePosition) { if (currentEvent.type == EventType.MouseDown) { Undo.RecordObject(cable, "Removed control point"); cable.RemoveControlPoint(inScenePosition.ControlPoints.First); currentEvent.Use(); } }
private void ShowControlPoint(CableSpline.SplineControlPoint controlPoint, CableSpline cable) { var position = cable.transform.position + controlPoint.Position; float size = HandleUtility.GetHandleSize(position) * 1f; EditorGUI.BeginChangeCheck(); controlPoint.Position = Handles.DoPositionHandle(position, controlPoint.Direction) - cable.transform.position; controlPoint.Direction = Handles.DoRotationHandle(controlPoint.Direction, position); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(cable, "Edited Cable control point"); cable.UpdateMesh(); EditorUtility.SetDirty(cable); } }
private void DrawCurvedLine(CableSpline cable) { var leftRotation = Quaternion.LookRotation(Vector3.left); var rightotation = Quaternion.LookRotation(Vector3.right); foreach (var pair in cable.GetControlPointPairs(cable.ControlPoints)) { var distance = (pair.Second.Position - pair.First.Position).magnitude / 3; var firstPoint = pair.First.Position + cable.transform.position; var lastPoint = pair.Second.Position + cable.transform.position; var extraPosition01 = pair.First.Position + pair.First.Direction * Vector3.forward * distance + cable.transform.position; var extraPosition02 = pair.Second.Position + pair.Second.Direction * Vector3.back * distance + cable.transform.position; Handles.DrawBezier(firstPoint, lastPoint, extraPosition01, extraPosition02, Color.green, null, 2); } }
private InScenePosition GetInScenePoint(Vector2 position, CableSpline cable) { if (Physics.Raycast(HandleUtility.GUIPointToWorldRay(position), out RaycastHit raycastHit)) { if (raycastHit.collider.gameObject == cable.gameObject) { return(new InScenePosition { Position = raycastHit.point, IsInWorld = true, IsInCable = true, ControlPoints = GetSelectedControlPoint(raycastHit, cable) }); } else { return(new InScenePosition { Position = raycastHit.point, IsInWorld = true, IsInCable = false }); } } else { return(new InScenePosition { Position = Vector3.zero, IsInWorld = false }); } }
private void HandleEvent(Event currentEvent, CableSpline cable) { var inScenePosition = GetInScenePoint(Event.current.mousePosition, cable); if (currentEvent.control) { AddContolPoint(currentEvent, cable, inScenePosition); } else if (currentEvent.shift) { RemoveControlPoint(currentEvent, cable, inScenePosition); } else { Handles.Label(inScenePosition.Position + Vector3.down * 2, "Hold control to place point\nHold shift to remove a point\nPress space to release", EditorTextStyle); } // Space releases the editing of this cable if (currentEvent.type == EventType.KeyDown && currentEvent.keyCode == KeyCode.Space) { cable.IsEditable = false; currentEvent.Use(); } }
private CableSpline.ControlPointPair GetSelectedControlPoint(RaycastHit raycastHit, CableSpline connection) { // Each segment consist of a |RoundSegments| quad, for a total of RoundSegments * 2 tris or RoundSegments * 6 vertices. var segmentIndex = raycastHit.triangleIndex / (2 * connection.RoundSegments); var placedSegmentIndex = Mathf.Clamp((segmentIndex + 1) / (connection.SmoothnessLevel + 1), 0, int.MaxValue); var previousSegmentIndex = Mathf.Clamp(segmentIndex / (connection.SmoothnessLevel + 1), 0, int.MaxValue); if (previousSegmentIndex == placedSegmentIndex) { previousSegmentIndex = placedSegmentIndex + 1; } return(new CableSpline.ControlPointPair(connection.ControlPoints[placedSegmentIndex], connection.ControlPoints[previousSegmentIndex])); }