SamplerState.LinearWrap not working as expected

I’m using the current release of monogame (3.4.0) and I experience an unexpected behaviour trying to tile sprites using the SpriteBatch. I’m following the original XNA-tutorial and expect that my texture is repeated like shown on the linked page. However, this is not the case:

(Click to enlarge)

Here’s the drawing code I’m using:

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone);

spriteBatch.Draw(texture, new Rectangle(0, 0, 600, 600), Color.White);

spriteBatch.End();

The rest of the code is equivalent to the project template. The texture is loaded in LoadContent and is the following file:

Am I wrong believing that the texture shouldn’t appear stretched? How can I fix that?

This happens both with the Windows and the OpenGL template.

Well, it works well (it’s linear).
To get the tiling effect you should draw your texture with this function:
spriteBatch.Draw(spriteTexture, Vector2.Zero, destRect, color, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
as it’s in the tutorial you’re using.

Source rectangle is what makes it tileable

A rectangle that specifies (in texels) the source texels from a texture. Use
null to draw the entire texture.

So if your source rectangle is bigger than the size of your texture, you get the tiling effect.

1 Like

That’s some weird behaviour, imo … How can I combine this with tilesets? If I have hundreds of different tiles on one texture, I’d be using the source rectangle to select one specific tile from the tileset, but I can’t do that when I need the source rectangle to be bigger than the texture.

Texture wrapping generally doesn’t work with tilesets (although you can with some shader based wrapping). Usually you just render the tile multiple times to get the effect of wrapping.