I always have trouble with this and, unfortunately, I like working with instancing in this way.

I’ve seen many different formulas, such as:

This sometimes works for me in pixel shaders, although not all the time.

ivec2 outRes = ivec2(uTD2DInfos[0].res.zw);

float ratio = uTD2DInfos[0].res.z/uTD2DInfos[0].res.w;

vec2 coords = vec2(vUV.s, vUV.t*(1.0/ratio));

vec2 wh = uTD2DInfos[0].res.zw;

vec2 coord = vec2(floor(gl_FragCoord.x),floor(gl_FragCoord.y));

return int(coord.x + (coord.y * wh.x));

// returns a normalized position using uniform world bounds

vec3 worldSize = uWorldBoundsMax - uWorldBoundsMin;

vec3 normPos = (p.position-uWorldBoundsMin) / worldSize;

return normPos;

This in the vertex shader,.

vec2 pixSize = vec2(1/uRes.x,1/uRes.y);

int vInstanceID = TDInstanceID();

int vVertex = gl_VertexID;

int currParticle = vVertex + uParticles*vInstanceID;

vec2 currPixel = vec2(0.0);

currPixel.x = (currParticle)%int(uRes.x)/uRes.x+pixSize.x/2;

currPixel.y = floor((currParticle)/uRes.x)/uRes.y+pixSize.y/2;

Also this one in Compute shader

int id = int(gl_GlobalInvocationID.x);

ivec2 inCoord = ivec2(id%inRes.x,id/inRes.x);

Although sometimes I can’t get any of these formulas work. So it’d be great to get some light shed on it since its difficult to find elsewhere.

To get more specific I guess I’m confused about the difference between Index and normalized coordinates and when you would use each and how they could relate to aspect ratio when instancing.