예제 #1
0
        private void UpdateVertexEditing()
        {
            var guiEvent = Event.current.type;

            var mouseRay = Utils.GetSceneViewMouseRay();
            var vertex   = -1;

            editObject.ppMesh.GetNearestPoint(mouseRay, out vertex, true);

            if (vertex != -1)
            {
                var p0       = HandleUtility.WorldToGUIPoint(editObject.ppMesh.GetVertexByIndexWorld(vertex));
                var p1       = Event.current.mousePosition;
                var distance = (p0 - p1).sqrMagnitude;

                if (distance > 30 * 30)
                {
                    vertex = -1;
                }
            }

            var hoverPoint     = guiEvent != EventType.MouseDrag;
            var handleSelected = GUIUtility.hotControl != 0;

            if (handleSelected)
            {
                editCommand.Reset();
            }

            if (editCommand.IsSet(EditCommand.CommandType.Clear))
            {
                vertexSelection.Clear();
            }
            else if (editCommand.IsSet(EditCommand.CommandType.Add))
            {
                vertexSelection.Add(editCommand.Vertex);

                if (settings.StickOverlappingPoints)
                {
                    editObject.ppMesh.AddOverlappingVertices(vertexSelection, editCommand.Vertex);
                }
            }
            else if (editCommand.IsSet(EditCommand.CommandType.Remove))
            {
                vertexSelection.Remove(editCommand.Vertex);
            }

            editCommand.Reset();

            if (!Event.current.alt)
            {
                if (vertex != -1)
                {
                    if (guiEvent == EventType.MouseDown)
                    {
                        painting = true;
                    }
                    else if (guiEvent == EventType.MouseUp || guiEvent == EventType.MouseMove)
                    {
                        painting = false;
                    }
                    // adding
                    if (Event.current.shift)
                    {
                        if (painting)
                        {
                            editCommand.Command |= EditCommand.CommandType.Add;
                            editCommand.Vertex   = vertex;
                        }
                    }
                    // removing
                    else if (Event.current.control)
                    {
                        if (painting)
                        {
                            editCommand.Command |= EditCommand.CommandType.Remove;
                            editCommand.Vertex   = vertex;
                        }
                    }
                    else
                    {
                        if (guiEvent == EventType.MouseDown)
                        {
                            editCommand.Command |= EditCommand.CommandType.Clear;
                            editCommand.Vertex   = -1;
                        }

                        if (painting)
                        {
                            editCommand.Command |= EditCommand.CommandType.Add;
                            editCommand.Vertex   = vertex;
                        }
                    }
                }
                else
                {
                    if (guiEvent == EventType.MouseDown)
                    {
                        if (!Event.current.shift)
                        {
                            editCommand.Command |= EditCommand.CommandType.Clear;
                        }
                        editCommand.Vertex = -1;
                        multiselectStart   = Event.current.mousePosition;
                        multiselecting     = true;
                    }
                }
            }
            else
            {
                painting       = false;
                multiselecting = false;
            }

            if (guiEvent == EventType.MouseUp)
            {
                OnMultiselectFinished();
            }

            if (handleSelected)
            {
                multiselecting = false;
                painting       = false;
            }

            var handleSize = HandleUtility.GetHandleSize(editObject.transform.position) * 0.1f;

            foreach (var vert in vertexSelection)
            {
                Handles.color = handlesColorSelected;
                Handles.CubeHandleCap(1, editObject.ppMesh.GetVertexByIndexWorld(vert), Quaternion.identity, handleSize, EventType.Repaint);
            }

            if (!handleSelected)
            {
                if (guiEvent == EventType.MouseUp)
                {
                    var array = new int[vertexSelection.Count];
                    vertexSelection.CopyTo(array);
                    oldDragPos = editObject.ppMesh.GetAveragePos(array);
                    editObject.ppMesh.StartUpdateVerticesDelta(vertexSelection, settings.StickOverlappingPoints);

                    if (Tools.pivotRotation == PivotRotation.Local)
                    {
                        if (vertexSelection.Count > 0)
                        {
                            handlesRotation = editObject.ppMesh.GetLocalRotation(vertexSelection);
                        }
                    }
                    else
                    {
                        handlesRotation = Quaternion.identity;
                    }
                }
            }

            if (guiEvent == EventType.MouseUp)
            {
                editObject.ppMesh.SaveUndo();
                RefreshToolbar();
            }

            var newPos = Handles.PositionHandle(oldDragPos, handlesRotation);

            if (guiEvent == EventType.MouseDrag && handleSelected)
            {
                if (settings.GridSnap)
                {
                    newPos = grid.FindClosestGridPointXZ(newPos);
                }
                else if (settings.VertexSnap)
                {
                    var retPos = Vector3.zero;

                    if (editObject.ppMesh.FindClosestPoint(newPos, vertexSelection, vertexSnapTolerance, ref retPos))
                    {
                        newPos = retPos;
                    }
                }

                var diff = editObject.ppMesh.Transform.InverseTransformPoint(newPos) - editObject.ppMesh.Transform.InverseTransformPoint(oldDragPos);

                editObject.ppMesh.UpdateVerticesDelta(diff);

                editObject.UpdateMesh();
            }

            oldDragPos = newPos;

            if (!handleSelected && vertex != -1 && hoverPoint && !vertexSelection.Contains(vertex))
            {
                Handles.color = handlesColorHovered;
                Handles.CubeHandleCap(1, editObject.ppMesh.GetVertexByIndexWorld(vertex), Quaternion.identity, handleSize, EventType.Repaint);
            }
        }