Seeing the Wrap Texture When Using the Clamp Mode in MonoGame (Photos On)

I draw a tile map in the MonoGame project using the sprite command.

Here is the rendering code:

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, CameraManager.pInstance.pFinalTransform);

        foreach( TmxLayer Layer in mCurrentTileMap.Layers)
        {
            for (int y = 0; y < mCurrentTileMap.Height; y++)
            {
                for (int x = 0; x < mCurrentTileMap.Width; x++)
                {
                    TmxLayerTile Tile = Layer.Tiles[mCurrentTileMap.Width * y + x];
                    int TileID = -1;
                    Texture2D Texture = FindTileMapTextureForTileID(Tile.Gid, out TileID);
                    if (TileID > 0)
                    {
                        int atlasX = TileID % (Texture.Width / TileSize);
                        int atlasY = TileID / (Texture.Width / TileSize);

                        SpriteEffects FX = SpriteEffects.None;
                        if (Tile.HorizontalFlip)
                        {
                            FX = SpriteEffects.FlipHorizontally;
                        }

                        SpriteBatch.Draw(Texture, new Vector2(x * TileSize, y * TileSize), new Microsoft.Xna.Framework.Rectangle(atlasX * TileSize, atlasY * TileSize, TileSize, TileSize), Color.White, 0, Vector2.Zero, 1, FX, 0);
                    }
                }
            }
        }

Here is what I consider important lines of code:

SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, 
  null, null, null, CameraManager.pInstance.pFinalTransform);

SpriteBatch.Draw(Texture, new Vector2(x * TileSize, y * TileSize), 
  new Microsoft.Xna.Framework.Rectangle(atlasX * TileSize, atlasY * TileSize, TileSize, TileSize), 
  Color.White, 0, Vector2.Zero, 1, FX, 0);

As you can see, I use SamplerState.PointClamp. However, when I move around the world, I sometimes see this; below some fragments you can see one line of pixels, which are the pixels below in the sprite atlas.

Click to enlarge to see the problem better. Click to enlarge to clearly see the problem ...

, , , (CameraManager.pInstance.pFinalTransform), . , . , ( , 10 ), .

, ?

, .

enter image description here

+4
1

, , , (CameraManager.pInstance.pFinalTransform), .

, . , Tiled MonoGame.Extended , .

, RenderTarget2D, . , , .

, MonoGame.Extended TiledMap.

:

private readonly RenderTarget2D _renderTarget;

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.SetRenderTarget(_renderTarget); 

    GraphicsDevice.Clear(Color.Black);

    SpriteBatch.Begin(sortMode: SpriteSortMode.Deferred, samplerState: SamplerState.PointClamp);

    foreach( TmxLayer Layer in mCurrentTileMap.Layers)
    {
        for (int y = 0; y < mCurrentTileMap.Height; y++)
        {
            for (int x = 0; x < mCurrentTileMap.Width; x++)
            {
                TmxLayerTile Tile = Layer.Tiles[mCurrentTileMap.Width * y + x];
                int TileID = -1;
                Texture2D Texture = FindTileMapTextureForTileID(Tile.Gid, out TileID);
                if (TileID > 0)
                {
                    int atlasX = TileID % (Texture.Width / TileSize);
                    int atlasY = TileID / (Texture.Width / TileSize);

                    SpriteEffects FX = SpriteEffects.None;
                    if (Tile.HorizontalFlip)
                    {
                        FX = SpriteEffects.FlipHorizontally;
                    }

                    SpriteBatch.Draw(Texture, new Vector2(x * TileSize, y * TileSize), new Microsoft.Xna.Framework.Rectangle(atlasX * TileSize, atlasY * TileSize, TileSize, TileSize), Color.White, 0, Vector2.Zero, 1, FX, 0);
                }
            }
        }
    }

    SpriteBatch.End();

    GraphicsDevice.SetRenderTarget(null);

    SpriteBatch.Begin(sortMode: SpriteSortMode.Immediate, blendState: BlendState.NonPremultiplied,
        samplerState: SamplerState.PointClamp, transformMatrix: CameraManager.pInstance.pFinalTransform);
    SpriteBatch.Draw(_renderTarget, Vector2.Zero, Color.White);
    SpriteBatch.End();
}

, SpriteBatch.Begin - _renderTarget. _renderTarget - Draw. - :

_renderTarget = new RenderTarget2D(graphicsDevice, width*tileWidth, height*tileHeight);

, . , , .

+1

Source: https://habr.com/ru/post/1623030/


All Articles