XNA绘制订单问题

时间:2013-08-10 00:18:23

标签: c# xna rendering

我一直在尝试对我的XNA项目应用去饱和和白噪声效果,我已经设法做到了,但现在我遇到了绘制顺序的一些问题。

在下面的代码中,注释掉的行是造成问题的原因。如果它们被注释,绘制顺序问题是固定的,但屏幕不会去饱和。当它们被取消注释时,屏幕会根据我的意愿降级,但会出现绘制顺序问题。

//GraphicsDevice.SetRenderTarget(scaleupTarget);
GraphicsDevice.Clear(Color.SeaGreen);            

DrawModel(building_a_mdl, (Matrix.Identity * Matrix.CreateTranslation(100, -14, -100)), building_a_tex);

/*GraphicsDevice.SetRenderTarget(null);
scaleupEffect.Parameters["RandomOffset"].SetValue((float)rng.NextDouble());
GraphicsDevice.Textures[1] = noiseTexture;
spriteBatch.Begin(
    SpriteSortMode.Texture,
    BlendState.AlphaBlend,
    SamplerState.PointClamp,
    null,
    null,
    scaleupEffect);
spriteBatch.Draw(scaleupTarget, Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, upScaleAmount, SpriteEffects.None, 0.0f);
spriteBatch.End();*/

With the issue http://i42.tinypic.com/i2rac2.png With the issue http://i42.tinypic.com/2hf6ez7.png

2 个答案:

答案 0 :(得分:1)

请尝试以下代码:

    RenderTarget2D scaleupTarget = null;

    protected override void Draw(GameTime gameTime)
    {                        
            if (scaleupTarget == null)
            {
                    // be sure to keep the depthformat when creating the new renderTarget2d
                    scaleupTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);                
            }        
            GraphicsDevice.SetRenderTarget(scaleupTarget);
            GraphicsDevice.Clear(ClearOptions.Target, Color.SeaGreen, 1.0f, 0);
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // Setup the rasterState, 
            // if CullMode.None; works, try with 
            // CullMode.CullCounterClockwiseFace
            // afterwards
            var rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            rs.FillMode = FillMode.Solid;

            // Set the GraphicsDevice to use the new RasterizeState
            GraphicsDevice.RasterizerState = rs;

            DrawModel(building_a_mdl, (Matrix.Identity * Matrix.CreateTranslation(100, -14, -100)), building_a_tex);

            scaleupEffect.Parameters["RandomOffset"].SetValue((float)rng.NextDouble());
            GraphicsDevice.Textures[1] = noiseTexture;

            GraphicsDevice.SetRenderTarget(null);

            // SpriteBatch.Begin will set the GraphicsDevice.DepthStencilState to None.
            spriteBatch.Begin(
                    SpriteSortMode.Texture,
                    BlendState.AlphaBlend,
                    SamplerState.PointClamp,
                    null,
                    null,
                    scaleupEffect);
            spriteBatch.Draw(scaleupTarget, Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, upScaleAmount, SpriteEffects.None, 0.0f);            
            spriteBatch.End();

            // Set back to the original depthstate before you continue.
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
    }

答案 1 :(得分:0)

我怀疑即使您尝试更改DepthStencilState,它仍然是您问题的关键。

调用SpriteBatch.Begin()的过程会关闭深度缓冲区测试,因为SpriteBatch.Draw()调用的2d渲染不需要它。然后是下一帧,当你去绘制你的3D东西时,它仍然会关闭,你会遇到你遇到的问题。

因此,在绘制3d内容之前,请确保将DepthStencilState设置回Default

GraphicsDevice.Clear(Color.SeaGreen);

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;            

DrawModel(building_a_mdl, (Matrix.Identity * Matrix.CreateTranslation(100, -14, -100)), building_a_tex);

请参阅此链接以供参考:http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx