Xna将场景渲染到多个目标上

调用GraphicsDevice.setRenderTarget(target),将要渲染的目标放入.(多个调用之),然后从target中获取渲染后的纹理,使用SpriteBatch绘制的哦屏幕上,SpriteBatch需要保持状态.下面是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BookCode
{
class Game2 : Game
{
GraphicsDeviceManager graphics;

QuakeCamera camera;

SpriteBatch spriteBatch;

RenderTarget2D renderTarget;
RenderTarget2D renderTarget2;

CoordCross cCross;

Model tank;
Matrix[] tankTransform;
Matrix world = Matrix.CreateScale(0.01f);

public Game2()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

}
protected override void Initialize()
{
base.Initialize();

cCross = new CoordCross(GraphicsDevice);
spriteBatch = new SpriteBatch(GraphicsDevice);
//1
camera = new QuakeCamera(GraphicsDevice.Viewport);
renderTarget = new RenderTarget2D(GraphicsDevice, 200, 100, 1, SurfaceFormat.Color);
//2
renderTarget2 = new RenderTarget2D(GraphicsDevice, 400, 300, 1, SurfaceFormat.Color);
}

protected override void LoadContent()
{
base.LoadContent();
tank = Content.Load<Model>("tank");
tankTransform = new Matrix[tank.Bones.Count];
tank.CopyAbsoluteBoneTransformsTo(tankTransform);
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
KeyboardState keyState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
camera.Update(mouseState, keyState, GamePad.GetState(PlayerIndex.One));

if (keyState.IsKeyDown(Keys.Escape))
Exit();
}

protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);

//渲染到 目标1
GraphicsDevice.SetRenderTarget(0, renderTarget);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);
cCross.Draw(camera.ViewMatrix, camera.ProjectionMatrix);
foreach (ModelMesh mesh in tank.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = tankTransform[mesh.ParentBone.Index] * world;
effect.View = camera.ViewMatrix;
effect.Projection = camera.ProjectionMatrix;
}
mesh.Draw();
}
//渲染到目标2
GraphicsDevice.SetRenderTarget(0, renderTarget2);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);
cCross.Draw(camera.InvertViewMatrix, camera.ProjectionMatrix);
foreach (ModelMesh mesh in tank.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = tankTransform[mesh.ParentBone.Index] * world;
effect.View = camera.ViewMatrix;
effect.Projection = camera.ProjectionMatrix;
}
mesh.Draw();
}
//切换渲染屏幕目标
GraphicsDevice.SetRenderTarget(0, null);
GraphicsDevice.Clear(Color.Cornsilk);
Texture2D texture = renderTarget.GetTexture();
Texture2D texture2 = renderTarget2.GetTexture();

spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.BackToFront,SaveStateMode.SaveState);
spriteBatch.Draw(texture, new Rectangle(0, 0, 200, 100), Color.White);
spriteBatch.Draw(texture2, new Rectangle(0, 140, 400, 300), Color.White);
spriteBatch.End();

}
}
}




材质太大,只贴上源码了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gamebox1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值