Sampling positions in GLSL

Hi!

I got this example where three samples with x and y positions is moving three balls painted in GLSL over the screen.

Shader1 example works fine. Im using a loop to iterate over the positions:

for(int i = 0; i < numSamples; i++) { drawCircle += circle(aPos[i], st, 0.01, 0.03, aspect); }

In Shader2 im trying to avoid the for loop by sampling a texture with the positions but I guess there is something I dont fully understand.

[code]
vec2 position = texture(sTD2DInputs[0], vUV.st).rg;

drawCircle += circle(position, st, 0.01, 0.03, aspect);[/code]

Is there a way to avoid the for loop? Im just experimenting but I dont fully understand whats going on I guess.

Can anyone help me explain a bit! Thanks!
glslExample.3.toe (4.61 KB)

The second way, although less efficient, would be

for(int i = 0; i < numSamples; i++) {
   vec2 position = texelFetch(sTD2DInputs[0], ivec2(i,0), 0).rg;
   drawCircle += circle(position, st, 0.01, 0.03, aspect);
}

One way of sampling fewer times would be to treat the array as a vec4, so that rg is one sample and ba is another sample. You’d call the circle function for the rg pair and then for the ba pair.

Thanks! So I guess I cannot manage without the for loop.

It works fine with fetching with the texelFetch
and after shuffling the samples I can get fewer loops as well. Thanks!