void main()

in showcases/wind/src/layers/particle-layer/transform-feedback-vertex.glsl.js [50:107]


void main(void) {
  // position in texture coords
  float x = (posFrom.x - bbox.x) / (bbox.y - bbox.x);
  float y = (posFrom.y - bbox.z) / (bbox.w - bbox.z);
  vec2 coord = vec2(x, 1. - y);
  vec4 texel1 = texture2D(dataFrom, coord);
  vec4 texel2 = texture2D(dataTo, coord);
  vec4 texel = mix(texel1, texel2, delta);

  // angle
  float angle = texel.x * PI4;
  float anglePast = posFrom.z;
  if (angle < 0.) {
    angle += PI * 2.;
  }
  if (anglePast > -1.) {
    if (angle > anglePast && abs(angle - anglePast) > abs(angle - (anglePast + PI * 2.))) {
      anglePast += PI * 2.;
    } else if (angle < anglePast && abs(anglePast - angle) > abs(anglePast - (angle + PI * 2.))) {
      angle += PI * 2.;
    }
    angle = angle * FACTOR + anglePast * (1. - FACTOR);
  }

  // wind speed
  float wind = 0.05 + 0.9 * (texel.y - bounds1.x) / (bounds1.y - bounds1.x);
  float windPast = posFrom.w;
  if (windPast > -1.) {
    wind = wind * FACTOR + windPast * (1. - FACTOR);
  }

  vec2 offset = vec2(cos(angle), sin(angle)) * wind * 0.2;
  vec2 offsetPos = posFrom.xy + offset;

  vec4 endPos = vec4(offsetPos, mod(angle, PI * 2.), wind);

  // if out of bounds then map to random position
  float r1 = rand(vec2(posFrom.x, offset.x + time));
  float r2 = rand(vec2(posFrom.y, offset.y + time));
  r1 = r1 * (bbox.y - bbox.x) + bbox.x;
  r2 = r2 * (bbox.w - bbox.z) + bbox.z;
  vec2 randValues = vec2(r1, r2);

  // endPos = vec4(offsetPos, randValues);
  endPos.xy = mix(offsetPos, randValues,
    float(offsetPos.x < bbox.x || offsetPos.x > bbox.y ||
      offsetPos.y < bbox.z || offsetPos.y > bbox.w));
  endPos.xy = mix(endPos.xy, randValues, float(length(offset) < EPSILON));
  endPos.xy = mix(endPos.xy, randValues, float(texel.x == 0. && texel.y == 0. && texel.z == 0.));
  if (flip > 0.) {
    if (abs(abs(fract(endPos.x)) - flip / 10.) < EPSILON) {
      endPos.xy = randValues;
    }
  }
  // endPos.xy = mix(endPos.xy, randValues, abs(flip - positions.z) <= DELTA ? 1. : 0.);

  gl_Position = endPos;
}