Skew and stretch textures dynamically

I have a game working with tiles, each representing a 32*32 texture, and a simple drawing system working with SpriteBatch
What I want to do is skew and stretch some of these tiles dynamically but I don’t know how.


I first thought of using matrices but it don’t seem to be the best way since I want to draw multiples tiles with different matrices. Maybe I need to make a custom vertex shader ? If I do, how ?

Thanks in advance !

For stretch this is relatively trivial since it’s a part of the arguments for SpriteBatch.Draw. Skew, however…

It depends on how you’re drawing. A matrix is a good approach since you can put all your transformations into the matrix and apply it. However, if you’re using SpriteBatch for all your drawing then I think the only way to apply a transform via a Matrix is through the Begin call. It’s been a while since I’ve played with this, but this might be challenging. I’m not sure if you can nest begin/end calls like this without involving another spritebatch. For individual sprite transformations, this might get a little tedious :frowning:

Another option is to abandon SpriteBatch all together and draw all your sprites using Graphics.DrawPrimitive (or w/e) on a textured quad. I think this has some additional benefits as well, but I believe you can apply the matrix transform to the quad points directly so that when you render out, you get the appropriate skew. This would probably be a much easier approach, but you can’t use SpriteBatch :smiley:

The last option is, as you pointed out, a shader. Though similar to the first option, a shader is passed in via the SpriteBatch.Begin call, so you might have some complications depending on how you’re batching up your draws. For the shader itself you can probably google the implementation, or just fake something, but you’ll want to make sure that the texture you’re drawing has enough empty space around the edges to hold the additional data. The bounds of the texture passed to the shader don’t change, so if your sprite is 32x32 pixels and you want to skew it to a resulting rectangle of 48x32 pixels (for example), you need to make sure that the texture that goes to the shader has enough room. This is for pixel shaders anyway… I’m not sure if you can use vertex shaders with a SpriteBatch, and if you’re not using a SpriteBatch then I’d just go with the geometry manipulation via matrix method above.

That’s all the ideas I have haha. Other folks might have some different or better ones :wink: Good luck!

I’ll try to use Graphics.DrawPrimitive, it seems to be the best option in my case.

Thanks a lot for your help ! :smiley: