Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Different Shaders on different sprites  (Read 2079 times)

0 Members and 1 Guest are viewing this topic.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Different Shaders on different sprites
« on: March 28, 2020, 12:16:28 am »
So, I have a game engine I'm working on and I want to have multiple shaders applied to some sprites. However, not all sprites have the same shaders being applied to them and I plan on having support for shaders that all drawables go through.

I understand that I have to chain it using render textures, however I am unsure how to position the individual sprites properly.

So it would go a little something like this

1) Sprite gets drawn to a small render texture with first applied shader
2) render texture gets drawn to a large render texture with second applied shader
Repeat 1 and 2 for all sprites
3) Large render texture is applied to screen with the global shader.

What I'm having an issue with is choosing the size of the small render texture (because it needs to fit a possibly rotated sprite) and keeping it positioned in the center, but drawn in the right position for the global texture.

If anybody has any insight, I would be greatly appreciative.
Thank you.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Different Shaders on different sprites
« Reply #1 on: March 28, 2020, 01:50:15 am »
I would say that the position of the sprite on the render texture is added to the "render sprite" position to give the final sprite position.

You can position a sprite using its centre by setting the origin to its local centre. Afterwards, all rotations will rotate around its centre and the centre of the sprite will be at the position to which you set the sprite.

So, if your sprite is 30x40, set its origin to 15,20 - its centre.
Then, if you want the top-left to be at 100,100 you can set its position (of its centre) to 115,120.
Your render texture only needs to be as big as the largest sprite rotated so pick the biggest one and double it (to cover some shader bleed as well) and then re-use it for every sprite (don't resize it). Assume that this 30x40 sprite is the biggest - render texture would be 60x80.
Set position of the sprite (the centre, remember?) on render texture to (30,40) - the render texture's centre.
Rotate (around centre) as necessary.
Draw sprite on render texture using shader.
"Render sprite" is the sprite you use to draw the render texture to the window...
Set the render sprite's origin to its centre - 30,40.
Now, set the render sprite's position to the position on the window you would have put the centre of the sprite!
Draw render texture using other shader.

It's more complicated if you can't, for some reason, position the sprite using its centre...  :(

Something you should know is that to apply a shader to an entire window, you need to draw to a full-sized render texture first and then draw that render texture to the window using that (post-processing?) shader.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Different Shaders on different sprites
« Reply #2 on: March 28, 2020, 02:08:32 am »
My engine is designed so the origin of any sprite can be changed fairly easily and is handles.
Thank you very much for the quick response, id have never thought of using the largest sprite. I have methods of obtaining the largest already but realised that the gpu bandwidth wouldnt allow me to repeatedly create render textures.
I also realized the fact that i needed to draw to a rendertexture to get global effects, I just really had issues with figuring out positioning.
This should work great,
Thank you again.