#include "vertexVersionSimple.h"

precision highp float;

#include "uniformWorldConstants.h"
#include "uniformWeatherConstants.h"
#include "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;

pos.xyz += POSITION_OFFSET.xyz;
pos.xyz = mod(pos.xyz, PARTICLE_BOX.xyz);
pos.xyz -= PARTICLE_BOX.xyz * 0.5;
pos.xyz += FORWARD.xyz;

vec4 worldPositionBottom = vec4(pos.xyz, 1.0);
vec4 worldPositionTop = vec4(worldPositionBottom.xyz + (VELOCITY.xyz * SIZE_SCALE.y), 1.0);

vec4 bottom = WORLDVIEWPROJ * worldPositionBottom;
vec4 top = WORLDVIEWPROJ * worldPositionTop;

vec2 dir = normalize((top.xy/top.w) - (bottom.xy/bottom.w));

vec2 dirPerp = normalize(vec2(-dir.y, dir.x));

//	gl_Position = mix(top, bottom, TEXCOORD_0.y);
//	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;
}
