0

我一直在尝试对我的 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();*/

随着问题 随着问题

4

2 回答 2

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;
    }
于 2013-08-13T10:18:30.987 回答
0

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

调用过程SpriteBatch.Begin()会关闭深度缓冲区测试,因为调用的 2d 渲染不需要它SpriteBatch.Draw()。然后下一帧,当你去绘制你的 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

于 2013-08-10T11:32:17.677 回答