private void Manipulation_Mouse3DMove(object sender, MouseMove3DEventArgs e)
        {
            if (!isCaptured)
            {
                return;
            }

            Vector3 hit;

            if (currentViewport.UnProjectOnPlane(e.Position.ToVector2(), lastHitPosWS, normal, out hit))
            {
                var rotationMatrix = GetRotationMatrix();

                var moveDir = hit - currentHit;
                currentHit = hit;

                moveDir = Vector3.TransformNormal(moveDir, rotationMatrix.Inverted());

                var sizeDir = Vector3.Zero;
                switch (manipulationType)
                {
                case ManipulationType.PosX:
                    moveDir  = new Vector3(moveDir.X, 0, 0);
                    sizeDir -= moveDir;
                    break;

                case ManipulationType.PosY:
                    moveDir  = new Vector3(0, moveDir.Y, 0);
                    sizeDir -= moveDir;
                    break;

                case ManipulationType.PosZ:
                    moveDir  = new Vector3(0, 0, moveDir.Z);
                    sizeDir -= moveDir;
                    break;

                case ManipulationType.SizeX:
                    sizeDir = new Vector3(moveDir.X, 0, 0);
                    moveDir = Vector3.Zero;
                    break;

                case ManipulationType.SizeY:
                    sizeDir = new Vector3(0, moveDir.Y, 0);
                    moveDir = Vector3.Zero;
                    break;

                case ManipulationType.SizeZ:
                    sizeDir = new Vector3(0, 0, moveDir.Z);
                    moveDir = Vector3.Zero;
                    break;
                }

                moveDir = Vector3.TransformNormal(moveDir, rotationMatrix);
                //dont re-transform sizeDir as it should always be axis aligned

                Position += moveDir;
                Size     += sizeDir;
            }
        }
        private void Manipulation_Mouse3DDown(object sender, MouseDown3DEventArgs e)
        {
            if (e.HitTestResult.ModelHit == negX)
            {
                manipulationType = ManipulationType.PosX;
                direction        = Vector3.UnitX;
            }
            else if (e.HitTestResult.ModelHit == negY)
            {
                manipulationType = ManipulationType.PosY;
                direction        = Vector3.UnitY;
            }
            else if (e.HitTestResult.ModelHit == negZ)
            {
                manipulationType = ManipulationType.PosZ;
                direction        = Vector3.UnitZ;
            }
            else if (e.HitTestResult.ModelHit == posX)
            {
                manipulationType = ManipulationType.SizeX;
                direction        = Vector3.UnitX;
            }
            else if (e.HitTestResult.ModelHit == posY)
            {
                manipulationType = ManipulationType.SizeY;
                direction        = Vector3.UnitY;
            }
            else if (e.HitTestResult.ModelHit == posZ)
            {
                manipulationType = ManipulationType.SizeZ;
                direction        = Vector3.UnitZ;
            }
            else
            {
                manipulationType = ManipulationType.None;
                isCaptured       = false;
                return;
            }

            var material = ((e.HitTestResult.ModelHit as MeshGeometryModel3D).Material as DiffuseMaterial);

            material.DiffuseColor = Color.Yellow;
            currentViewport       = e.Viewport;
            var cameraNormal = Vector3.Normalize(e.Viewport.Camera.CameraInternal.LookDirection);

            this.lastHitPosWS = e.HitTestResult.PointHit;
            var up = Vector3.Cross(cameraNormal, direction);

            normal = Vector3.Cross(up, direction);

            Vector3 hit;

            if (currentViewport.UnProjectOnPlane(e.Position.ToVector2(), lastHitPosWS, normal, out hit))
            {
                currentHit = hit;
                isCaptured = true;
            }
        }
 /// <summary>
 /// Un-project a point from the screen (2D) to a point on plane (3D)
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <param name="p">
 /// The 2D point.
 /// </param>
 /// <param name="position">
 /// plane position
 /// </param>
 /// <param name="normal">
 /// plane normal
 /// </param>
 /// <returns>
 /// A 3D point.
 /// </returns>
 public static Point3D?UnProjectOnPlane(this Viewport3DX viewport, Point p, Point3D position, Vector3D normal)
 {
     if (viewport.UnProjectOnPlane(p.ToVector2(), position.ToVector3(), normal.ToVector3(), out var intersection))
     {
         return(intersection.ToPoint3D());
     }
     else
     {
         return(null);
     }
 }
        private void Scaling_Mouse3DDown(object sender, MouseDown3DEventArgs e)
        {
            if (target == null || !CanBeginTransform(e))
            {
                return;
            }

            if (e.HitTestResult.ModelHit == scaleX)
            {
                manipulationType = ManipulationType.ScaleX;
                direction        = Vector3.UnitX;
            }
            else if (e.HitTestResult.ModelHit == scaleY)
            {
                manipulationType = ManipulationType.ScaleY;
                direction        = Vector3.UnitY;
            }
            else if (e.HitTestResult.ModelHit == scaleZ)
            {
                manipulationType = ManipulationType.ScaleZ;
                direction        = Vector3.UnitZ;
            }
            else
            {
                manipulationType = ManipulationType.None;
                isCaptured       = false;
                return;
            }

            HideAllGeometry();
            (e.HitTestResult.ModelHit as GeometryModel3D).IsRendering = true;

            var material = ((e.HitTestResult.ModelHit as MeshGeometryModel3D).Material as DiffuseMaterial);

            currentColor          = material.DiffuseColor;
            material.DiffuseColor = Color.Yellow;
            currentViewport       = e.Viewport;
            var cameraNormal = Vector3.Normalize(e.Viewport.Camera.CameraInternal.LookDirection);

            this.lastHitPosWS = e.HitTestResult.PointHit;
            var up = Vector3.Cross(cameraNormal, direction);

            normal = Vector3.Cross(up, direction);

            Vector3 hit;

            if (currentViewport.UnProjectOnPlane(e.Position.ToVector2(), lastHitPosWS, normal, out hit))
            {
                currentHit = hit;
                isCaptured = true;
            }
        }
示例#5
0
        private void Rotation_Mouse3DDown(object sender, MouseDown3DEventArgs e)
        {
            if (target == null || !CanBeginTransform(e))
            {
                return;
            }
            if (!(e.HitTestResult.ModelHit is Element3D elem))
            {
                manipulationType = ManipulationType.None;
                isCaptured       = false;
                return;
            }
            if (elem == rotationX)
            {
                manipulationType = ManipulationType.RotationX;
                direction        = new Vector3(1, 0, 0);
            }
            else if (elem == rotationY)
            {
                manipulationType = ManipulationType.RotationY;
                direction        = new Vector3(0, 1, 0);
            }
            else if (elem == rotationZ)
            {
                manipulationType = ManipulationType.RotationZ;
                direction        = new Vector3(0, 0, 1);
            }
            else
            {
                manipulationType = ManipulationType.None;
                isCaptured       = false;
                return;
            }
            var material = ((e.HitTestResult.ModelHit as MeshGeometryModel3D).Material as DiffuseMaterial);

            currentColor          = material.DiffuseColor;
            material.DiffuseColor = Color.Yellow;
            currentViewport       = e.Viewport;
            normal            = Vector3.Normalize(e.Viewport.Camera.CameraInternal.LookDirection);
            this.lastHitPosWS = e.HitTestResult.PointHit;
            //var up = Vector3.Cross(cameraNormal, direction);
            //normal = Vector3.Cross(up, direction);
            if (currentViewport.UnProjectOnPlane(e.Position.ToVector2(), lastHitPosWS, normal, out var hit))
            {
                currentHit = hit;
                isCaptured = true;
            }
        }
示例#6
0
 /// <summary>
 /// Un-project a point from the screen (2D) to a point on plane (3D)
 /// </summary>
 /// <param name="viewport">
 /// The viewport.
 /// </param>
 /// <param name="p">
 /// The 2D point.
 /// </param>
 /// <param name="position">
 /// plane position
 /// </param>
 /// <param name="normal">
 /// plane normal
 /// </param>
 /// <returns>
 /// A 3D point.
 /// </returns>
 public static Vector3?UnProjectOnPlane(this Viewport3DX viewport, Point p, Vector3 position, Vector3 normal)
 {
     return(viewport.UnProjectOnPlane(p.ToVector2(), position, normal));
 }
        private void Translation_Mouse3DDown(object sender, MouseDown3DEventArgs e)
        {
            if (target == null || !CanBeginTransform(e))
            {
                return;
            }

            var overrideNormal = false;

            if (e.HitTestResult.ModelHit == translationX)
            {
                manipulationType = ManipulationType.TranslationX;
                direction        = Vector3.UnitX;
            }
            else if (e.HitTestResult.ModelHit == translationY)
            {
                manipulationType = ManipulationType.TranslationY;
                direction        = Vector3.UnitY;
            }
            else if (e.HitTestResult.ModelHit == translationZ)
            {
                manipulationType = ManipulationType.TranslationZ;
                direction        = Vector3.UnitZ;
            }
            else if (e.HitTestResult.ModelHit == translationXY)
            {
                manipulationType = ManipulationType.TranslationXY;
                direction        = Vector3.UnitZ;
                overrideNormal   = true;
            }
            else if (e.HitTestResult.ModelHit == translationYZ)
            {
                manipulationType = ManipulationType.TranslationYZ;
                direction        = -Vector3.UnitX;
                overrideNormal   = true;
            }
            else if (e.HitTestResult.ModelHit == translationXZ)
            {
                manipulationType = ManipulationType.TranslationXZ;
                direction        = -Vector3.UnitY;
                overrideNormal   = true;
            }
            else
            {
                manipulationType = ManipulationType.None;
                isCaptured       = false;
                return;
            }

            if (LocalAxes)
            {
                direction = Vector3.TransformNormal(direction, rotationMatrix);
            }

            HideAllGeometry();
            (e.HitTestResult.ModelHit as GeometryModel3D).IsRendering = true;

            var material = ((e.HitTestResult.ModelHit as MeshGeometryModel3D).Material as DiffuseMaterial);

            currentColor          = material.DiffuseColor;
            material.DiffuseColor = Color.Yellow;
            currentViewport       = e.Viewport;

            if (overrideNormal)
            {
                normal = direction;
            }
            else
            {
                var cameraNormal = Vector3.Normalize(e.Viewport.Camera.CameraInternal.LookDirection);
                lastHitPosWS = e.HitTestResult.PointHit;
                var up = Vector3.Cross(cameraNormal, direction);
                normal = Vector3.Cross(up, direction);
            }

            Vector3 hit;

            if (currentViewport.UnProjectOnPlane(e.Position.ToVector2(), lastHitPosWS, normal, out hit))
            {
                currentHit = hit;
                isCaptured = true;
            }
        }