attribute vec3 va_pos0, va_explodeVelocity, va_displacement;
attribute float va_radius, va_delay;

uniform float twist, time, flip, jump;
uniform vec3 color, flipColor;

varying vec3 spriteCenterVS, viewPos;
varying float radius;

void main(void) {

	float a = twist * (va_pos0.y*va_pos0.y);
	float ca = cos(a), sa=sin(a);
	
	vec3 v = va_explodeVelocity;
	
	float t=max(jump, 0.0) * 10.0;
	
	vec3 pos =va_pos0+ v*t - vec3(0.0, 9.82 * t * t / 2.0 , 0.0);
	pos.y = max(pos.y, 0.0);

	pos = vec3(ca * pos.x + sa * pos.z, pos.y, -sa * pos.x + ca * pos.z);

	vec3 diffX = pos - vec3(pos.x,             sin(time*1.1)*2.0,  cos(time*0.7)*2.0);
	vec3 diffY = pos - vec3(sin(time*0.4)*2.0, pos.y,              cos(time*0.9)*2.0);
	vec3 diffZ = pos - vec3(sin(time*0.5)*2.0, cos(time*1.3)*2.0,  pos.z            );
	
	float lenX = length(diffX);
	float lenY = length(diffY);
	float lenZ = length(diffZ);
	
	float r = va_radius * mix(1.0, min(lenX*lenY*lenZ, 1.0), flip);

	vec3 spriteCenterWS = pos + flip * (diffX * (0.5/lenX / (0.6+lenX*lenX)) +  diffY * (0.5 / lenY / (0.6+lenY*lenY)) +  diffZ * (0.5 / lenZ / (0.6 + lenZ*lenZ)));

	vec3 posOS = spriteCenterWS + va_displacement * r;

	viewPos         = (gl_ModelViewMatrix * vec4(posOS,          1.0)).xyz;
	spriteCenterVS  = (gl_ModelViewMatrix * vec4(spriteCenterWS, 1.0)).xyz;
	radius = r;
	
	gl_Position   = gl_ModelViewProjectionMatrix * vec4(posOS, 1.0f);
	gl_FrontColor = vec4(color + flipColor * (flip * (1.0/(1.0+lenX*lenX*5.0)+1.0/(1.0+lenY*lenY*5.0)+1.0/(1.0+lenZ*lenZ*5.0))), 0.0);
}
