Ejemplo n.º 1
0
        /// <summary>
        /// Per-frame check to see if mouse is hovering over any axis.
        /// </summary>
        private void SelectAxis(Vector2 mousePosition)
        {
            if (!Enabled)
            {
                return;
            }

            float closestintersection = float.MaxValue;
            Ray   ray = _camera.CreateCameraRay(mousePosition);

            var intersection = XSphere.Intersects(ray);

            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.X;
                    closestintersection = intersection.Value;
                }
            }
            intersection = YSphere.Intersects(ray);
            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.Y;
                    closestintersection = intersection.Value;
                }
            }
            intersection = ZSphere.Intersects(ray);
            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.Z;
                    closestintersection = intersection.Value;
                }
            }

            if (closestintersection >= float.MaxValue || closestintersection <= float.MinValue)
            {
                ActiveAxis = GizmoAxis.None;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Per-frame check to see if mouse is hovering over any axis.
        /// </summary>
        private void SelectAxis(Vector2 mousePosition)
        {
            if (!Enabled)
            {
                return;
            }

            float closestintersection = float.MaxValue;
            Ray   ray = ConvertMouseToRay(mousePosition);

            if (ActiveMode == GizmoMode.Translate)
            {
                // transform ray into local-space of the boundingboxes.
                ray.Direction = Vector3.TransformNormal(ray.Direction, Matrix.Invert(_gizmoWorld));
                ray.Position  = Vector3.Transform(ray.Position, Matrix.Invert(_gizmoWorld));
            }

            #region X,Y,Z Boxes

            float?intersection = XAxisBox.Intersects(ray);
            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.X;
                    closestintersection = intersection.Value;
                }
            }

            intersection = YAxisBox.Intersects(ray);
            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.Y;
                    closestintersection = intersection.Value;
                }
            }

            intersection = ZAxisBox.Intersects(ray);
            if (intersection.HasValue)
            {
                if (intersection.Value < closestintersection)
                {
                    ActiveAxis          = GizmoAxis.Z;
                    closestintersection = intersection.Value;
                }
            }

            #endregion

            if (ActiveMode == GizmoMode.Rotate || ActiveMode == GizmoMode.UniformScale || ActiveMode == GizmoMode.NonUniformScale)
            {
                #region BoundingSpheres

                intersection = XSphere.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value < closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.X;
                        closestintersection = intersection.Value;
                    }
                }

                intersection = YSphere.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value < closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.Y;
                        closestintersection = intersection.Value;
                    }
                }

                intersection = ZSphere.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value < closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.Z;
                        closestintersection = intersection.Value;
                    }
                }
                #endregion
            }

            if (ActiveMode == GizmoMode.Translate || ActiveMode == GizmoMode.NonUniformScale || ActiveMode == GizmoMode.UniformScale)
            {
                // if no axis was hit (x,y,z) set value to lowest possible to select the 'farthest' intersection for the XY,XZ,YZ boxes.
                // This is done so you may still select multi-axis if you're looking at the gizmo from behind!
                if (closestintersection >= float.MaxValue)
                {
                    closestintersection = float.MinValue;
                }

                #region BoundingBoxes

                intersection = XYBox.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value > closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.XY;
                        closestintersection = intersection.Value;
                    }
                }

                intersection = XZAxisBox.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value > closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.ZX;
                        closestintersection = intersection.Value;
                    }
                }

                intersection = YZBox.Intersects(ray);
                if (intersection.HasValue)
                {
                    if (intersection.Value > closestintersection)
                    {
                        ActiveAxis          = GizmoAxis.YZ;
                        closestintersection = intersection.Value;
                    }
                }

                #endregion
            }

            if (closestintersection >= float.MaxValue || closestintersection <= float.MinValue)
            {
                ActiveAxis = GizmoAxis.None;
            }
        }