static const float2 TexProt[5] = 
{0.0, 0.0,
 0.0, 0.0,
 0.5, 0.0,
 0.0, 0.5,
 0.5, 0.5};

float4x4 matAll;
float4x4 matWorld;
float3 LightDir;
float FogStart;
float FogEnd;

float4 Pos[40];// xyz scale X
float4 Col[40];// RGBA
float4 RotScl[40];// qwaternion
float  Scl[40];// scale Y
int TexCprot[40];

float4 quatMul ( float4 q1, float4 q2)
{
    float3  im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );
    float4  dt = q1 * q2;
    float re = dot ( dt, float4 ( -1.0, -1.0, -1.0, 1.0 ) );
    return float4 ( im.xyz, re );
}

float4 quatRotate ( float3 p, float4 q)
{
    float4 temp = quatMul ( q, float4 ( p, 0.0 ) );
    return quatMul ( temp, float4 ( -q.x, -q.y, -q.z, q.w ) );
}

struct VS_INPUT
{
  float4 position  : POSITION;
  float2 tex0 : TEXCOORD0;
  float3 normal: NORMAL;
};

struct VS_OUTPUT
{
  float4 position: POSITION;
  float2 tex0 : 	 TEXCOORD0;
  float4 diffuse : TEXCOORD1;
  float3 light: TEXCOORD2;
  float  fog: 	 TEXCOORD3;
};

VS_OUTPUT main( VS_INPUT IN )
{
	VS_OUTPUT OUT;
	int ins=round(IN.position.z);
	float3 pns = float3(Pos[ins].xyz);
	float4 q=RotScl[ins];//get quaternion
	float4 V= float4(IN.position.x*Pos[ins].w,IN.position.y*Scl[ins],0.0f,1.0f);
	float4 pos;
	pos.xyz = pns.xyz + quatRotate(V.xyz,q).xyz;
	pos.w = 1.0;
	OUT.position = mul( pos , matAll);

		int txpro=TexCprot[ins];
		if (txpro!=0) {OUT.tex0=IN.tex0*0.5+TexProt[txpro];}
		else {OUT.tex0=IN.tex0;}

	OUT.diffuse = Col[ins];
	  float li=dot(normalize(mul(quatRotate(IN.normal,q).xyz,matWorld)),normalize(LightDir));
     	  if (li<0) {li=0;}
     	  OUT.light = li;

	  float fogy=(OUT.position.z-FogStart)/(FogEnd-FogStart);
	  if (fogy>1) {fogy=1;}
	  OUT.fog=fogy;

	return OUT;
}