XNA 4.0.4在rendertarget中使用Z-buffer

时间:2014-11-11 22:42:52

标签: c# xna-4.0 depth-buffer rendertarget

我尝试在rendertarget2d中绘制我的3d世界。 如果我使用默认的rendertarget,那么世界看起来很完美......但是如果我渲染到rendertarget2d,则z缓冲区不起作用,后台中的对象将被绘制在其他人面前。

是rendertarget2d合适的班级吗? 我做错了什么?

public class Camera
{
    public Matrix View { get; set; }
    public Matrix Projection { get; set; }
    public RenderTarget2D RenderTarget { get; set; }
    public Vector2 Screen { get; set; }
    public Effect ZBufferEffect { get; set; }
    private GraphicsDevice device;


    public void Draw(SpriteBatch spriteBatch, Player player, SpriteFont font)
    {
        device.SetRenderTarget(RenderTarget);

        device.Clear(Color.Black);

        device.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };

        // Draw the scene



        View = Matrix.Invert(Object.OBJECTS[0].PosRotSca);
        View *= Matrix.CreateTranslation(0, -5, -5);
        View *= Matrix.CreateRotationX(MathHelper.ToRadians(20));
        foreach(Object obj in Object.OBJECTS)
        {               
            foreach(ModelMesh mesh in Game1.modelArray[obj.Model].Meshes)
            {
                //foreach (ModelMeshPart part in mesh.MeshParts)
                //{
                //    part.Effect = ZBufferEffect;
                //    ZBufferEffect.Parameters["World"].SetValue(obj.PosRotSca * mesh.ParentBone.Transform);
                //    ZBufferEffect.Parameters["View"].SetValue(View);
                //    ZBufferEffect.Parameters["Projection"].SetValue(Projection);
                //    ZBufferEffect.Parameters["ModelTexture"].SetValue(Game1.textureArray[obj.Texture]);
                //    ZBufferEffect.Parameters["invert"].SetValue(false);
                //}
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Texture = Game1.textureArray[obj.Texture];
                    effect.TextureEnabled = true;
                    effect.World = obj.PosRotSca;
                    effect.View = View;
                    effect.Projection = Projection;
                }
                mesh.Draw();
            }
        }
        device.SetRenderTarget(null);

        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
            SamplerState.LinearClamp, DepthStencilState.Default,
            RasterizerState.CullNone);

        spriteBatch.Draw(RenderTarget, new Rectangle(0, 0, device.Viewport.Width, device.Viewport.Height), Color.White);

        spriteBatch.End();
    }


    public Camera(GraphicsDevice device)
    {

        RenderTarget2D rt = new RenderTarget2D(
            device,
            device.PresentationParameters.BackBufferWidth,
            device.PresentationParameters.BackBufferHeight,
            false,
            device.PresentationParameters.BackBufferFormat,
            DepthFormat.Depth24);

        RenderTarget = rt;


        this.device = device;

    }        

}

我可以更改rendertarget中的某些设置吗?或者我是否必须编写Z缓冲区着色器? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

由于我现在没有在我的机器上安装XNA,我只能根据以前的经验进行推测。

我看到两个可能出错的地方。

首先,SpriteBatch会更改许多需要更改为3D渲染的渲染状态。在这里查看所有州的概述:http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

其次,但我不认为这是你问题的原因, 尝试使用不同的清除过载清除渲染目标。

GraphicsDevice.Clear具有以下重载,您可以在其中指定要清除的深度值:

public void Clear (
         ClearOptions options,
         Color color,
         float depth,
         int stencil
)

阅读此处的文档:http://msdn.microsoft.com/en-us/library/ff433734(v=xnagamestudio.40).aspx