Transparent pixels dont render correctly in tiles

Hi,

So currently i am rendering a sprite behind a tilemap layer using the depth buffer(as suggested here).

The problem i am facing is that the tiles that should have transparent pixels are not rendered correctly when another sprite is drawn behind it.
Here is an example:
(The statues and rock pile are all part of the same tilemap layer. When drawing loose sprites with the depth buffer it works fine)
image

And this is the code that is used to draw the layer and sprite:

 _spriteBatch.Begin(samplerState: SamplerState.PointClamp, depthStencilState: DepthStencilState.Default, transformMatrix: Camera.GetViewMatrix());
            _motwSprite.Depth = 0.3f;
            _spriteBatch.Draw(_motwSprite, _motwPosition);
            foreach (TiledMapTileLayer layer in tiledMap.TileLayers)
            {
                if (layer.Name == "Layer 2")
                {
                    tiledMapRenderer.Draw(layer, viewMatrix: Camera.GetViewMatrix(), null, null, 0.2f);
                }
            }     
 _spriteBatch.End();

I know painters algorithm is also an option, but for my use case i want to provide the depth value dynamically so the players depth is based on Y value.

I read all over the monogame forums but cant seem to find a solution to this problem, any help is greatly appreciated.

-Thanks

Thanks to Might in the discord server i found the solution.

  1. You need to provide an alpha test effect to the spritebatch that has your stencilstate defined
var alphaTest = new AlphaTestEffect(GraphicsDevice)
            {
                Projection = transformMatrix *
                         Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height,
                             0, 0, -1),
                VertexColorEnabled = true,
                ReferenceAlpha = 1,
            };
  1. The sprite has to be drawn in a separate spritebatch. Keep in mind that the order of the spritebatch calls matters, so in my case the batch with the sprite has to end before the batch with the layer is called