Example #1
0
        /// <summary>
        /// When the mouse is pressed in the 3D view, we want to select the clicked object.
        /// </summary>
        /// <param name="viewport">The viewport that was clicked</param>
        /// <param name="camera"></param>
        /// <param name="e">The click event</param>
        protected override void MouseDown(MapViewport viewport, PerspectiveCamera camera, ViewportEvent e)
        {
            // First, get the ray that is cast from the clicked point along the viewport frustrum
            var(start, end) = camera.CastRayFromScreen(new Vector3(e.X, e.Y, 0));
            var ray = new Line(start, end);

            // Grab all the elements that intersect with the ray
            var closestObject = Document.Map.Root.GetIntersectionsForVisibleObjects(ray)
                                .Where(x => x.Object is Solid)
                                .Select(x => (Solid)x.Object)
                                .FirstOrDefault();

            SelectObject(closestObject);
        }
Example #2
0
        protected override void MouseDown(MapDocument document, MapViewport viewport, PerspectiveCamera camera,
                                          ViewportEvent e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            e.Handled = true;

            // First, get the ray that is cast from the clicked point along the viewport frustrum
            var(start, end) = camera.CastRayFromScreen(new Vector3(e.X, e.Y, 0));
            var ray = new Line(start, end);

            // Grab all the elements that intersect with the ray
            var hits = Selection.Where(x => x.Copy.BoundingBox.IntersectsWith(ray));

            // Sort the list of intersecting elements by distance from ray origin
            var clickedFace = hits
                              .SelectMany(x => x.Copy.Faces.Select(f => new { Solid = x, Face = f }))
                              .Select(x => new { Item = x, Intersection = GetIntersectionPoint(x.Face, ray) })
                              .Where(x => x.Intersection != null)
                              .OrderBy(x => (x.Intersection.Value - ray.Start).Length())
                              .Select(x => x.Item)
                              .FirstOrDefault();


            var faces = new List <SolidFace>();

            if (clickedFace != null)
            {
                if (KeyboardState.Shift)
                {
                    faces.AddRange(clickedFace.Solid.Copy.Faces.Select(x => new SolidFace(clickedFace.Solid, x)));
                }
                else
                {
                    faces.Add(new SolidFace(clickedFace.Solid, clickedFace.Face));
                }
            }

            if (!KeyboardState.Ctrl)
            {
                ClearSelection();
            }
            _selectedFaces.AddRange(faces);

            Invalidate();
        }
Example #3
0
        // 3D interaction

        protected override void MouseDown(MapDocument document, MapViewport viewport, PerspectiveCamera camera, ViewportEvent e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            // Get the ray that is cast from the clicked point along the viewport frustrum
            var(rs, re) = camera.CastRayFromScreen(new Vector3(e.X, e.Y, 0));
            var ray = new Line(rs, re);

            // Grab all the elements that intersect with the ray
            var hit = document.Map.Root.GetIntersectionsForVisibleObjects(ray).FirstOrDefault();

            if (hit == null)
            {
                return;              // Nothing was clicked
            }
            CreateEntity(document, hit.Intersection);
        }
        protected override void MouseDown(MapDocument document, MapViewport viewport, PerspectiveCamera camera, ViewportEvent e)
        {
            var vp = viewport;

            if (vp == null || (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right))
            {
                return;
            }

            var(start, end) = camera.CastRayFromScreen(new Vector3(e.X, e.Y, 0));
            var ray = new Line(start, end);

            var clickedFace = document.Map.Root.GetBoudingBoxIntersectionsForVisibleObjects(ray)
                              // We only care about solids
                              .OfType <Solid>()
                              // Specifically, their faces
                              .SelectMany(a => a.Faces.Select(f => new { Face = f, Solid = a }))
                              // Get the face intersection points and sort by distance from line start
                              .Select(x => new { x.Face, x.Solid, Intersection = new Polygon(x.Face.Vertices).GetIntersectionPoint(ray) })
                              .Where(x => x.Intersection != null)
                              .OrderBy(x => (x.Intersection.Value - ray.Start).Length())
                              // Select the closest one.
                              .Select(x => x)
                              .FirstOrDefault();

            if (clickedFace == null)
            {
                return;
            }

            if (e.Button == MouseButtons.Left)
            {
                SelectFace(document, camera, clickedFace.Face, clickedFace.Solid);
            }
            else if (e.Button == MouseButtons.Right)
            {
                ApplyFace(document, camera, clickedFace.Face, clickedFace.Solid);
            }
        }