Ejemplo n.º 1
0
        private static Texture2D RenderToTarget(Model model, ModelAnimationPlayer player, Microsoft.Xna.Framework.Game game, Matrix world, out Texture2D Sillouette)
        {
            DepthStencilBuffer old = ShadowMapManager.SetupShadowMap(game.GraphicsDevice, ref renderTarget, ref depthBuffer);

            BoundingSphere bs = new BoundingSphere();

            bool first = true;

            foreach (ModelMesh mesh in model.Meshes)
            {
                if (first)
                {
                    bs    = mesh.BoundingSphere;
                    first = false;
                }
                else
                {
                    bs = BoundingSphere.CreateMerged(bs, mesh.BoundingSphere);
                }
            }

            bs.Center.X = bs.Center.Z;
            bs.Radius  *= 1.5f;

            ShaderManager.AddEffect(ShaderManager.EFFECT_ID.PHYSICS, "PhysicsRenderer", game);
            ShaderManager.SetCurrentEffect(ShaderManager.EFFECT_ID.PHYSICS);
            ShaderManager.SetValue("World", world);

            Matrix m = Matrix.Identity;

            m.M33 = 0;
            m.M43 = 0.5f;
            ShaderManager.SetValue("Projection", m);


            Matrix view;

            view = Matrix.CreateTranslation(-bs.Center) * Matrix.CreateScale(1 / bs.Radius, 1 / bs.Radius, 1);

            ShaderManager.SetValue("Projection", m);
            ShaderManager.SetValue("View", view);
            ShaderManager.SetValue("usingBones", player != null);

            if (player != null)
            {
                ShaderManager.SetValue("Bones", player.GetSkinTransforms());
            }

            ShaderManager.CommitChanges();
            List <Effect> effects = new List <Effect>();

            game.GraphicsDevice.Clear(Color.Black);

            foreach (ModelMesh mesh in model.Meshes)
            {
                effects.Clear();

                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    effects.Add(part.Effect);
                    part.Effect = ShaderManager.GetCurrentEffect();
                }
                mesh.Draw();
                for (int i = 0; i < mesh.MeshParts.Count; i++)
                {
                    mesh.MeshParts[i].Effect = effects[i];
                }
            }

            ShadowMapManager.ResetGraphicsDevice(game.GraphicsDevice, old);

            Color[] textureColors = new Color[(int)(TextureSize.X * TextureSize.Y)];
            renderTarget.GetTexture().GetData <Color>(textureColors);

            Sillouette = new Texture2D(game.GraphicsDevice, (int)TextureSize.X, (int)TextureSize.Y);
            Sillouette.SetData <Color>(textureColors);
            return(renderTarget.GetTexture());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The load content.
        /// </summary>
        protected override void LoadContent()
        {
            this.HeaderFont    = this.Game.Content.Load <SpriteFont>("Font//TutorialHeader");
            this.BodyFont      = this.Game.Content.Load <SpriteFont>("Font//TutorialBody");
            this.DialogTexture = this.Game.Content.Load <Texture2D>("Textures\\DialogTexture");

            ShaderManager.AddEffect(ShaderManager.EFFECT_ID.TEXTURE, "TextureEffect", this.Game);

            // create the texture that will be shown
            var renderTarget = new RenderTarget2D(
                this.Game.GraphicsDevice,
                512,
                (int)(512 * (this.TextBoxSize.Y / this.TextBoxSize.X)),
                1,
                SurfaceFormat.Color);
            var depthBuffer = new DepthStencilBuffer(
                this.Game.GraphicsDevice,
                512,
                (int)(512 * (this.TextBoxSize.Y / this.TextBoxSize.X)),
                DepthFormat.Depth16);

            DepthStencilBuffer old = ShadowMapManager.SetupShadowMap(
                this.Game.GraphicsDevice, ref renderTarget, ref depthBuffer, Color.TransparentWhite);

            var spriteBatch = (SpriteBatch)this.scene.Game.Services.GetService(typeof(SpriteBatch));

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            spriteBatch.Draw(
                this.DialogTexture,
                new Rectangle(0, 0, 512, (int)(512 * (this.TextBoxSize.Y / this.TextBoxSize.X))),
                Color.White);

            string header = this.HeaderText.Replace("\\n", "\n");

            spriteBatch.DrawString(
                this.HeaderFont,
                header,
                new Vector2(renderTarget.Width / 2.0f, (renderTarget.Height / 10.0f) + 30),
                Color.Black,
                0.0f,
                this.HeaderFont.MeasureString(header) / 2.0f,
                1.0f,
                SpriteEffects.None,
                1.0f);
            string body    = this.BodyText.Replace("\\n", "\n");
            var    textPos = new Vector2(renderTarget.Width / 2.0f, renderTarget.Height / 10.0f) +
                             new Vector2(
                this.BodyFont.MeasureString(body).X / -2.0f, 2 * this.HeaderFont.MeasureString(header).Y);

            spriteBatch.DrawString(
                this.BodyFont,
                body,
                textPos,
                Color.Black,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                1.0f);
            spriteBatch.End();

            ShadowMapManager.ResetGraphicsDevice(this.Game.GraphicsDevice, old);
            this.TextTexture = renderTarget.GetTexture();

            var halfTextBoxSizeHeight = new Vector3(0.0f, this.TextBoxSize.Y / 2.0f, 0.0f);
            var halfTextBoxSizeWidth  = new Vector3(this.TextBoxSize.X / 2.0f, 0.0f, 0.0f);

            this.textSprite = new[]
            {
                new TextureSprite(-halfTextBoxSizeWidth - halfTextBoxSizeHeight, new Vector2(0.0f, 1.0f)),
                new TextureSprite(-halfTextBoxSizeWidth + halfTextBoxSizeHeight, Vector2.Zero),
                new TextureSprite(halfTextBoxSizeWidth - halfTextBoxSizeHeight, new Vector2(1.0f, 1.0f)),
                new TextureSprite(halfTextBoxSizeWidth + halfTextBoxSizeHeight, new Vector2(1.0f, 0.0f)),
            };

            this.PhysicsBody = BodyFactory.Instance.CreateRectangleBody(
                halfTextBoxSizeWidth.X * 2, halfTextBoxSizeHeight.Y * 2, 1.0f);
            this.PhysicsGeometry = new Physics.GeomDC(this, GeomFactory.Instance.CreateRectangleGeom(
                                                          this.PhysicsBody, halfTextBoxSizeWidth.X * 2, halfTextBoxSizeHeight.Y * 2));
            this.aabb = this.PhysicsGeometry.AABB;
        }