varying vec2 		texcoord;
				
uniform vec2		fresnel0,
					spec0;
uniform float 		scatterlevel;
uniform float 		scatterRed;
uniform float 		backScatter;
uniform float		wFar;

uniform float 		reflMul;
	
varying vec3		vertColor;

uniform float		matID;
	
#ifdef ALPHATEST

	uniform vec2		dscale0,
						dofs0;
	uniform vec4		dcolor0;
	uniform sampler2D 	dif0;
	uniform sampler2D 	noisemap;
	uniform float 		alphalevel;
	varying float		alphaFade;
	
#endif

#ifdef PSYSTEM

	varying float 	life;
	varying float	cosa,
			sina;
					
#else

	#ifdef BSET

		varying vec4 pnormal;
		varying vec4 pcolor;

	#else
	
		varying mat3	TBN;
		
	#endif
		
#endif				

varying vec4 		curpos,
					prev;
uniform float		vScale;
uniform float 		fade;

varying vec2 depth;

vec2 encode(vec3 n)
{
	n=normalize(n);
    float f = sqrt(8.0*n.z+8.0);
    return n.xy / f + 0.5;
}

void main()
{
	vec2 screenCoords=prev.xy;

	#ifdef PSYSTEM
	
	if(life<=0.0)
		discard;
			
	#endif

	float 	fdalpha;
	vec3	fnormal;
	vec2	fspec,
			ffresnel;
			
	#ifdef ALPHATEST

		fdalpha=texture2D(dif0,texcoord.st*dscale0+dofs0).a;
	
		#ifdef PSYSTEM
			float alpha=fdalpha*life;
			if(alpha<=alphalevel)
				discard;
		#else
			#ifdef BSET
				float alpha=texture2D(noisemap,screenCoords.st).r;
				if(alpha>pcolor.w)
					discard;
			#else
				#ifdef MSET
					float alpha=texture2D(noisemap,screenCoords.st*5.0).r;
					if(alpha>fade)
						discard;
				#else
					#ifdef _HEIGHTMAP_
					#else
						#ifdef SKINNING
						#else
							#ifndef _NOT_INSTANCED_
								float alpha=texture2D(noisemap,screenCoords.st*5.0).r;
								if(alpha>alphaFade)
									discard;
							#else
								float alpha=texture2D(noisemap,screenCoords.st*5.0).r;
								if(alpha>fade)
									discard;
							#endif
						#endif
					#endif
				#endif
			#endif
			
			#ifdef BSET
				if(fdalpha<=alphalevel)
					discard;
			#else
				if(fdalpha<=mix(alphalevel,alphalevel*0.8,clamp(curpos.z*25000.0-4500.0,0.0,1.0)))
					discard;
			#endif
		#endif
		
	#endif
			
	#ifdef BSET
	
	#else
		fnormal.xyz=vec3(0.0,0.0,1.0);
		//ffresnel.y=fresnel0.y*0.5;
	#endif

	gl_FragData[0]=vec4(vertColor,scatterlevel/*1.0*/);//fdcolor;

	#ifdef PSYSTEM
			
		gl_FragData[1].xy=encode(vec3((fnormal.x*cosa)+(fnormal.y*sina),(fnormal.x*sina)-(fnormal.y*cosa),fnormal.z));
			
	#else
		
		#ifdef BSET
			
			vec3 one=vec3(1.0,1.0,1.0);
			vec3 rmul=gl_FragData[0].rgb*pcolor.xyz;
			vec3 rscr=one-((one-pcolor.xyz)*(one-gl_FragData[0].rgb));
			gl_FragData[0].rgb=gl_FragData[0].rgb*rscr+(one-gl_FragData[0].rgb)*rmul;
			gl_FragData[1].xy = encode(pnormal.xyz);
			
		#else
		
			// normal
			gl_FragData[1].xy=encode(TBN*fnormal.xyz);
			
		#endif
			
	#endif
	
	gl_FragData[1].z=0.0;//reflMul*0.0625;///16.0;
	gl_FragData[1].a=backScatter;
	
	// pos
	vec2 vel=((curpos.xy/curpos.w)-(prev.xy/prev.w))*vScale;
	
	// material params
	gl_FragData[2].r=matID;//scatterlevel; 
	gl_FragData[2].g=0.0;//ffresnel.y*0.00390625; 
	gl_FragData[2].ba=(vel*vec2(0.5))+vec2(0.5);
}
