示例#1
0
        private void Update()
        {
            // Animation
            if (ChkAnimated.IsChecked != null && ChkAnimated.IsChecked.Value)
            {
                CameraZ = -Math.Abs(Math.Sin(_f)) * (FarPlane - NearPlane) * 1.5;
                CameraY = Math.Sin(_f * 10) * 1000;
                _f     += 0.008;
            }

            // Create global transformations
            var      vw          = Viewport.ActualWidth;
            var      vh          = Viewport.ActualHeight;
            var      invertYAxis = Matrix3DFactory.CreateScale(1, -1, 1);
            var      translate   = Matrix3DFactory.CreateTranslation(TranslateX, TranslateY, TranslateZ);
            var      rotateX     = Matrix3DFactory.CreateRotationX(MathHelper.ToRadians(RotateX));
            var      rotateY     = Matrix3DFactory.CreateRotationY(MathHelper.ToRadians(RotateY));
            var      rotateZ     = Matrix3DFactory.CreateRotationZ(MathHelper.ToRadians(RotateZ));
            var      scale       = Matrix3DFactory.CreateScale(ScaleX, ScaleY, ScaleZ);
            var      lookAt      = Matrix3DFactory.CreateLookAtLH(CameraX, CameraY, CameraZ, CameraLookAtX, CameraLookAtY, CameraLookAtZ);
            var      viewport    = Matrix3DFactory.CreateViewportTransformation(vw, vh);
            Matrix3D projectionMatrix;

            projectionMatrix = ChkPerspective.IsChecked != null && ChkPerspective.IsChecked.Value ? Matrix3DFactory.CreatePerspectiveFieldOfViewLH(MathHelper.ToRadians(FieldOfView), vw / vh, NearPlane, FarPlane) : Matrix3DFactory.CreateOrthographicLH(vw, vh, NearPlane, FarPlane);

            // Transform all elements
            var selectedMatrix = Matrix3D.Identity;

            foreach (var elem in _elements)
            {
                // The UIElement
                var e = elem.Element;

                // Create basic transformation matrices
                var centerAtOrigin = Matrix3DFactory.CreateTranslation(-e.ActualWidth * 0.5, -e.ActualHeight * 0.5, 0);
                var baseTranslate  = Matrix3DFactory.CreateTranslation(elem.PositionX, elem.PositionY, elem.PositionZ);

                // Combine the transformation matrices
                var m = Matrix3D.Identity;
                m = m * centerAtOrigin;
                m = m * invertYAxis;

                // Apply the world transformation to the selected element
                if (elem == _selectedElement)
                {
                    m = m * scale;
                    m = m * rotateX * rotateY * rotateZ;
                    m = m * translate;

                    // Should the camera target be fixed at the selected element?
                    if (ChkLookAtSelected.IsChecked != null && ChkLookAtSelected.IsChecked.Value)
                    {
                        lookAt = Matrix3DFactory.CreateLookAtLH(CameraX, CameraY, CameraZ, elem.PositionX, elem.PositionY, elem.PositionZ);
                    }
                }

                // Calculate the final view projection matrix
                m = m * baseTranslate;
                m = Matrix3DFactory.CreateViewportProjection(m, lookAt, projectionMatrix, viewport);

                if (elem == _selectedElement)
                {
                    selectedMatrix = m;
                }

                // Apply the transformation to the UIElement
                e.Projection = new Matrix3DProjection {
                    ProjectionMatrix = m
                };
            }

            // Trace
            TxtTrace1.Text = String.Format("{0} Elements. Matrix:\r\n{1}", _elements.Count, selectedMatrix.Dump());
        }