Texture as data in shader has odd behavior

The same issue is present in Processing:

color.frag

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform sampler2D tex;

void main() {
  float bri = 0.0;
  float maxBri = 0.0009;
  vec2 pos = gl_FragCoord.xy / 300.0;
  for(int i=0; i<50; i++) {
    vec2 data = texture2D(tex, vec2(float(i) / 50.0, 0.0)).rg;
    bri += maxBri / distance(pos, data);
  }
  gl_FragColor = vec4(vec3(bri), 1.0);
}

sketch.pde

PShader fx;
PImage img;
int NUM_PARTICLES = 50;

void settings() {
  size(300, 300, P2D);
}
void setup() {
  fx = loadShader("color.frag");
  shader(fx);
  img = createImage(NUM_PARTICLES, 1, ARGB);
  noStroke();
}

void draw() {
  img.loadPixels();
  img.pixels[frameCount % NUM_PARTICLES] = color(
    255.0 * mouseX / width, 
    255.0 * (height-mouseY) / height, 
    0, 
    255);
  img.updatePixels();
  fx.set("tex", img);
  rect(0, 0, width, height);
}

Very interesting… a particle with personality… :slight_smile: