Shader - UVs based on whole atlas?

I’ve started fiddling with shaders, using a copy of the built-in sprite shader to mess with. It seems the UVs that get passed to the fragment program (var_texcoord0) are based on the whole atlas, not the individual sprite itself. This makes sense now that I’ve figured it out, but I guess it means if I want to mess with the UV coordinates or use them for things other than reading the texture I have to put each sprite in its own atlas? I don’t suppose there’s any way around that, is there?

Also, it would be nice if there was a note about this in the shader doc.

1 Like

Hi Ross!

You are correct, the UVs that gets passed to the materials/shaders are precalculated based on the current flipbook frame and image location in the texture atlas.

Could you describe what you want to accomplish, maybe there is some workaround? :slight_smile:


Hey, sorry wasn’t clear. I sort of answered my own question in that post, haha, and the workaround is to put each sprite in an atlas by itself.

However, I would like to know, is there any way to get the range of UVs that a sprite is using? Within the atlas? Right now I have this super simple progress-bar shader:

 varying mediump vec2 var_texcoord0;

 uniform lowp sampler2D DIFFUSE_TEXTURE;
 uniform lowp vec4 fill;
 uniform lowp vec4 fgtint;
 uniform lowp vec4 bgtint;

 void main()
 	vec4 outcolor = fgtint;
 	if ( var_texcoord0.x > fill.x ) outcolor = bgtint;
 	gl_FragColor = texture2D(DIFFUSE_TEXTURE, var_texcoord0) * outcolor;

Is there any way to make this work with a sprite in an atlas with other sprites? I guess to get the min and max values of var_texcoord0.x for that sprite, so I can remap them to 0-1?

I realize that the answer to my question is probably: “No”, and I can live with that. :slight_smile:

There is a workaround you can do to get the “id” of each vertex, and thus implement your own uv coords.
I posted an example here which adds some shader constants to a sprite/shader, in particular, the center point of the sprite.
With it you can determine the id like this.


Oh, nice! That would do it. I will play with this, thanks!

1 Like