updateSprites = regl({
vert: `
precision mediump float;
attribute vec2 position;
void main () {
gl_Position = vec4(position, 0, 1);
}
`,
frag: `
precision highp float;
uniform sampler2D state;
uniform float shapeX, shapeY, deltaT, gravity;
uniform float tick;
uniform float friction;
uniform float noiseScale;
uniform float balance;
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Gradient Noise by Inigo Quilez - iq/2013
// https://www.shadertoy.com/view/XdXGW8
float noise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
vec2 u = f*f*(3.0-2.0*f);
return mix( mix( dot( random2(i + vec2(0.0,0.0) ), f - vec2(0.0,0.0) ),
dot( random2(i + vec2(1.0,0.0) ), f - vec2(1.0,0.0) ), u.x),
mix( dot( random2(i + vec2(0.0,1.0) ), f - vec2(0.0,1.0) ),
dot( random2(i + vec2(1.0,1.0) ), f - vec2(1.0,1.0) ), u.x), u.y);
}
void main () {
vec2 shape = vec2(shapeX, shapeY);
vec4 prevState = texture2D(state,
gl_FragCoord.xy / shape);
vec2 position = prevState.xy;
vec2 velocity = prevState.zw;
position += 0.5 * velocity * deltaT;
// if (position.x < -1.0 || position.x > 1.0) {
// velocity.x *= -1.0;
// }
// if (position.y < -1.0 || position.y > 1.0) {
// velocity.y *= -1.0;
// }
position += 0.5 * velocity * deltaT;
vec2 force_noise = vec2(
noise(position * noiseScale),
noise(position * noiseScale)
);
vec2 force_radial = vec2(
position
//////////////////////////////////////////////////////////////////////////////////
);
// velocity.y = velocity.y + gravity * deltaT;
velocity *= (1. - friction);
vec2 force = mix(force_noise, force_radial, balance);
velocity += force * 3.;
gl_FragColor = vec4(position, velocity);
}
`,
depth: {enable: false},
framebuffer: ({tick}) => vars.SPRITES[(tick + 1) % 2],
uniforms: {
state: ({tick}) => vars.SPRITES[(tick) % 2],
shapeX: regl.context('viewportWidth'),
shapeY: regl.context('viewportHeight'),
deltaT: 0.0005,
gravity: -0.05,
tick: regl.context('tick'),
friction,
noiseScale,
balance,
},
attributes: {
position: [
0, -4,
4, 4,
-4, 4
]
},
primitive: 'triangles',
elements: null,
offset: 0,
count: 3
})