Ejemplo n.º 1
0
        public void Draw(IEditorGame game)
        {
            game.GraphicsDevice.SetBlendState(game.GraphicsDevice.BlendStates.NonPremultiplied);
            var viewProj = game.Camera.ViewMatrix * game.Camera.ProjectionMatrix;

            // CullNone, if the camera is inside the selection.
            var cullNone = Cuboid.Within((Point3)game.Camera.Position);

            if (cullNone)
            {
                game.GraphicsDevice.SetRasterizerState(game.GraphicsDevice.RasterizerStates.CullNone);
            }

            foreach (var face in FacesUtils.AllFaces)
            {
                var quad      = GetQuad(face);
                var leftFace  = face.GetLeftVisualFace();
                var topFace   = face.GetTopVisualFace();
                var transform = new Vector4(
                    Cuboid.GetLengthComponent(leftFace),
                    Cuboid.GetLengthComponent(topFace),
                    Cuboid.GetPositionComponent(leftFace),
                    Cuboid.GetPositionComponent(topFace));

                // TODO: texture scaling on the bottom face is fudged

                // invert in certain edge cases
                if (face.HasSideFaces())
                {
                    transform.Y = -transform.Y;
                    if (face == Faces.Right)
                    {
                        transform.X = -transform.X;
                    }
                    else if (face == Faces.Forward)
                    {
                        transform.X = -transform.X;
                    }
                }

                switch (face)
                {
                case Faces.Left:
                    _effect.TransformMatrix = Matrix.Scaling(0, Cuboid.Height, Cuboid.Width) *
                                              Matrix.Translation(Cuboid.Left - _zBias, Cuboid.Bottom, Cuboid.Forward) *
                                              viewProj;
                    break;

                case Faces.Bottom:
                    _effect.TransformMatrix = Matrix.Scaling(Cuboid.Length, 0, Cuboid.Width) *
                                              Matrix.Translation(Cuboid.Left, Cuboid.Bottom - _zBias, Cuboid.Forward) *
                                              viewProj;
                    break;

                case Faces.Forward:
                    _effect.TransformMatrix = Matrix.Scaling(Cuboid.Length, Cuboid.Height, 0) *
                                              Matrix.Translation(Cuboid.Left, Cuboid.Bottom, Cuboid.Forward - _zBias) *
                                              viewProj;
                    break;

                case Faces.Right:
                    _effect.TransformMatrix = Matrix.Scaling(0, Cuboid.Height, Cuboid.Width) *
                                              Matrix.Translation(Cuboid.Left + Cuboid.Length + _zBias, Cuboid.Bottom, Cuboid.Forward) *
                                              viewProj;
                    break;

                case Faces.Top:
                    _effect.TransformMatrix = Matrix.Scaling(Cuboid.Length, 0, Cuboid.Width) *
                                              Matrix.Translation(Cuboid.Left, Cuboid.Bottom + Cuboid.Height + _zBias, Cuboid.Forward) *
                                              viewProj;
                    break;

                case Faces.Backward:
                    _effect.TransformMatrix = Matrix.Scaling(Cuboid.Length, Cuboid.Height, 0) *
                                              Matrix.Translation(Cuboid.Left, Cuboid.Bottom, Cuboid.Forward + Cuboid.Width + _zBias) *
                                              viewProj;
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }

                _effect.MainTransform = transform * _textureScaling;

                quad.Draw(game.GraphicsDevice);
            }

            // reset to default
            if (cullNone)
            {
                game.GraphicsDevice.SetRasterizerState(game.GraphicsDevice.RasterizerStates.CullBack);
            }
        }