#ifdef _VERSION_CHECK_
	#ifndef MSET

		#ifndef _NOT_INSTANCED_
		
			#extension GL_EXT_gpu_shader4 : enable         
			
		#endif
		
	#endif
#endif


// vertex attributes
attribute vec3 			s2_vertex;
attribute vec3			s2_normal;
attribute vec4 			s2_color;
attribute vec4 			s2_secondaryColor;
attribute vec4			s2_texcoords; 
attribute vec4			s2_binormal; 

	uniform sampler2D 	dif0;
	varying vec3		vertColor;
	uniform vec4		dcolor0;
	uniform vec2		dscale0;
	uniform vec2		dofs0;
	
#ifdef ALPHATEST

	#ifdef BSET
		uniform float 		fade;
	#endif
	varying float		alphaFade;

	#ifdef VEGETATION
		uniform float 	windStrenght;
		uniform vec3 	windVec;
		uniform float	time;
		uniform vec2 	meshbox;
		uniform vec2	bendMul;
		float 			freq=time;
		
		#ifndef BSET
			#ifndef MSET
				uniform float	hueVariation;
				vec3 vgColor;
			#endif
		#endif
	#endif
				
#endif


	#ifdef SKINNING
	
		uniform samplerBuffer 	boneMatrices;
		uniform samplerBuffer 	prevBoneMatrices;

		varying mat3	TBN;
		
		#ifdef BONES_8
			attribute vec4			S2_texcoords2_Array;
			attribute vec4			S2_texcoords3_Array;
		#endif
						
	#else
	
		#ifdef PSYSTEM

			uniform	vec4 	pparams[100]; 
			uniform vec4	prevPParams[100];
			
			vec4			tpos;
			uniform vec4	data;

			varying float 	life;

			uniform vec2	tofs;			// cambiare in uniform
			uniform vec2	tscale;
			varying float	cosa,
							sina;
			uniform vec4 	particleScale; //particleScale.w start rotation
	
		#else

			#ifdef BSET

				uniform 		samplerBuffer 	pparams;
				vec4			tpos;
				uniform vec4	data;
				varying vec4	pnormal;
				varying vec4 	pcolor;
				
				uniform vec3	campos;
				
			#else
	
				#ifdef MSET

					uniform	mat4 	pparams[60];
					
					vec4		tpos;
				
				#else
				
					#ifndef _NOT_INSTANCED_
					
						uniform samplerBuffer 		WorldPoses;
						//uniform samplerBuffer 		prevWorldPoses;
					
					#endif
					
				#endif
		
				varying mat3	TBN;

			#endif

		#endif
						
	#endif

varying vec2 			texcoord;
					
vec4 					outNormal,
						outTangent,
						outBinormal; 

varying vec4 	curpos;/*,		
				prev;
uniform mat4	prevMVM,*/
uniform mat4	prevWTM;
uniform vec2	NF;

vec2 ComputeFrame(float dt, vec2 nf)
{
	vec2 ofs=vec2(1.0,1.0)/nf;
	float frame=floor(dt*nf.x*nf.y);
	float row=floor(frame/nf.x);
	float col=floor(frame-nf.x*row);
	
	return vec2(ofs.x*col, 1.0-(ofs.y*row)-ofs.y);
}

vec4 SmoothCurve( vec4 x ) {
  return x * x *( 3.0 - 2.0 * x );
}

vec4 TriangleWave( vec4 x ) {
  return abs( fract( x + 0.5 ) * 2.0 - 1.0 );
}

vec4 SmoothTriangleWave( vec4 x ) {
  return SmoothCurve( TriangleWave( x ) );
}

vec2 XformWind(vec2 wind, mat4 TM)
{
	mat2 TM2;
	TM2[0]=vec2(TM[0].x,TM[2].x);
	TM2[1]=vec2(TM[0].z,TM[2].z);
	return TM2*wind;
}

void main()
{	
	float occlusion=1.0;
	vec4 normal4      	= vec4( s2_normal, 0.0 );
	vec4 binormal4      = vec4( s2_binormal.xyz, 0.0 );
	vec4 tangent4      	= vec4(cross(binormal4.xyz,normal4.xyz),0.0);
	binormal4.xyz*=s2_binormal.w;
	#ifdef SKINNING

		vec4 index  		= s2_secondaryColor;
		vec4 weight 		= s2_color;
		vec4 position   	= vec4( 0.0, 0.0, 0.0, 0.0 );
		vec4 vertex			= vec4(s2_vertex,1.0);
		vec4 tempNormal  	= vec4( 0.0, 0.0, 0.0, 0.0 );
		vec4 tempTangent  	= vec4( 0.0, 0.0, 0.0, 0.0 );
		vec4 tempBinormal  	= vec4( 0.0, 0.0, 0.0, 0.0 );
		vec4 prevpos 		= vec4( 0.0, 0.0, 0.0, 0.0 );
		
		mat4 TM=mat4(	0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,1.0);
		/*mat4 pTM=mat4(	0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,0.0,
						0.0,0.0,0.0,1.0);
*/
		int j;
		for( int i = 0; i < 4; i++ )
		{
			// Apply influence of bone i
			j=int(index[i]*4);
			TM[0].xyz=texelFetchBuffer(boneMatrices,j).xyz;
			TM[1].xyz=texelFetchBuffer(boneMatrices,j+1).xyz;
			TM[2].xyz=texelFetchBuffer(boneMatrices,j+2).xyz;
			TM[3].xyz=texelFetchBuffer(boneMatrices,j+3).xyz;
			
			position = position + (TM * vertex) *weight[i];
			
			// Transform normals by bone i
			TM[3]=vec4(0,0,0,1);
			tempNormal = tempNormal + (TM * normal4)*weight[i];
			tempBinormal = tempBinormal + (TM * binormal4)*weight[i];
			tempTangent = tempTangent + (TM * tangent4)*weight[i];
		}
		
		#ifdef BONES_8
			index 	= S2_texcoords3_Array;
			weight 	= S2_texcoords2_Array;
			for( int i = 0; i < 4; i++ )
			{
				// Apply influence of bone i
				j=int(index[i]*4);
				TM[0].xyz=texelFetchBuffer(boneMatrices,j).xyz;
				TM[1].xyz=texelFetchBuffer(boneMatrices,j+1).xyz;
				TM[2].xyz=texelFetchBuffer(boneMatrices,j+2).xyz;
				TM[3].xyz=texelFetchBuffer(boneMatrices,j+3).xyz;
				
				position = position + (TM * vertex) *weight[i];
				
				// Transform normals by bone i
				TM[3]=vec4(0,0,0,1);
				tempNormal = tempNormal + (TM * normal4)*weight[i];
				tempBinormal = tempBinormal + (TM * binormal4)*weight[i];
				tempTangent = tempTangent + (TM * tangent4)*weight[i];
			}
		#endif
		
		gl_Position = gl_ModelViewProjectionMatrix * position;
		//prevpos=prevMVM*prevpos;
		//prev = gl_ProjectionMatrix*prevpos;	
		//pos = (gl_ModelViewMatrix * position);

		normal4=tempNormal;
		tangent4=tempTangent;
		binormal4=tempBinormal;
		
		texcoord = s2_texcoords.st;
		curpos=gl_Position;	
		
		outNormal=gl_ModelViewMatrix*normal4;
		outTangent=gl_ModelViewMatrix*tangent4;
		outBinormal=gl_ModelViewMatrix*binormal4; 

		TBN[0]=normalize(outTangent.xyz);
		TBN[1]=normalize(outBinormal.xyz);
		TBN[2]=normalize(outNormal.xyz);
		
	#else

		#ifdef PSYSTEM

			vec4 index=s2_color;
			vec4 ppos=pparams[int(index.x)];
			life=clamp(ppos.w,0.0,1.0);
			float scale=data.x + (data.y*(1.0-life));
			scale=clamp(scale,0.0,abs(scale))*0.5;
			scale+=scale*index.y;	
			ppos.w=1.0;

			float angle=(index.y*6.28+particleScale.w)+(data.z*(1.0-life)*6.28);

			cosa=cos(angle);
			sina=sin(angle);

			tpos=gl_ModelViewMatrix * ppos;	
			
			vec3 tv=s2_vertex;
			tv.x=(s2_vertex.x*cosa)-(s2_vertex.y*sina);
			tv.y=(s2_vertex.x*sina)+(s2_vertex.y*cosa);
			tpos.xyz=(tv.xyz*particleScale.xyz*scale + tpos.xyz);

			gl_Position=gl_ProjectionMatrix*tpos;
			texcoord = s2_texcoords.st*tscale;

			curpos=gl_Position;		
			//pos=tpos;
			
			/*ppos=prevPParams[int(index.x)];
			scale=data.x + (data.y*(1.0-life));
			scale=clamp(scale,0.0,abs(scale))*0.5;
			scale+=scale*index.y;	
			ppos.w=1.0;
*/
			//tpos=prevMVM*ppos;	
			//tpos.xyz=(tv.xyz*scale + tpos.xyz);
			//prev=gl_ProjectionMatrix*tpos;
			
		#else

			texcoord = s2_texcoords.st;		

			#ifdef BSET

				vec4 index=s2_color;
				int i=int(index.x)*3;
				vec4 ppos;
				ppos=texelFetchBuffer(pparams,i);
				pcolor=texelFetchBuffer(pparams,i+1);	
				
				////////////////////////////////////////////
				float bend=pcolor.w*s2_vertex.y;
				vec4 newpos=vec4(s2_vertex,1.0);
				#ifdef VEGETATION
					float vlen=length(newpos.xyz);
					float phase=dot(ppos.xyz,vec3(1.0,1.0,1.0));
					newpos.x+=(sin( (freq*5.2)+phase)*0.4 + cos((freq*8.5)+2.0*phase)*0.2 )*bend*bendMul.x*windStrenght;
					newpos.xyz=normalize(newpos.xyz)*vlen;
				#endif
				//////////////////////////////////////
				
				pcolor.w=ppos.w;
				ppos.w=1.0;
				
				normal4=texelFetchBuffer(pparams,i+2);
				float scale=abs(normal4.w)*0.5*index.y;
				
				mat3 PTM;
				PTM[2]=normalize(ppos.xyz-campos);
				PTM[0]=normalize(cross(vec3(0.0,1.0,0.0),PTM[2]));
				PTM[1]=cross(PTM[2],PTM[0]);
				
				vec4 rpos=newpos*scale;
				rpos.xyz=(PTM*rpos.xyz) + ppos.xyz;
				rpos.w=1.0;
				
				tpos=gl_ModelViewMatrix*rpos;
				gl_Position=gl_ProjectionMatrix*tpos;	
				curpos=gl_Position;	
				pos=tpos;
				
				// compute prev pos
				//tpos=prevMVM*rpos;
				//prev=gl_ProjectionMatrix*tpos;
				
				PTM=mat3(gl_ModelViewMatrix[0].xyz,gl_ModelViewMatrix[1].xyz,gl_ModelViewMatrix[2].xyz);
				pnormal.xyz=PTM * normal4.xyz;
			//	texcoord = s2_texcoords.st;	
		
			#else
			
				#ifdef MSET
					
					vec4 index=s2_color;
					mat4 mtx=pparams[int(index.w)];
					float bendScale=mtx[3].w;
					mtx[3].w=1.0;
					vec4 newpos=vec4(s2_vertex,1.0);
					
					#ifdef VEGETATION
						////////////////////////////////////////////
						//vec2 dBend=s2_color.gb*windStrenght*bendMul.y;
						float phase=dot(mtx[3].xyz,vec3(1.0,1.0,1.0));
						/*float phase2=phase+s2_color.r;
						float phase3=phase2+dot(s2_vertex,vec3(1.0,1.0,1.0));
						phase2*=3.14;
						phase3*=3.14;*/
						phase+=freq;
						
						//vec4 	waves=sin( vec4( (freq*8.2)+phase3,	(freq*4.0)+phase2, ((freq*13.5)+2.0*phase3)+1.570796, 	((freq*10.0)+phase2*3.0)+1.570796)	);
						vec2 	windMul=sin( vec2(phase, ((phase+1.0)*5.0)+1.570796) );
						
						/*newpos.xz+=s2_normal.xy*(waves.x*2.0 + waves.z)*dBend.x;
						newpos.y+=(waves.y + waves.w)*dBend.y;*/
						
						float vlen=length(s2_vertex);
						float bend;
						bend=s2_vertex.y*bendScale;
						bend += 1.0;
						bend = (bend * bend - bend) * bendMul.x * windStrenght;
						
						vec2 windVector=windVec.xz;
						mtx[1].w=0.0;
						mtx[2].w=0.0;
					
						newpos.xz+=( XformWind(windVector,mtx)*((windMul.x+windMul.y)*10.0+30.0) *bend );
						newpos.xyz=normalize(newpos.xyz)*vlen;
						//////////////////////////////////////
					#else
						mtx[1].w=0.0;
						mtx[2].w=0.0;
					#endif
					
					newpos=mtx*newpos;
					tpos=gl_ModelViewMatrix * newpos;	
					gl_Position=gl_ProjectionMatrix*tpos;	
					curpos=gl_Position;	
				//	pos=tpos;
					
					// compute prev pos
					//tpos=prevMVM*newpos;	
					//prev=gl_ProjectionMatrix*tpos;
		
				//	texcoord = s2_texcoords.st;	
					
					outNormal=gl_ModelViewMatrix*normal4;
					outTangent=gl_ModelViewMatrix*tangent4;
					outBinormal=gl_ModelViewMatrix*binormal4; 

					TBN[0]=normalize(outTangent.xyz);
					TBN[1]=normalize(outBinormal.xyz);
					TBN[2]=normalize(outNormal.xyz);
					
				#else
				
					occlusion=s2_color.a;
			
					#ifndef _NOT_INSTANCED_
					
						mat4 MTX;
						int index=gl_InstanceID*4;
						MTX[0]=texelFetchBuffer(WorldPoses,index);
						MTX[1]=texelFetchBuffer(WorldPoses,index+1);
						MTX[2]=texelFetchBuffer(WorldPoses,index+2);
						MTX[3]=texelFetchBuffer(WorldPoses,index+3);
						
						#ifdef ALPHATEST
							alphaFade=MTX[3].w+0.01;
							MTX[3].w=1.0;
						#endif
						
						/*mat4 prevMTX;
						prevMTX[0]=texelFetchBuffer(prevWorldPoses,index);
						prevMTX[1]=texelFetchBuffer(prevWorldPoses,index+1);
						prevMTX[2]=texelFetchBuffer(prevWorldPoses,index+2);
						prevMTX[3]=texelFetchBuffer(prevWorldPoses,index+3);*/
						
						vec4 newpos=vec4(s2_vertex,1.0);
						vec4 worldPos;
						
						MTX[1].w=0.0;
						MTX[2].w=0.0;
							
						#ifdef VEGETATION
							
							/*vec2 dBend=s2_color.gb*windStrenght*bendMul.y;
							float phase=dot(prevMTX[3].xyz,vec3(1.0,1.0,1.0));
							float phase2=phase+s2_color.r;
							float phase3=phase2+dot(s2_vertex,vec3(1.0,1.0,1.0));
							phase2*=3.14;
							phase3*=3.14;
							
							vec4 	waves=sin( vec4( (freq*8.2)+phase3,	(freq*4.0)+phase2, ((freq*13.5)+2.0*phase3)+1.570796, 	((freq*10.0)+phase2*3.0)+1.570796)	);
							
							newpos.xz+=s2_normal.xy*(waves.x*2.0 + waves.z)*dBend.x;
							newpos.y+=(waves.y + waves.w)*dBend.y;
							
							float vlen=length(s2_vertex);	
							float bend;
							bend=s2_vertex.y*meshbox.y;
							bend += 1.0;
							bend = (bend * bend - bend) * bendMul.x * windStrenght;
							
							vec2 windVector=vec2(MTX[1].w,MTX[2].w);
							MTX[1].w=0.0;
							MTX[2].w=0.0;
							newpos.xz+=(XformWind(windVector,MTX)*bend);
							newpos.xyz=normalize(newpos.xyz)*vlen;*/
							
							/* prova hue variation */
							float phase=dot(MTX[3].xyz,vec3(1.0,1.0,1.0));
							float sinPhase=sin(phase*0.01);
							vgColor.r=hueVariation*sinPhase;
							vgColor.g=hueVariation*(1.0-sinPhase);
							vgColor.b=hueVariation*-sinPhase;
							vgColor.rgb=vgColor.rgb*vec3(0.5)+vec3(0.5);
							/* */
							//////////////////////////////////////
						/*#else	
							MTX[1].w=0.0;
							MTX[2].w=0.0;*/
						#endif
					
						worldPos=MTX*newpos;
						gl_Position = (gl_ModelViewProjectionMatrix)*worldPos;
						
					//	pos=gl_ModelViewMatrix*worldPos;
					//	worldPos=prevMTX*newpos;
					//	worldPos=prevMVM*worldPos;
					//	prev=gl_ProjectionMatrix*worldPos;
					
					//	texcoord = s2_texcoords.st;		
						curpos=gl_Position;
						
						outNormal=MTX*normal4;
						outTangent=MTX*tangent4;
						outBinormal=MTX*binormal4; 
						outNormal=gl_ModelViewMatrix*outNormal;
						outTangent=gl_ModelViewMatrix*outTangent;
						outBinormal=gl_ModelViewMatrix*outBinormal; 
					
					#else
						vec4 newpos=vec4(s2_vertex,1.0);
					
						#ifdef VEGETATION
							////////////////////////////////////////////
							/*vec2 dBend=s2_color.gb*windStrenght*bendMul.y;
							float phase=dot(prevWTM[3].xyz,vec3(1.0,1.0,1.0));
							float phase2=phase+s2_color.r;
							float phase3=phase2+dot(s2_vertex,vec3(1.0,1.0,1.0));
							phase2*=3.14;
							phase3*=3.14;
							
							vec4 waves=sin( vec4( (freq*8.2)+phase3,	(freq*4.0)+phase2, ((freq*13.5)+2.0*phase3)+1.570796, 	((freq*10.0)+phase2*3.0)+1.570796)	);
							
							newpos.xz+=s2_normal.xy*(waves.x*2.0 + waves.z)*dBend.x;
							newpos.y+=(waves.y + waves.w)*dBend.y;
							
							float vlen=length(s2_vertex);			
							float bend;
							bend=s2_vertex.y*meshbox.y;
							bend += 1.0;
							bend = (bend * bend - bend) * bendMul.x * windStrenght;
							
							vec2 windVector=windVec.xz;
							newpos.xz+=(XformWind(windVector,prevWTM)*bend);
							newpos.xyz=normalize(newpos.xyz)*vlen;*/
							
							/* prova hue variation */
							float phase=dot(prevWTM[3].xyz,vec3(1.0,1.0,1.0));
							float sinPhase=sin(phase*0.01);
							vgColor.r=hueVariation*sinPhase;
							vgColor.g=hueVariation*(1.0-sinPhase);
							vgColor.b=hueVariation*-sinPhase;
							vgColor.rgb=vgColor.rgb*vec3(0.5)+vec3(0.5);
							/* */
							//////////////////////////////////////
							//////////////////////////////////////
						#else
							
						#endif
						
						gl_Position = (gl_ModelViewProjectionMatrix)*newpos;//ftransform();
					//	pos=(gl_ModelViewMatrix * newpos);
						
						//newpos=prevWTM*newpos;
						//newpos=prevMVM*newpos;
						//prev=gl_ProjectionMatrix*newpos;
					//	texcoord = s2_texcoords.st;		
						curpos=gl_Position;
						
						outNormal=gl_ModelViewMatrix*normal4;
						outBinormal=gl_ModelViewMatrix*binormal4; 
						outTangent=gl_ModelViewMatrix*tangent4;
						
					#endif

					TBN[0]=normalize(outTangent.xyz);
					TBN[1]=normalize(outBinormal.xyz);
					TBN[2]=normalize(outNormal.xyz);
					
				#endif
			
			#endif
			
		#endif
		
	#endif


vertColor=dcolor0.rgb;//pow(texture2D(dif0,s2_texcoords.st*dscale0+dofs0).rgb,vec3(2.2))*dcolor0.rgb;//*vec3(0.5,0.5,0.5);

#ifdef VEGETATION
	#ifndef BSET
		#ifndef MSET
			vec3 one=vec3(1.0,1.0,1.0);
			vec3 rmul=vertColor.rgb*vgColor.xyz;
			vec3 rscr=one-((one-vgColor.xyz)*(one-vertColor.rgb));
			vertColor.rgb=vertColor.rgb*rscr+(one-vertColor.rgb)*rmul;
		#endif
	#endif
#endif

vertColor*=occlusion;
//curpos.xy/=curpos.w;
}