//Particle shader without atmosphere/lightcolor calculations
//designed to be lightweight and just do the basics like billboarding

#include "../common/constants.cg"

// App to vertex
struct a2v
{
  float3 position     : POSITION;
  float4 color        : COLOR;      //optional
  float2 texcoords    : TEXCOORD0;
  float2 size         : TEXCOORD1;
};

// Vertex to fragment shader
struct v2p
{
  float4 position     : POSITION;
  float3 uv		      : TEXCOORD0;    // UV and logZ
  float4 color		  : COLOR;
};

v2p main
(
  // IN
  a2v IN,
  
  // Constants
  uniform float4x4 modelView,
  uniform float4x4 modelViewProj
)
{
	v2p OUT;
	
	// for every quad particle we get 4x the same IN.position.
	// we use texcoords to offset the position
	float4 outpos = float4(IN.position, 1);
	
    // Method 2: more performant collective billboarding
	// Get a right and up vector to billboard
    float3 vRight =	modelView._m00_m01_m02;
    float3 vUp 	  = modelView._m10_m11_m12;
	// Create offsets from particle position; only to be scaled by the size
	// outpos.xyz += vRight*sign + vUp*sign
	float3 vertexOffset = vRight * ((IN.texcoords.x*2)-1)*IN.size.x + vUp * ((IN.texcoords.y*2)-1)*IN.size.y;
	
  outpos.xyz += vertexOffset;
	OUT.position = mul(modelViewProj, outpos);
  //OUT.position.z=LogZ(OUT.position,zFar);
  OUT.uv.z=LogZ2(OUT.position,zFar,OUT.position);
	OUT.uv.xy=IN.texcoords.xy;
	OUT.color=IN.color;
  
  return OUT;
}
