I don't think using LinearWrap
will work in this case, the reason being that LinearWrap
takes place automatically when the texture coordinates fall beyond the edges of the texture, but you're using a source rectangle that falls entirely within the texture, so it's not even doing anything.
I can think of three solutions (in the order I'd probably choose):
- Create a new
Texture2D
and copy only that portion of the texture into the new one, or do this offline in your image editor. Then draw it with a source rectangle that has the same size as the destination rectangle. Since this source rectangle will be larger than the texture, wrapping will take place. And since they both have the same size, there won't be any stretching.
To be more specific what I mean is something like:
Rectangle windowTopBorderRect = /* wherever you want it to draw */
Rectangle sourceTextureRegionRect = new Rectangle(0, 0, windowTopBorderRect.Width, windowTopBorderRect.Height);
You could also use the position
parameter instead of the destinationRectangle
and let the sourceRectangle
determine the size.
Alternatively do the tiling manually with multiple
SpriteBatch.Draw
calls. Basically calculate how many times you need to draw the texture to fill that space, and draw them in the loop. In the case of an horizontal strip like your example, the last draw call will probably need a source rectangle, to crop only the remaining portion that fits that space.You could also probably draw using a special pixel shader effect that calculates a new texture coordinate that wraps around the original source rectangle without stretching, but this might be tricky.
All in all I suggest the first option, move your tileable sprites outside of the spritesheet (at load time, or offline) into their separate textures, so that you can use LinearWrap
and save a bunch of draw calls or having to use a special effect.