// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

#include "shaders/vertexVersionSimple.h"

precision highp float;

#include "shaders/uniformWorldConstants.h"
#include "shaders/uniformWeatherConstants.h"
#include "shaders/uniformPerFrameConstants.h"

attribute mediump vec4 POSITION;
attribute vec2 TEXCOORD_0;
attribute vec4 COLOR;

varying vec2 uv;
varying vec4 color;
varying vec4 worldPosition;
varying vec4 fogColor;

void main()
{
	float spriteSelector = COLOR.x*255.0;
	uv = UV_INFO.xy + (TEXCOORD_0 * UV_INFO.zw);
#ifndef NO_VARIETY
	uv.x += spriteSelector * UV_INFO.z;
#endif

	vec3 pos = POSITION.xyz;
		
	// subtract the offset then fmod into (0.0f, PARTICLE_BOX)
	pos.xyz += POSITION_OFFSET.xyz;
	pos.xyz = mod(pos.xyz, PARTICLE_BOX.xyz);
	
	// centre box on origin
	pos.xyz -= PARTICLE_BOX.xyz * 0.5;
	
	// push along view vector so box is positioned more infront of camera
	pos.xyz += FORWARD.xyz;
	
	// get world position
	vec4 worldPositionBottom = vec4(pos.xyz, 1.0);
	vec4 worldPositionTop = vec4(worldPositionBottom.xyz + (VELOCITY.xyz * SIZE_SCALE.y), 1.0);

	// get projected positions of top and bottom of particle, and top of particle in previous frame
	vec4 bottom = WORLDVIEWPROJ * worldPositionBottom;
	vec4 top = WORLDVIEWPROJ * worldPositionTop;
	
	// get 2d vector in screenspace between top and bottom of particle
	vec2 dir = normalize((top.xy/top.w) - (bottom.xy/bottom.w));
	
	// get 2d vector perpendicular to velocity
	vec2 dirPerp = normalize(vec2(-dir.y, dir.x));

	// choose either the top or bottom projected position using uv.y
	gl_Position = mix(top, bottom, TEXCOORD_0.y);
	 	
	// offset the position of each side of the particle using uv.x 
	gl_Position.xy += (0.5 - TEXCOORD_0.x) * dirPerp * SIZE_SCALE.x;
	
	color = ALPHA.rgba;

#if defined(COMFORT_MODE) && defined(VR_MODE)
	if (gl_Position.z < 2.0) {
		color.a = clamp((gl_Position.z - 1.2)/0.8, 0.0, 1.0);
	}
#endif

	//fog
    fogColor.rgb = FOG_COLOR.rgb;
	fogColor.a = clamp(((bottom.z / RENDER_DISTANCE) - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 1.0);

	worldPositionBottom.xz += VIEW_POSITION.xz;
	worldPositionBottom.xz *= 1.0 / 64.0;	// Scale by 1/TextureDimensions to get values between
	worldPositionBottom.xz += 0.5;			// Offset so that center of view is in the center of occlusion texture
	worldPositionBottom.y += VIEW_POSITION.y - 0.5;
	worldPositionBottom.y *= 1.0/255.0;
	worldPosition = worldPositionBottom;
}
