My stencil is not working. It just draw both textures as normal.
graphics = new GraphicsDeviceManager(this)
{
PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8
};
void Draw(GameTime gameTime){
// GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.CornflowerBlue, 0, 0);
// TODO: Add your drawing code here
// Start drawing spriteBatch.Begin(transformMatrix:GameResolution.Instance.GetScaleMatrix()
,blendState: BlendState.AlphaBlend);
-----Draw other stuf ------
DrawStencil(…);
-----Draw other stuf ------
spriteBatch.End();
}
> public void DrawStencil(Vector2 start, Vector2 end,Texture2D texture,Rectangle source, Vector2 position)
{
this.SpriteBatch.End();
var m = Matrix.CreateOrthographicOffCenter(0, Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth, Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight, 0, 0, 1 );
var a = new AlphaTestEffect(Graphics.GraphicsDevice) { Projection = m };
var s1 = new DepthStencilState { StencilEnable = true, StencilFunction = CompareFunction.Always, StencilPass = StencilOperation.Replace, ReferenceStencil = 1, DepthBufferEnable = false, };
var s2 = new DepthStencilState { StencilEnable = true, StencilFunction = CompareFunction.Equal, StencilPass = StencilOperation.Keep, ReferenceStencil = 1, DepthBufferEnable = false, }; Graphics.GraphicsDevice.Clear(ClearOptions.Stencil, Color.Gray, 0, 0);
this.SpriteBatch.Begin(SpriteSortMode.Immediate, depthStencilState: s1, effect: a , transformMatrix: GameResolution.Instance.GetScaleMatrix()); //DrawRectangle(start, end, Color.White); this.SpriteBatch.Draw(pixel3x7, position, Color.White); //The mask this.SpriteBatch.End();
this.SpriteBatch.Begin(SpriteSortMode.Immediate, depthStencilState: s2, effect: a, transformMatrix: GameResolution.Instance.GetScaleMatrix()); this.SpriteBatch.Draw(texture, position, sourceRectangle: source, color: Color.White); //The background this.SpriteBatch.End();
//Graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Blue, 0, 0); this.SpriteBatch.Begin(transformMatrix: GameResolution.Instance.GetScaleMatrix(), blendState: BlendState.AlphaBlend);
}