I have a 2D scene with three layers - background, center and foreground. I'm rendering the central layer first so I can perform lighting calculations, and then want to render the background followed by the foreground.
I'm currently trying to set up a stencil buffer when rendering the central layer, so any objects drawn in the background pass will skip pixels that already have central there.
Only trouble is the stencil test doesn't seem to apply (unless I'm completely missing how it's supposed to work), the background objects always overwrite the foreground objects (as they're second).
I'm setting everything up like this :
myColourTarget = new RenderTarget2D( aGraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8 ); ... graphicsDevice.SetRenderTarget( myColourTarget ); graphicsDevice.Clear( ClearOptions.Stencil | ClearOptions.Target, ClearColor, -1.0f, 0 ); myDepthStencilState.StencilEnable = true; myDepthStencilState.StencilFunction = CompareFunction.Always; myDepthStencilState.ReferenceStencil = 1; myDepthStencilState.StencilPass = StencilOperation.Replace; myDepthStencilState.DepthBufferEnable = false; spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend, null, myDepthStencilState, null, objectEffect); // draw all central objects spriteBatch.End() myDepthStencilState.StencilEnable = true; myDepthStencilState.StencilFunction = CompareFunction.Equal; myDepthStencilState.ReferenceStencil = 0; myDepthStencilState.StencilPass = StencilOperation.Keep; myDepthStencilState.DepthBufferEnable = false; spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend, null, myDepthStencilState, null, objectEffect); // draw all background objects spriteBatch.End();
I've tried to do the same sort of effect using depth testing, but I can't seem to get the alpha blending correct - pixels from central objects with an alpha value of 0 always overwrite background images, and the clear colour for the render target bleeds through ( http://i.imgur.com/lRkh3Mj.png )
Any ideas what I'm doing wrong with the stencil test... I figured that the first stencil state would write a 1 whenever a pixel value is written, and the second state would set things up sot that it will only keep the pixel if there's a 0 in the stencil buffer.
Edited by Orangeatang, 23 April 2013 - 06:35 AM.