static void GetHit(SceneView sceneView, out GameObject hitObject, out Vector3 hitPoint, out Vector3 hitNormal)
    {
        if (SceneQueryUtility.FindClickWorldIntersection(sceneView.camera, Event.current.mousePosition, out hitObject))
        {
            var intersection = SceneQueryUtility.FindMeshIntersection(sceneView.camera, Event.current.mousePosition);
            hitPoint  = intersection.worldIntersection;
            hitNormal = intersection.worldPlane.normal;
        }
        else
        {
            hitObject = HandleUtility.PickGameObject(Event.current.mousePosition, out int materialIndex);
            hitPoint  = Vector3.zero;
            hitNormal = Vector3.zero;

            if (hitObject)
            {
                var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                var mf  = hitObject.GetComponentInChildren <MeshFilter>();
                if (mf)
                {
                    EditorHandles_UnityInternal.IntersectRayMesh(ray, mf, out RaycastHit hit);
                    hitPoint  = hit.point;
                    hitNormal = hit.normal;
                }
            }
        }
    }
Example #2
0
        public override bool DragUpdated()
        {
            //Debug.Log("DragUpdated");
            //InternalCSGModelManager.skipRefresh = true;
            try
            {
                DisableVisualObjects();
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                var intersection = SceneQueryUtility.FindMeshIntersection(Event.current.mousePosition);
                var normal       = intersection.plane.normal;

                hoverPosition     = intersection.worldIntersection;
                hoverParent       = SelectionUtility.FindParentToAssignTo(intersection);
                hoverBrushSurface = intersection.brush != null ? new SelectedBrushSurface(intersection.brush, intersection.surfaceIndex) : null;
                hoverRotation     = SelectionUtility.FindDragOrientation(normal, sourceSurfaceAlignment, destinationSurfaceAlignment);
                haveNoParent      = (hoverParent == null);
                hoverSiblingIndex = int.MaxValue;

                RealtimeCSG.Grid.SetForcedGrid(intersection.plane);

                var toggleSnapping = (Event.current.modifiers & EventModifiers.Control) == EventModifiers.Control;
                var doSnapping     = RealtimeCSG.CSGSettings.SnapToGrid ^ toggleSnapping;
                if (doSnapping)
                {
                    var localPoints = new Vector3[8];
                    var localPlane  = intersection.plane;
                    for (var i = 0; i < localPoints.Length; i++)
                    {
                        localPoints[i] = GeometryUtility.ProjectPointOnPlane(localPlane, (hoverRotation * projectedBounds[i]) + hoverPosition);
                    }

                    hoverPosition += RealtimeCSG.Grid.SnapDeltaToGrid(MathConstants.zeroVector3, localPoints);
                }
                hoverPosition = GeometryUtility.ProjectPointOnPlane(intersection.plane, hoverPosition) + (normal * 0.01f);

                EnableVisualObjects();
                return(true);
            }
            finally
            {
                if (!SceneViewEventHandler.IsActive())
                {
                    SceneViewEventHandler.ResetUpdateRoutine();
                }
            }
        }
        public bool DragUpdated(SceneView sceneView)
        {
            try
            {
                DisableVisualObjects();
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                var camera       = sceneView.camera;
                var intersection = SceneQueryUtility.FindMeshIntersection(camera, Event.current.mousePosition);
                var normal       = intersection.worldPlane.normal;

                hoverPosition     = intersection.worldIntersection;
                hoverParent       = SelectionUtility.FindParentToAssignTo(intersection);
                hoverBrushSurface = intersection.brush != null ? new SelectedBrushSurface(intersection.brush, intersection.surfaceIndex, intersection.worldPlane) : null;
                hoverRotation     = SelectionUtility.FindDragOrientation(sceneView, normal, sourceSurfaceAlignment, destinationSurfaceAlignment);
                haveNoParent      = (hoverParent == null);
                hoverSiblingIndex = int.MaxValue;

                RealtimeCSG.CSGGrid.SetForcedGrid(camera, intersection.worldPlane);

                // Since we're snapping points to grid and do not have a relative distance, relative snapping makes no sense
                var doGridSnapping = RealtimeCSG.CSGSettings.ActiveSnappingMode != SnapMode.None;
                if (doGridSnapping)
                {
                    var localPoints = new Vector3[8];
                    var localPlane  = intersection.worldPlane;
                    for (var i = 0; i < localPoints.Length; i++)
                    {
                        localPoints[i] = GeometryUtility.ProjectPointOnPlane(localPlane, (hoverRotation * projectedBounds[i]) + hoverPosition);
                    }

                    hoverPosition += RealtimeCSG.CSGGrid.SnapDeltaToGrid(camera, MathConstants.zeroVector3, localPoints);
                }
                hoverPosition = GeometryUtility.ProjectPointOnPlane(intersection.worldPlane, hoverPosition);                  // + (normal * 0.01f);

                EnableVisualObjects();
                return(true);
            }
            finally
            {
                if (!UpdateLoop.IsActive())
                {
                    UpdateLoop.ResetUpdateRoutine();
                }
            }
        }