void DoPointPlacement() { Event evt = Event.current; EventType eventType = evt.type; if (m_PlacingPoint) { Ray ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition); if (eventType == EventType.MouseDrag) { float hitDistance = Mathf.Infinity; if (plane.Raycast(ray, out hitDistance)) { evt.Use(); polygon.m_Points[m_SelectedIndex] = GetPointInLocalSpace(ray.GetPoint(hitDistance)); RebuildPolyShapeMesh(false); SceneView.RepaintAll(); } } if (eventType == EventType.MouseUp || eventType == EventType.Ignore || eventType == EventType.KeyDown || eventType == EventType.KeyUp) { evt.Use(); m_PlacingPoint = false; m_SelectedIndex = -1; SceneView.RepaintAll(); } } else if (polygon.polyEditMode == PolyShape.PolyEditMode.Path) { if (eventType == EventType.MouseDown && HandleUtility.nearestControl == m_ControlId) { if (polygon.m_Points.Count < 1) { SetupInputPlane(evt.mousePosition); } float hitDistance = Mathf.Infinity; Ray ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition); if (plane.Raycast(ray, out hitDistance)) { UndoUtility.RecordObject(polygon, "Add Polygon Shape Point"); Vector3 hit = ray.GetPoint(hitDistance); if (polygon.m_Points.Count < 1) { // this monstrosity exists so that grid and incremental snap work when possible, and // incremental is enabled when grid is not available. polygon.transform.position = m_Polygon.isOnGrid ? EditorSnapping.MoveSnap(hit) : EditorSnapping.snapMode == SnapMode.Relative ? ProBuilderSnapping.Snap(hit, EditorSnapping.incrementalSnapMoveValue) : hit; Vector3 cameraFacingPlaneNormal = plane.normal; if (Vector3.Dot(cameraFacingPlaneNormal, SceneView.lastActiveSceneView.camera.transform.forward) > 0f) { cameraFacingPlaneNormal *= -1; } polygon.transform.rotation = Quaternion.LookRotation(cameraFacingPlaneNormal) * Quaternion.Euler(new Vector3(90f, 0f, 0f)); } Vector3 point = GetPointInLocalSpace(hit); if (polygon.m_Points.Count > 2 && Math.Approx3(polygon.m_Points[0], point)) { m_NextMouseUpAdvancesMode = true; return; } polygon.m_Points.Add(point); m_PlacingPoint = true; m_SelectedIndex = polygon.m_Points.Count - 1; RebuildPolyShapeMesh(polygon); evt.Use(); } } else { float hitDistance = Mathf.Infinity; Ray ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition); if (plane.Raycast(ray, out hitDistance)) { Vector3 hit = ray.GetPoint(hitDistance); m_CurrentPosition = GetPointInLocalSpace(hit); } } } else if (polygon.polyEditMode == PolyShape.PolyEditMode.Edit) { if (polygon.m_Points.Count < 3) { SetPolyEditMode(PolyShape.PolyEditMode.Path); return; } if (m_DistanceFromHeightHandle > PreferenceKeys.k_MaxPointDistanceFromControl) { // point insertion Vector2 mouse = evt.mousePosition; Ray ray = HandleUtility.GUIPointToWorldRay(mouse); float hitDistance = Mathf.Infinity; if (plane.Raycast(ray, out hitDistance)) { Vector3 hit = ray.GetPoint(hitDistance); Vector3 point = GetPointInLocalSpace(hit); int polyCount = polygon.m_Points.Count; float distToLineInGUI; int index; Vector3 pInGUI = EditorHandleUtility.ClosestPointToPolyLine(polygon.m_Points, out index, out distToLineInGUI, true, polygon.transform); Vector3 aToPoint = point - polygon.m_Points[index - 1]; Vector3 aToB = polygon.m_Points[index % polyCount] - polygon.m_Points[index - 1]; float ratio = Vector3.Dot(aToPoint, aToB.normalized) / aToB.magnitude; Vector3 wp = Vector3.Lerp(polygon.m_Points[index - 1], polygon.m_Points[index % polyCount], ratio); wp = polygon.transform.TransformPoint(wp); Vector2 aInGUI = HandleUtility.WorldToGUIPoint(polygon.transform.TransformPoint(polygon.m_Points[index - 1])); Vector2 bInGUI = HandleUtility.WorldToGUIPoint(polygon.transform.TransformPoint(polygon.m_Points[index % polyCount])); float distanceToVertex = Mathf.Min(Vector2.Distance(mouse, aInGUI), Vector2.Distance(mouse, bInGUI)); if (distanceToVertex > PreferenceKeys.k_MaxPointDistanceFromControl && distToLineInGUI < PreferenceKeys.k_MaxPointDistanceFromControl) { m_MouseCursor = MouseCursor.ArrowPlus; if (evt.type == EventType.Repaint) { Handles.color = Color.green; Handles.DotHandleCap(-1, wp, Quaternion.identity, HandleUtility.GetHandleSize(wp) * k_HandleSize, evt.type); } if (evt.type == EventType.MouseDown && HandleUtility.nearestControl == m_ControlId) { evt.Use(); UndoUtility.RecordObject(polygon, "Insert Point"); polygon.m_Points.Insert(index, point); m_SelectedIndex = index; m_PlacingPoint = true; RebuildPolyShapeMesh(true); OnBeginVertexMovement(); } Handles.color = Color.white; } if (evt.type != EventType.Repaint) { SceneView.RepaintAll(); } } } } }
void DoPointPlacement() { Event evt = Event.current; EventType eventType = evt.type; if (m_PlacingPoint) { Ray ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition); if (eventType == EventType.MouseDrag) { float hitDistance = Mathf.Infinity; if (m_Plane.Raycast(ray, out hitDistance)) { evt.Use(); polygon.m_Points[m_SelectedIndex] = ProGridsInterface.ProGridsSnap(polygon.transform.InverseTransformPoint(ray.GetPoint(hitDistance)), Vector3.one); RebuildPolyShapeMesh(false); SceneView.RepaintAll(); } } if (eventType == EventType.MouseUp || eventType == EventType.Ignore || eventType == EventType.KeyDown || eventType == EventType.KeyUp) { evt.Use(); m_PlacingPoint = false; m_SelectedIndex = -1; SceneView.RepaintAll(); } } else if (polygon.polyEditMode == PolyShape.PolyEditMode.Path) { if (eventType == EventType.MouseDown) { if (polygon.m_Points.Count < 1) { SetupInputPlane(evt.mousePosition); } float hitDistance = Mathf.Infinity; Ray ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition); if (m_Plane.Raycast(ray, out hitDistance)) { UndoUtility.RecordObject(polygon, "Add Polygon Shape Point"); Vector3 hit = ray.GetPoint(hitDistance); if (polygon.m_Points.Count < 1) { polygon.transform.position = polygon.isOnGrid ? ProGridsInterface.ProGridsSnap(hit) : hit; polygon.transform.rotation = Quaternion.LookRotation(m_Plane.normal) * Quaternion.Euler(new Vector3(90f, 0f, 0f)); } Vector3 point = ProGridsInterface.ProGridsSnap(polygon.transform.InverseTransformPoint(hit), Vector3.one); if (polygon.m_Points.Count > 2 && Math.Approx3(polygon.m_Points[0], point)) { m_NextMouseUpAdvancesMode = true; return; } polygon.m_Points.Add(point); m_PlacingPoint = true; m_SelectedIndex = polygon.m_Points.Count - 1; RebuildPolyShapeMesh(polygon); evt.Use(); } } } else if (polygon.polyEditMode == PolyShape.PolyEditMode.Edit) { if (polygon.m_Points.Count < 3) { SetPolyEditMode(PolyShape.PolyEditMode.Path); return; } if (m_DistanceFromHeightHandle > PreferenceKeys.k_MaxPointDistanceFromControl) { // point insertion int index; float distanceToLine; Vector3 p = EditorHandleUtility.ClosestPointToPolyLine(polygon.m_Points, out index, out distanceToLine, true, polygon.transform); Vector3 wp = polygon.transform.TransformPoint(p); Vector2 ga = HandleUtility.WorldToGUIPoint(polygon.transform.TransformPoint(polygon.m_Points[index % polygon.m_Points.Count])); Vector2 gb = HandleUtility.WorldToGUIPoint(polygon.transform.TransformPoint(polygon.m_Points[(index - 1)])); Vector2 mouse = evt.mousePosition; float distanceToVertex = Mathf.Min(Vector2.Distance(mouse, ga), Vector2.Distance(mouse, gb)); if (distanceToVertex > PreferenceKeys.k_MaxPointDistanceFromControl && distanceToLine < PreferenceKeys.k_MaxPointDistanceFromControl) { Handles.color = Color.green; Handles.DotHandleCap(-1, wp, Quaternion.identity, HandleUtility.GetHandleSize(wp) * k_HandleSize, evt.type); if (evt.type == EventType.MouseDown) { evt.Use(); UndoUtility.RecordObject(polygon, "Insert Point"); polygon.m_Points.Insert(index, p); m_SelectedIndex = index; m_PlacingPoint = true; RebuildPolyShapeMesh(true); OnBeginVertexMovement(); } Handles.color = Color.white; } } } }
void OnSceneGUI() { Event e = Event.current; bool eventHasBeenUsed = false; if (m_IsMoving) { if (e.type == EventType.Ignore || e.type == EventType.MouseUp) { eventHasBeenUsed = true; OnFinishVertexModification(); } } bool sceneViewInUse = EditorHandleUtility.SceneViewInUse(e); if (e.type == EventType.KeyDown) { if (e.keyCode == KeyCode.Backspace && m_currentHandle > -1 && m_currentHandle < m_Points.Count) { UndoUtility.RecordObject(m_Target, "Delete Bezier Point"); m_Points.RemoveAt(m_currentHandle); UpdateMesh(true); } else if (e.keyCode == KeyCode.Escape) { SetIsEditing(false); } } int count = m_Points.Count; Matrix4x4 handleMatrix = Handles.matrix; Handles.matrix = m_Target.transform.localToWorldMatrix; EditorGUI.BeginChangeCheck(); for (int index = 0; index < count; index++) { if (index < count - 1 || m_CloseLoop) { Handles.DrawBezier(m_Points[index].position, m_Points[(index + 1) % count].position, m_Points[index].tangentOut, m_Points[(index + 1) % count].tangentIn, Color.green, EditorGUIUtility.whiteTexture, 1f); } if (!m_IsEditing) { continue; } // If the index is selected show the full transform gizmo, otherwise use free move handles if (m_currentHandle == index) { BezierPoint point = m_Points[index]; if (!m_currentHandle.isTangent) { Vector3 prev = point.position; prev = Handles.PositionHandle(prev, Quaternion.identity); if (!Math.Approx3(prev, point.position)) { if (!m_IsMoving) { OnBeginVertexModification(); } prev = EditorSnapping.MoveSnap(prev); Vector3 dir = prev - point.position; point.position = prev; point.tangentIn += dir; point.tangentOut += dir; } // rotation int prev_index = index > 0 ? index - 1 : (m_CloseLoop ? count - 1 : -1); int next_index = index < count - 1 ? index + 1 : (m_CloseLoop ? 0 : -1); Vector3 rd = BezierPoint.GetLookDirection(m_Points, index, prev_index, next_index); Quaternion look = Quaternion.LookRotation(rd); float size = HandleUtility.GetHandleSize(point.position); Matrix4x4 pm = Handles.matrix; Handles.matrix = pm * Matrix4x4.TRS(point.position, look, Vector3.one); point.rotation = Handles.Disc(point.rotation, Vector3.zero, Vector3.forward, size, false, 0f); Handles.matrix = pm; } else { Handles.color = bezierTangentHandleColor; if (m_currentHandle.tangent == BezierTangentDirection.In && (m_CloseLoop || index > 0)) { EditorGUI.BeginChangeCheck(); point.tangentIn = Handles.PositionHandle(point.tangentIn, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { if (!m_IsMoving) { OnBeginVertexModification(); } point.tangentIn = EditorSnapping.MoveSnap(point.tangentIn); point.EnforceTangentMode(BezierTangentDirection.In, m_TangentMode); } Handles.color = Color.blue; Handles.DrawLine(m_Points[index].position, m_Points[index].tangentIn); } if (m_currentHandle.tangent == BezierTangentDirection.Out && (m_CloseLoop || index < count - 1)) { EditorGUI.BeginChangeCheck(); point.tangentOut = Handles.PositionHandle(point.tangentOut, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { if (!m_IsMoving) { OnBeginVertexModification(); } point.tangentOut = EditorSnapping.MoveSnap(point.tangentOut); point.EnforceTangentMode(BezierTangentDirection.Out, m_TangentMode); } Handles.color = Color.red; Handles.DrawLine(m_Points[index].position, m_Points[index].tangentOut); } } m_Points[index] = point; } } if (!m_IsEditing) { return; } EventType eventType = e.type; if (!eventHasBeenUsed) { eventHasBeenUsed = eventType == EventType.Used; } for (int index = 0; index < count; index++) { Vector3 prev; BezierPoint point = m_Points[index]; // Position Handle float size = HandleUtility.GetHandleSize(point.position) * k_HandleSize; Handles.color = bezierPositionHandleColor; if (m_currentHandle == index && !m_currentHandle.isTangent) { Handles.DotHandleCap(0, point.position, Quaternion.identity, size, e.type); } else { prev = point.position; prev = Handles.FreeMoveHandle(prev, Quaternion.identity, size, Vector3.zero, Handles.DotHandleCap); if (!eventHasBeenUsed && eventType == EventType.MouseUp && e.type == EventType.Used) { eventHasBeenUsed = true; m_currentHandle = (BezierHandle)index; Repaint(); SceneView.RepaintAll(); } else if (!Math.Approx3(prev, point.position)) { if (!m_IsMoving) { OnBeginVertexModification(); } point.SetPosition(EditorSnapping.MoveSnap(prev)); } } // Tangent handles Handles.color = bezierTangentHandleColor; // Tangent In Handle if (m_CloseLoop || index > 0) { size = HandleUtility.GetHandleSize(point.tangentIn) * k_HandleSize; Handles.DrawLine(point.position, point.tangentIn); if (index == m_currentHandle && m_currentHandle.isTangent && m_currentHandle.tangent == BezierTangentDirection.In) { Handles.DotHandleCap(0, point.tangentIn, Quaternion.identity, size, e.type); } else { prev = point.tangentIn; prev = Handles.FreeMoveHandle(prev, Quaternion.identity, size, Vector3.zero, Handles.DotHandleCap); if (!eventHasBeenUsed && eventType == EventType.MouseUp && e.type == EventType.Used) { eventHasBeenUsed = true; m_currentHandle.SetIndexAndTangent(index, BezierTangentDirection.In); Repaint(); SceneView.RepaintAll(); } else if (!Math.Approx3(prev, point.tangentIn)) { if (!m_IsMoving) { OnBeginVertexModification(); } point.tangentIn = EditorSnapping.MoveSnap(prev); point.EnforceTangentMode(BezierTangentDirection.In, m_TangentMode); } } } // Tangent Out if (m_CloseLoop || index < count - 1) { size = HandleUtility.GetHandleSize(point.tangentOut) * k_HandleSize; Handles.DrawLine(point.position, point.tangentOut); if (index == m_currentHandle && m_currentHandle.isTangent && m_currentHandle.tangent == BezierTangentDirection.Out) { Handles.DotHandleCap(0, point.tangentOut, Quaternion.identity, size, e.type); } else { prev = point.tangentOut; prev = Handles.FreeMoveHandle(prev, Quaternion.identity, size, Vector3.zero, Handles.DotHandleCap); if (!eventHasBeenUsed && eventType == EventType.MouseUp && e.type == EventType.Used) { eventHasBeenUsed = true; m_currentHandle.SetIndexAndTangent(index, BezierTangentDirection.Out); Repaint(); SceneView.RepaintAll(); } else if (!Math.Approx3(prev, point.tangentOut)) { if (!m_IsMoving) { OnBeginVertexModification(); } point.tangentOut = EditorSnapping.MoveSnap(prev); point.EnforceTangentMode(BezierTangentDirection.Out, m_TangentMode); } } } m_Points[index] = point; } // Do control point insertion if (!eventHasBeenUsed && m_ControlPoints != null && m_ControlPoints.Count > 1) { int index = -1; float distanceToLine; Vector3 p = EditorHandleUtility.ClosestPointToPolyLine(m_ControlPoints, out index, out distanceToLine, false, null); if (!IsHoveringHandlePoint(e.mousePosition) && distanceToLine < PreferenceKeys.k_MaxPointDistanceFromControl) { Handles.color = Color.green; Handles.DotHandleCap(-1, p, Quaternion.identity, HandleUtility.GetHandleSize(p) * .05f, e.type); Handles.color = Color.white; if (!eventHasBeenUsed && eventType == EventType.MouseDown && e.button == 0) { UndoUtility.RecordObject(m_Target, "Add Point"); Vector3 dir = m_ControlPoints[(index + 1) % m_ControlPoints.Count] - m_ControlPoints[index]; m_Points.Insert((index / m_Columns) + 1, new BezierPoint(p, p - dir, p + dir, Quaternion.identity)); UpdateMesh(true); e.Use(); } SceneView.RepaintAll(); } } if (e.type == EventType.MouseUp && !sceneViewInUse) { m_currentHandle.SetIndex(-1); } Handles.matrix = handleMatrix; if (EditorGUI.EndChangeCheck()) { UpdateMesh(false); } }