调用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();
}
}
}
材质太大,只贴上源码了