Exemple #1
0
        public Gizmo(Device1 device) 
        {
            Mode = GizmoMode.None;
            SelectedAxis = -1;
            TranslationSnapValue = 1.0f;
            RotationSnapValue = 5.0f;
            ScaleSnapValue = 0.1f;
            ScaleSensitivity = 0.2f;

            _StartPosition = null;
            _StartScale = null;
            _StartRotation = null;

            // Translation Gizmo
            TranslationGizmo = new List<IModel>();
            var XArrow = new OvmModel("Resources//Arrow.ovm");
            XArrow.Create(device);
            XArrow.Shader = new ColorShader(new Color(255, 0, 0, 255));
            TranslationGizmo.Add(XArrow);

            var YArrow = new OvmModel("Resources//Arrow.ovm");
            YArrow.Create(device);
            YArrow.Shader = new ColorShader(new Color(0, 255, 0, 255));
            YArrow.Rotation.Z = 90;
            TranslationGizmo.Add(YArrow);

            var ZArrow = new OvmModel("Resources//Arrow.ovm");
            ZArrow.Create(device);
            ZArrow.Shader = new ColorShader(new Color(0, 0, 255, 255));
            ZArrow.Rotation.Y = 90;
            TranslationGizmo.Add(ZArrow);

            // Rotation Gizmo
            RotationGizmo = new List<IModel>();
            var XRotatinGizmo = new OvmModel("Resources//RotationGizmo.ovm");
            XRotatinGizmo.Create(device);
            XRotatinGizmo.Shader = new ColorShader(new Color(255, 0, 0, 255));
            RotationGizmo.Add(XRotatinGizmo);

            var YRotatinGizmo = new OvmModel("Resources//RotationGizmo.ovm");
            YRotatinGizmo.Create(device);
            YRotatinGizmo.Shader = new ColorShader(new Color(0, 255, 0, 255));
            YRotatinGizmo.Rotation.Z = 90;
            RotationGizmo.Add(YRotatinGizmo);

            var ZRotatinGizmo = new OvmModel("Resources//RotationGizmo.ovm");
            ZRotatinGizmo.Create(device);
            ZRotatinGizmo.Shader = new ColorShader(new Color(0, 0, 255, 255));
            ZRotatinGizmo.Rotation.Y = 90;
            RotationGizmo.Add(ZRotatinGizmo);

            // Scale Gizmo
            ScaleGizmo = new List<IModel>();
            var XScaleArrow = new OvmModel("Resources//ScaleArrow.ovm");
            XScaleArrow.Create(device);
            XScaleArrow.Shader = new ColorShader(new Color(255, 0, 0, 255));
            ScaleGizmo.Add(XScaleArrow);

            var YScaleArrow = new OvmModel("Resources//ScaleArrow.ovm");
            YScaleArrow.Create(device);
            YScaleArrow.Shader = new ColorShader(new Color(0, 255, 0, 255));
            YScaleArrow.Rotation.Z = 90;
            ScaleGizmo.Add(YScaleArrow);

            var ZScaleArrow = new OvmModel("Resources//ScaleArrow.ovm");
            ZScaleArrow.Create(device);
            ZScaleArrow.Shader = new ColorShader(new Color(0, 0, 255, 255));
            ZScaleArrow.Rotation.Y = 90;;
            ScaleGizmo.Add(ZScaleArrow);
        }
Exemple #2
0
        public void Update(Camera camera, ViewportF view, System.Windows.Point mousePos, GameObject linkedObject)
        {
             UpdateModels(camera);

            if (InputManager.IsLeftMouseButtonDown() && SelectedAxis != -1)
            {
                //Create a Ray from the mouse position
                Matrix worldViewProjectionMatrix = Matrix.Identity *  camera.ViewMatrix * camera.ProjectionMatrix;
                Ray ray = Ray.GetPickRay((int)(mousePos.X), (int)(mousePos.Y), view, worldViewProjectionMatrix);
                Plane plane;
                Matrix RotationMat = Matrix.Identity;
                switch (Mode)
                {
                    case GizmoMode.None:
                        break;
                    case GizmoMode.Translate:
                        if (_StartPosition == null)
                        {
                            _StartPosition = new MyVector3();
                            _StartPosition.Vector3 = linkedObject.Transform.Position.Vector3; ;
                        }
                        Vector3 translationDelta = Vector3.Zero;
                        switch (SelectedAxis)
                        {
                            case 0:
                                plane = new Plane(_HitPosition, camera.Forward);
                                translationDelta = CalculateTransformDelta(plane, ray, camera);
                                Console.WriteLine(_HitPosition);

                                translationDelta = new Vector3(translationDelta.X, 0, 0);
                                break;
                            case 1:
 
                                plane = new Plane(_HitPosition, camera.Forward);
                                translationDelta = CalculateTransformDelta(plane, ray, camera);
                                Console.WriteLine(_HitPosition);
                                translationDelta = new Vector3(0, translationDelta.Y, 0);
                                break;
                            case 2:
                                plane = new Plane(_HitPosition, Vector3.Up);
                                translationDelta = CalculateTransformDelta(plane, ray, camera);
                                translationDelta = new Vector3(0, 0, translationDelta.Z);
                                break;
                            default:
                                break;
                        }

                        var scaledTranslationDelta = new Vector3((int)(translationDelta.X / TranslationSnapValue) * TranslationSnapValue,
                                                                 (int)(translationDelta.Y / TranslationSnapValue) * TranslationSnapValue,
                                                                 (int)(translationDelta.Z / TranslationSnapValue) * TranslationSnapValue);

                        _LastIntersectionPosition += scaledTranslationDelta;
                        linkedObject.Transform.Position.Vector3 += scaledTranslationDelta;
                        break;

                    case GizmoMode.Rotate:
                        if (_StartRotation == null)
                        {
                            _StartRotation = new MyVector3();
                            _StartRotation.Vector3 = linkedObject.Transform.RotationEuler.Vector3; ;
                        }
                        Vector3 RotateDelta = Vector3.Zero;
                        Vector3 MouseDelta = Vector3.Zero;
                        switch (SelectedAxis)
                        {
                            case 0:
                                plane = new Plane(_HitPosition, camera.Forward);
                                RotateDelta = CalculateTransformDelta(plane, ray, camera);
                                MouseDelta = new Vector3(0, RotateDelta.Y, 0);
                                RotateDelta = new Vector3(RotateDelta.Y, 0, 0);

                                break;
                            case 1:
                                plane = new Plane(_HitPosition, camera.Forward);
                                RotateDelta = CalculateTransformDelta(plane, ray, camera);
                                MouseDelta = new Vector3(RotateDelta.X, 0, 0);
                                RotateDelta = new Vector3(0, -RotateDelta.X, 0);
                                break;
                            case 2:
                                plane = new Plane(_HitPosition, camera.Up);
                                RotateDelta = CalculateTransformDelta(plane, ray, camera);
                                MouseDelta = new Vector3(0, 0, RotateDelta.Z);
                                RotateDelta = new Vector3(0, 0, RotateDelta.Z);
                                break;
                            default:
                                break;
                        }

                        RotateDelta *= 10.0f;
                        var scaledRotationDelta = new Vector3((int)(RotateDelta.X / RotationSnapValue) * RotationSnapValue,
                                                                 (int)(RotateDelta.Y / RotationSnapValue) * RotationSnapValue,
                                                                 (int)(RotateDelta.Z / RotationSnapValue) * RotationSnapValue);
                        
                        _LastIntersectionPosition += MouseDelta;
                        linkedObject.Transform.RotationEuler.X += scaledRotationDelta.X;
                        linkedObject.Transform.RotationEuler.Y += scaledRotationDelta.Y;
                        linkedObject.Transform.RotationEuler.Z += scaledRotationDelta.Z;
                        break;
                    case GizmoMode.Scale:
                        Vector3 scaleDelta = Vector3.Zero;
                        if (_StartScale == null)
                        {
                            _StartScale = new MyVector3();
                            _StartScale.Vector3 = linkedObject.Transform.Scale.Vector3; ;
                        }
                        switch (SelectedAxis)
                        {
                            case 0:
                                plane = new Plane(_HitPosition, camera.Forward);
                                scaleDelta = CalculateTransformDelta(plane, ray, camera);
                                scaleDelta = new Vector3(scaleDelta.X, 0, 0);
                                break;
                            case 1:
                                plane = new Plane(_HitPosition, camera.Forward);
                                scaleDelta = CalculateTransformDelta(plane, ray, camera);
                                scaleDelta = new Vector3(0, scaleDelta.Y, 0);
                                break;
                            case 2:
                                plane = new Plane(_HitPosition, Vector3.Up);
                                scaleDelta = CalculateTransformDelta(plane, ray, camera);
                                scaleDelta = new Vector3(0, 0, scaleDelta.Z);
                                break;
                            default:
                                break;
                        }

                        var scaledScaleDelta = new Vector3((int)(scaleDelta.X / ScaleSnapValue) * ScaleSnapValue,
                                                           (int)(scaleDelta.Y / ScaleSnapValue) * ScaleSnapValue,
                                                           (int)(scaleDelta.Z / ScaleSnapValue) * ScaleSnapValue) ;

                        _LastIntersectionPosition += scaledScaleDelta;

                        RotationMat = Matrix.RotationQuaternion(linkedObject.Transform.Rotation);
                        scaledScaleDelta = Vector3.TransformCoordinate(scaledScaleDelta, RotationMat);

                        linkedObject.Transform.Scale.Vector3 += scaledScaleDelta * new Vector3(1,1,-1) ;
                        break;
                    default:
                        break;
                }

                
            }
            else 
            {
                if (SelectedAxis != -1 )
                {
                    switch (Mode)
                    {
                        case GizmoMode.None:
                            break;
                        case GizmoMode.Translate:
                                var UndoCommand = new RelayCommand<MyVector3>(linkedObject.Transform.SetPosition);
                                UndoRedoStack.AddUndoCommand(UndoCommand, _StartPosition);
                                UndoRedoStack.ClearRedoStack();
                                _StartPosition = null;
                            break;
                        case GizmoMode.Rotate:
                                var UndoRotateCommand = new RelayCommand<MyVector3>(linkedObject.Transform.SetRotation);
                                UndoRedoStack.AddUndoCommand(UndoRotateCommand, _StartRotation);
                                UndoRedoStack.ClearRedoStack();
                                _StartRotation = null;
                            break;
                        case GizmoMode.Scale:
                                var UndoScaleCommand = new RelayCommand<MyVector3>(linkedObject.Transform.SetScale);
                                UndoRedoStack.AddUndoCommand(UndoScaleCommand, _StartScale);
                                UndoRedoStack.ClearRedoStack();
                                _StartScale = null;
                            break;
                        default:
                            break;
                    }
                }
                SelectedAxis = -1;
                _LastIntersectionPosition = Vector3.Zero;
            }

        }