/*#ifdef SKINNING

	#version 120
	#extension EXT_gpu_shader4 : enable          

#endif*/

#extension GL_ARB_shading_language_420pack: enable 

#ifdef _VERSION_CHECK_
	#ifndef MSET

		#ifndef _NOT_INSTANCED_
		
			#version 120
			#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; 

#ifdef ALPHATEST

	#ifdef BSET
	//	varying float		distance;
		uniform float 		fade;
	#endif
	//uniform vec2		cull;
	varying float		alphaFade;

	#ifdef VEGETATION
		uniform float 	windStrenght;
		uniform vec3 	windVec;
		uniform float	time;
		uniform vec2 	meshbox;
		uniform vec2	bendMul;
		float 			freq=time/*+time*windStrenght*0.25*/;
		
		#ifndef BSET
			#ifndef MSET
				uniform float	hueVariation;
				varying vec3	vgColor;
			#endif
		#endif
		//uniform vec3 	prevcampos;
	#endif
				
#endif


#ifdef _HEIGHTMAP_
	
	uniform sampler2D	heightmap;
	//uniform sampler2D	cmask;
	uniform vec2		invHeightmapSize;
	uniform vec2		tileDim;
	//uniform vec3		tilePos;	// tilepos
	uniform vec3		terrainDim; // width, depth and height of the terrain
 	uniform vec3		invTerrainDim;
	uniform vec4		colorMod;			//x = LODpower, y = startFade, z = 1.0/(endFade-startFade)
	uniform vec3		WorldPoses[512];
	uniform vec2		campos;
	uniform float		tileRes;
	
	uniform vec2		baseTiles;
//	uniform vec3		baseTexParams;
	varying mat3		TBN;
//	varying vec3		vColor;
	//varying vec3		wNormal;//vSlope;
	varying vec2		baseCoords;
	varying float		NWheight;
	uniform float		terrainNormOrg;
#else

	#ifdef SKINNING

	/*	uniform mat3 	boneMatrices[100],
						prevBoneMatrices[100];
		*/				
		uniform samplerBuffer 	boneMatrices;
		//uniform samplerBuffer 	prevBoneMatrices;
		//attribute vec4 		 	weights;
		//attribute vec4  		matrixIndices;

		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;

			//attribute vec4	index;
			varying float 	life;

			uniform vec2	tofs;			// cambiare in uniform
			uniform vec2	tscale;
			//varying vec4	pnormal;
			varying float	cosa,
							sina;
			uniform vec4 	particleScale;
	
		#else

			#ifdef BSET

				//uniform	mat4 	pparams[100];
				
				//uniform float pparams[1000];
				uniform 		samplerBuffer 	pparams;
				//uniform vec4 pparams[300];
				vec4			tpos;
				uniform vec4	data;
				//attribute vec4	index;
				varying vec4	pnormal;
				varying vec4 	pcolor;
				
				uniform vec3	campos;
				
				////////////
				varying mat3	TBN;
				///////////////
				
			#else
	
				#ifdef MSET

					uniform	mat4 	pparams[/*64*/60];
					varying vec4 	pcolor;
					vec4		tpos;
					//attribute vec3 	index;
					
					//varying vec3 	pcolor;
				
				#else
				
					#ifndef _NOT_INSTANCED_
					
						uniform samplerBuffer 		WorldPoses;
						//uniform samplerBuffer 		prevWorldPoses;
					
					#endif
					
					varying float occlusion; 
					
				#endif
		
				varying mat3	TBN;

			#endif

		#endif
						
	#endif
	
#endif

varying vec4 			texcoord;
					
vec4 					outNormal,
						outTangent,
						outBinormal; 

varying vec4 	curpos,
				pos;
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()
{					
#ifdef _HEIGHTMAP_

	// compute position basing on heightmap
	vec4 	position00,
			position10,
			position01;
		
	mat4 MTX;
	vec3 tilepos=WorldPoses[gl_InstanceID];

	// compute LOD ///////////////////////////////////
	vec2 gridDim = vec2(tileRes / colorMod.x);
	vec2 quadDim = vec2(tileDim * colorMod.x);
	vec2 tileSize = quadDim*gridDim;
	vec2 gridPos = s2_vertex.xz/tileSize + 0.5;
	float camdist=length((campos-tilepos.xz)-s2_vertex.xz);
	float alpha=clamp((camdist - colorMod.y)*colorMod.z,0.0,1.0);
	vec2 fracPart = fract(gridPos * gridDim*0.5)*2.0;// / gridDim;
	vec2 vertpos = s2_vertex.xz - fracPart * quadDim * alpha;
	////////////////////////////////////////////////////
	
	texcoord.st = ((vertpos + tilepos.xz)*invTerrainDim.xz)+vec2(0.5,0.5)+(invHeightmapSize*0.5);
	position00.y=(texture2D(heightmap,texcoord.st).x-terrainNormOrg)*terrainDim.y;
	position00.xz=vertpos;
	position10.y=(texture2D(heightmap,texcoord.st + vec2(invHeightmapSize.x,0.0)).x-terrainNormOrg)*terrainDim.y;
	position10.xz=vertpos + vec2(tileDim.x,0.0);
	position01.y=(texture2D(heightmap,texcoord.st + vec2(0.0,invHeightmapSize.y)).x-terrainNormOrg)*terrainDim.y;
	position01.xz=vertpos + vec2(0.0,tileDim.y);
	
	vec4 position;
	position.xyz=tilepos+position00.xyz;
	position.w=1.0;
	NWheight=((position00.y*invTerrainDim.y)+terrainNormOrg);//*0.5+0.5;
	
	// transform position in projection and eye space
	gl_Position = gl_ModelViewProjectionMatrix * position;
	pos=gl_ModelViewMatrix*position;
	
	// compute normals (convert heightmap into normalmap
	vec3 binormal3=vec3(0.0,0.0,-1.0);//normalize(position00.xyz-position10.xyz);
	vec3 tangent3=vec3(1.0,0.0,0.0);//normalize(position00.xyz-position01.xyz);
	vec3 normal3=vec3(0.0,1.0,0.0);//cross(tangent3,binormal3);
	
	TBN[0]=gl_NormalMatrix*tangent3;//vec3(0.0,0.0,1.0);//
	TBN[1]=gl_NormalMatrix*binormal3;//vec3(1.0,0.0,0.0);
	TBN[2]=gl_NormalMatrix*normal3;//vec3(0.0,1.0,0.0);//
	
	curpos=gl_Position;
	baseCoords=texcoord.st*baseTiles;
	
#else

	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;
			
			/*pTM[0].xyz=texelFetchBuffer(prevBoneMatrices,j).xyz;
			pTM[1].xyz=texelFetchBuffer(prevBoneMatrices,j+1).xyz;
			pTM[2].xyz=texelFetchBuffer(prevBoneMatrices,j+2).xyz;
			pTM[3].xyz=texelFetchBuffer(prevBoneMatrices,j+3).xyz;
			*/
			//j+=4;
			position = position + (TM * vertex) *weight[i];
			//prevpos = prevpos + ( (pTM*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;
		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;//*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

			#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;
				
				vec4 scale=texelFetchBuffer(pparams,i+2);
				scale.w=abs(scale.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.w;
				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;
				
				pnormal.xyz=PTM * normalize(vec3(0.0,0.0,0.5) + normalize(newpos.xyz));//normalize((normal4.xyz + normalize(newpos.xyz) + s2_normal.xyz));
				//PTM=mat3(gl_ModelViewMatrix[0].xyz,gl_ModelViewMatrix[1].xyz,gl_ModelViewMatrix[2].xyz);
				vec4 pn=gl_ModelViewMatrix * vec4(pnormal.xyz,0.0);
				pnormal.xyz=pn.xyz;//normalize(vec3(0.0,0.01,1.0));//normalize((normal4.xyz + normalize(newpos.xyz) + s2_normal.xyz));
				pnormal.z=-pnormal.z;
				
				/*outNormal.xyz=PTM*normal4.xyz;
				outTangent.xyz=PTM*tangent4.xyz;
				outBinormal.xyz=PTM*binormal4.xyz; 
				outNormal=gl_ModelViewMatrix*outNormal;
				outTangent=gl_ModelViewMatrix*outNormal;
				outBinormal=gl_ModelViewMatrix*outNormal; 
				TBN[0]=outTangent.xyz;
				TBN[1]=outBinormal.xyz;
				TBN[2]=outNormal.xyz;*/
				
				texcoord = s2_texcoords;	
		
			#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);
					pcolor=vec4(mtx[0].w,mtx[1].w,mtx[2].w,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;//vec2(mtx[1].w,mtx[2].w);
						mtx[0].w=0.0;
						mtx[1].w=0.0;
						mtx[2].w=0.0;
					
						newpos.xz+=( XformWind(windVector,mtx)/*vec2(mtx[1].w,mtx[2].w)*/ * ((windMul.x+windMul.y)*10.0+30.0)*bend );
						newpos.xyz=normalize(newpos.xyz)*vlen;
						//////////////////////////////////////
					#else
						mtx[0].w=0.0;
						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;	
					
					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;
						
						#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)/*vec2(phase+phase2)*/);
							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)/*vec2(MTX[1].w,MTX[2].w)*/*bend);
							newpos.xyz=normalize(newpos.xyz)*vlen;
							
							/* prova hue variation */
							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;//ftransform();
						
						pos=gl_ModelViewMatrix*worldPos;
						//worldPos=prevMTX*newpos;
						//worldPos=prevMVM*worldPos;
						//prev=gl_ProjectionMatrix*worldPos;
						texcoord = s2_texcoords;		
						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)/*vec2(phase+phase2)*/);
							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 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
						
						//vec4 worldPos=prevWTM*newpos;
						gl_Position = (gl_ModelViewProjectionMatrix)*newpos;//ftransform();
						pos=(gl_ModelViewMatrix * newpos);
						
						//newpos=worldPos;
						//newpos=prevMVM*newpos;
						//prev=gl_ProjectionMatrix*newpos;
						texcoord = s2_texcoords;		
						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
	
#endif

#ifdef ALPHATEST
	#ifdef BSET
		float dist=(length(pos)-fade*0.5)/(fade*0.5);//*0.8);
		pcolor.w*=(1.0 - (clamp(dist,0.0,1.0)));
		pcolor.w+=0.01;
		
	#endif
#endif

	//curpos.xy/=curpos.w;
	//depth=gl_Position.zw;					
}