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;
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;
velocity.y = velocity.y + gravity * deltaT;
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.01,
gravity: -0.05
},
attributes: {
position: [
0, -4,
4, 4,
-4, 4
]
},
primitive: 'triangles',
elements: null,
offset: 0,
count: 3
})