Shading sprites based on sprite Y

Here you have the shader for Y-based fade for sprite in atlas.

Some remarks:

  1. the vertex shader needs some constants: translation, rotation (euler.z in radians) and scale (x/y).
  2. Of course this approach is problematic for batching…
  3. The main.script in the attached example DOES NOTHING to optimize the use of the shader: I call go.get_world_position / scale in update.
  4. I have not (extensively) tested the shader!!!

The idea for the shader is very (too) simple: give it the data needed to recompute the local xy coordinates from the world-coordinates. Only this!

sprite_shader.zip (909.8 KB)

4 Likes

That’s fair enough Mathias. Thanks you.

Thank you for sharing. Here is a modified version with more logos bouncing around with the directions randomized and changing over time.

sprite_shader.zip (90.2 KB)

I’ll try to adapt this to other situations soon. For some special effects, having extra draw calls is fine. They can be disabled / swapped to less expensive ones at runtime now anyway. I’ll do a test with dynamically setting material of a sprite to reduce draw calls when not using the special batch breaking special effects.

3 Likes

https://caniuse.com/webgl2

75.83% global usage

Seems it may be coming to iOS Safari and that’s a large %. The other large % is IE which doesn’t actually matter.

If you don’t use scaling / rotation then you may write a specialized version: you have a lighter shader and less data to pass. But I am sure this was already clear to you.

Ciao!

2 Likes

Until other options are available, I may provide 2 versions of sprite shaders one with scale/rotation and one without.

2 Likes