Simple Shader that translates textures drawn in SpriteBatch away from origin without scaling


As the title says, the effect I’m trying to achieve is to have my 2d sprites drawn through SpriteBatch to be moved away from the origin by some factor without scaling the sprites. I could achieve this by multiplying the positions passed into every draw to SpriteBatch, but I’d prefer to do this with a vertex shader, so all spritebatch draw calls will act properly without needing to edit them. Also I’d like to start learning HLSL beyond pixel shaders and felt like this was a good start towards that.

As far as I know the view matrix passed into spritebatch cannot achieve the effect that I want, because it will scale the sprites as well. I haven’t had much luck getting vertex shaders working and I’m now realizing the shader works on single vertices instead of the entire quad of a texture. Can I somehow pass in the texture’s actual position, so each corner of the texture translates uniformly?

Any help getting me going in the right direction would be greatly appreciated. Thanks!

I am sorry but this sounds like bad idea but I would have to know bit more details. But let´s start why this sounds like bad idea:
So I get it you have spritebatch with multiple draws. I will just kinda assume that you use deferred mode. If you will pass parameter to shader you can´t batch sprites together so you will have to use Immediate mode.

I dont know what view do you use? Orto? Perspective? How complex is your scene, what exactly are you trying to achieve? How does your parameter for vertex position suppose to look like? Is it offset in 2D space that should move origin of object, or is it multiplier?

Oh, didn’t realize you have to use immediate if you are passing things into the shader, if that’s the case then I definitely shouldn’t do this.

[quote=“Ravendarke, post:2, topic:2758”]
I dont know what view do you use? Orto? Perspective? How complex is your scene, what exactly are you trying to achieve?[/quote]
It’s an orthographic 2.5d scene (I’ve been using backtofront on the spritebatch begin call). Eventually the scene will have upwards of thousands of draws I’m guessing. The effect I am going for is to be able to change the visual distance between all objects. When this multiplier is high, things look further apart, but if it is less than 1, things look much closer.

Right now I just want it to offset via a multiplier.

You can set parameter but only once inside draw call, if you use deferred then everything that could be batched (same texture) will be one draw call and during that draw call parameter can´t be changed, so if you would want different position for every sprite it would be impossible.

Anyway as I said for you there is no reason not to change position of each sprite directly.

I still don´t know how exactly scene look like and what´s the issue but what about full perspective camera with billboard sprites and you will change fov accordingly (as right now it sounds like this is what you need) (-well to achieve exactly what you want you will need change FOV and Zposition at same time to offset size change of sprites, but that shouldnt be much of any issue)

1 Like