我正在将 3d 模型渲染到 rendertarget2D,然后我使用 sprite 批处理将其绘制到屏幕上。我使用这一行在我的构造函数中启用了抗锯齿:
graphics.PreferMultiSampling = true;
当我将模型直接渲染到后台缓冲区时,抗锯齿按预期工作。现在我正在渲染到 rendertarget2D,抗锯齿不再起作用,并且我得到锯齿状边缘。(我没有调整或旋转 rendertarget2D。)
请您为我解释一下这个谜团吗?
代码:
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 1208;
graphics.PreparingDeviceSettings += setBackbufferPreserveContents;
graphics.PreferMultiSampling = true;
this.IsMouseVisible = true;
this.Window.Title = "MyGame";
graphics.ApplyChanges();
}
public void setBackbufferPreserveContents(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(BoardScreen);
GraphicsDevice.Clear(Color.Transparent);
Board.DrawMe(cam);
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.CornflowerBlue);
sb.Begin();
sb.Draw((Texture2D)BoardScreen, new Rectangle(0, 0, 720, 720), Color.White);
sb.End();
base.Draw(gameTime);
}
Board 属于 DrawableModel 类,它包含一个模型和方法 DrawMe(Camera camera),它只是将模型绘制到屏幕上。Camera 是一个简单的类,包含投影和视图矩阵、相机的位置和目标位置。
更新:
下面是 DrawMe 方法的作用:
public void DrawMe(Camera camera)
{
foreach (ModelMesh mesh in ModelToDraw.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = Matrix.CreateTranslation(Translation);
effect.Projection = camera.Projection;
effect.View = camera.View;
}
mesh.Draw();
}
}
更新 2:
这是我已经完成的主 XNA 文件中的其余函数(更新和卸载内容尚未触及。):
protected override void Initialize()
{
BoardScreen = new RenderTarget2D(graphics.GraphicsDevice, 720, 720);
base.Initialize();
}
protected override void LoadContent()
{
sb = new SpriteBatch(GraphicsDevice);
Board = new DrawableModel(Content.Load<Model>("Models/Board"), Vector3.Zero, Vector3.Zero, Vector3.One);
}