I've been trying to apply a desaturation and white noise effect to my XNA project, and I've managed to do it but now I'm encountering some issues with the draw order.
In the code below, the commented out lines are what is causing the issue. If they are commented, the draw order problem is fixed, but then the screen isn't desaturated. When they're uncommented, the screen is desatured as I wish but the draw order problem occurs.
//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();*/
Try the following code:
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;
}