【问题标题】:XNA draw order issuesXNA 绘制顺序问题
【发布时间】:2013-08-10 00:18:23
【问题描述】:

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

【问题讨论】:

  • 究竟是什么画不正确?您是否尝试在调用 SpriteBatch.Begin() 时更改 BlendState?
  • @davidsbro 如果您仔细观察黑白图像(很难看到),但您可以看到建筑物另一侧的窗户正在通过它进行渲染——一组横穿模型。我已经更改了 BlendState、SpriteSortMode 和 SamplerState,但都没有运气。
  • @JamesMonger 在绘制模型之前是否更改了 DepthStencialState 和 RasterizerState?
  • @Blau 我更改了 DepthStencilState 但没有运气 - 我没有更改 RasterizerState。
  • 为什么不在绘制 3D 模型之前尝试设置 'GraphicsDevice.RenderState.DepthBufferEnable = true' 行。我只记得我遇到了与您相同的问题,并且在每次抽签解决问题之前将其设置为 true。

标签: c# xna rendering


【解决方案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;
    }

【讨论】:

    【解决方案2】:

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

    调用SpriteBatch.Begin() 的过程会关闭深度缓冲区测试,因为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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多