0
\$\begingroup\$

I'm trying to create a shader that creates a displacement on an object, slicing horizontally the sprite.

These is an example of the atlas I'm using and the effect I'm trying to achieve:

I tried to achieve this creating a fragment shader, that would read the Y coordinate of the texcoord, and depending on the height, displace it horizontally.

But I found two main problems:

1) If my atlas had more rows of sprites, I can't really tell at what height I'm drawing the sprite in the fragment shader. Since values of texcoord go from 0 to 1 relative to the atlas being use, the more rows I have, the smaller values should I choose to create the horizontal cut. Is there any way to know the position inside the drawn?

2) The problem is similar to the previous point. When displacing the pixels horizontally, if the displacement is too big, I end up sampling the sprite next to the one being rendered. The idea is for it to loop back the the begining of the sprite. If I had the width of the sprite, I could tell how to do so, but using texcoord alone is just not posible. How can I do this?

\$\endgroup\$
1
\$\begingroup\$

Easiest way to do this is to not use a packed sprite and instead use a plain unpacked texture so you can wrap-around using the texture samplers directly.


That said, UV displacement in the pixel shader (called a "dependent texture read") is costly on any GPU, it's best to avoid it when you can especially on mobile.

Slicing and sliding the sprite on the CPU by generating geometry could be much faster than having the GPU do it if those sliced sprites cover a large part of the screen.

A 1Ghz mobile CPU can generate 3 "quads" (3x 2 triangles) of geometry to avoid a displacement shader much faster than a 500Mhz mobile (or integrated) GPU will spend having to process all those pixels causing dependent reads in a displacement shader.

\$\endgroup\$
1
  • \$\begingroup\$ Using separated sprites was my first thought, but I felt like it was kind of a inefficient and cheap solution. But I guess its valid as well. \$\endgroup\$ – Leo Nov 19 '16 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.