attribute vec3 position;
uniform mat4 pmat;
uniform mediump int layer;

varying mediump vec2 xy;

void main() {
	gl_Position = pmat * vec4(1000.0 * position.x, 1000.0 * position.y, -5.0 * float(layer), 1.0);
	xy = position.xy;
}
uniform mediump int layer;
uniform mediump int nframe;

varying mediump vec2 xy;

mediump float rand(mediump vec2 seed) {
	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main() {
	mediump float scale = 0.01;
	mediump float dist = pow(xy.x, 2.0) + pow(xy.y, 2.0);

	bool x = mod(xy.x, scale) < .5 * scale;
	bool y = mod(xy.y, scale) < .5 * scale;

	if((x && y) || (!x && !y)) {
		discard;
	}


	mediump vec2 sq = vec2(
		floor(2.0 * xy.x / scale),
		floor(2.0 * xy.y / scale) + 2.0 * floor(float((nframe / 120)))
	);

	mediump float d = floor(10.0 * rand(sq));

	if(float(layer) < d) {
		discard;
	}

	mediump float b = .2 * rand(sq + vec2(920.0, 920.0));
	mediump float g = .2 * rand(sq + vec2(240.0, 240.0));
	mediump float opacity = exp(-100.0 * dist*dist) * exp(-.05 * pow(float(layer), 2.0));

	gl_FragColor = vec4(
		vec3(.2 + b, .5 + g, 1.0), opacity
	);
}