










	float PackFloat;


//--------------
//LightMapper
//--------------
	float3 LMRenderPos;
	float LMObjectSizeMax;
	float LMColorRenderLOD=0;
	//for uv render
	float LMImageSize;	
	float LMCameraFov;

//--------------
// un-tweaks
//--------------
	matrix WorldVP:WorldViewProjection; 
	matrix World:World;   
	matrix ViewInv:ViewInverse; 	
	matrix LMOrthoProj;

//--------------
// Textures
//--------------
   struct In_Color
     {
 	float4 Pos:POSITION;
 	float2 Tex0:TEXCOORD0;
 	float2 Tex1:TEXCOORD1;
    float3 Normal:NORMAL;	 
     };
   struct Out_Color
     {
	float4 Pos:POSITION; 
   	float4 Tex:TEXCOORD1;    
   	float3 Color:TEXCOORD2;    	
   	float3 Normal:TEXCOORD4;   	
   	float3 RND:TEXCOORD5;   	  	  		 	   
     };  
     
  float random( float3 p )
{
     float2 K1 = float3( 23.14069263277926, 2.665144142690225 , 214.2493875169875 );
     return frac(cos(dot(p,K1)) * 12345.6789);
}   
    
  float randomv2( float3 p )
{    
	return frac(sin(dot(p, float3(12.9898, 78.233,3.14515))) * 43758.5453);    
}      
    
float iqhash( float n )
{
    return frac(sin(n)*43758.5453);
}

float iqnoise( float3 x )
{
    // The noise function returns a value in the range -1.0f -> 1.0f
    float3 p = floor(x);
    float3 f = frac(x);

    f=f*f*(3.0-2.0*f);
    float n=p.x+p.y*57.0+113.0*p.z;
    return lerp(lerp(lerp( iqhash(n+0.0  ), iqhash(n+1.0  ),f.x),
	lerp( iqhash(n+57.0 ), iqhash(n+58.0 ),f.x),f.y),
	lerp(lerp( iqhash(n+113.0), iqhash(n+114.0),f.x),
	lerp( iqhash(n+170.0), iqhash(n+171.0),f.x),f.y),f.z);
}



    
    
     
   Out_Color VS_Color(In_Color IN) 
     {
  	Out_Color OUT;		
		
	// position based on UV
	float4 UVPos=float4(IN.Tex1.x,IN.Tex1.y*-1,0,1);
	float Offset=(0.5/LMImageSize);
	UVPos.xy=UVPos.xy+float2(-Offset,Offset);
	UVPos.xy=UVPos.xy*LMImageSize+float2(-LMImageSize*0.5,LMImageSize*0.5);
	UVPos.z=-1*(LMImageSize/2.0)/tan(LMCameraFov/2.0);
	OUT.Pos=mul(IN.Pos,WorldVP);	
			  	 		
 	OUT.Tex=float4(IN.Tex0,IN.Tex1);	

	float RND=iqnoise(IN.Normal+IN.Tex0.xyx*1000);
	float RND2=iqnoise(IN.Normal+IN.Normal*1000);

  	OUT.RND = frac((RND));

//delta=cross(delta,IN.Normal);

//delta=dot(delta,IN.Normal);
  //	float3 delta = frac(float3(IN.Normal.x,IN.Normal.y*2,IN.Normal.z*3));

//delta += dot(delta, delta.yzx + 19.19);
// delta=frac((delta.x + delta.y) * delta.z);


	//delta=sin(delta);

 //   fixed4 niceColor = fixed4(lerp(_Color.rgb, _SpecColor.rgb, abs(sin(_Time + delta * PI * 2))), 1);
    //return niceColor;



	//OUT.Delta=delta;//IN.Blend*10000;
	
   // float time=Time;
		
	OUT.Color=iqnoise(IN.Pos+IN.Tex0.xyx*1000);	
	OUT.Normal=IN.Normal;//IN.Pos*0.01*IN.Normal;
	
		
	return OUT;
     }     
     
     
     
//--------------------------------------------------------
//       COLOR + Luminance
//--------------------------------------------------------
float4 PS_Color(Out_Color IN) : COLOR
{ 

//no alpha clip
//add blur using lod
//tex2Dlod(BaseSampler,float4(IN.Tex.zw,0,LMColorRenderLOD));	

//float Luminance=Color.w;
//add if luminance = 0
//Color.w=max(Luminance,0.007);


//---------------------------------------------
//  Generate Flat shading
//float3 dpdx = ddx(IN.LocalPos);
//float3 dpdy = ddy(IN.LocalPos);
//float3 Normal = normalize(cross(dpdx, dpdy));
//---------------------------------------------	

	

//float3 dpdx = ddx(IN.Color);
//float3 dpdy = ddy(IN.Color);
//float3 flatNormal = cross(normalize(dpdx), normalize(dpdy));	

//flatNormal=flatNormal*2-1;

//float3 RNDColor=frac(IN.RND*IN.Normal);



//dpdx = ddx(IN.RND);
//dpdy = ddy(IN.RND);
//float3 flatDelta = normalize(cross(dpdx, dpdy));


float3 dpdx = ddx(IN.RND);
float3 dpdy = ddy(IN.RND);
float3 flatColor = normalize(cross(dpdy, dpdx));



//float4 Color=abs(IN.RND.x);//max(flatDelta,Color.xyz);


//Color.xyz=(cross(flatDelta,sin(flatDelta2))*1);

float4 Color=0;
Color.xyz=(flatColor.xyz);
//Color.xyz=flatNormal;
//Color.xyz=IN.Normal;
//Color.xyz=RNDColor;

//if (flatNormal.x<0) Color.xyz=1;



//Color.xyz=normalize(Color.xyz);

//Color.xyz=IN.Normal;
	
//Color.xyz=Normal;	
return Color;	
}      


float4 PS_White(Out_Color IN) : COLOR
{ 
return 1;	
}  

   technique Color
      {
 	pass p0
      {	           	
 	vertexShader = compile vs_3_0 VS_Color(); 
 	pixelShader  = compile ps_3_0 PS_Color();     
    cullmode=0;
      }     
              
   	pass p1
      {		
 		//vertexShader = compile vs_3_0 VS_UVMap(); 
 		//pixelShader  = compile ps_3_0 PS_White(); 		 		
       // cullmode=0;
        //FillMode = WireFrame;
      }     
      
      
      
      
      }
      
      
      
      




float3 dpdx = ddx(IN.Color);
float3 dpdy = ddy(IN.Color);
float3 flatNormal = cross(normalize(dpdx), normalize(dpdy));	

//flatNormal=flatNormal*2-1;

float3 RNDColor=frac(IN.Delta*IN.Normal);



dpdx = ddx(IN.Delta);
dpdy = ddy(IN.Delta);
float3 flatDelta = normalize(cross(dpdx, dpdy));


dpdx = ddx(IN.DeltaFrac);
dpdy = ddy(IN.DeltaFrac);
float3 flatDelta2 = normalize(cross(dpdy, dpdx));



Color.xyz=abs(IN.Delta);//max(flatDelta,Color.xyz);


Color.xyz=(cross(flatDelta,sin(flatDelta2))*1);
Color.xyz=normalize(Color.xyz);
//Color.xyz=flatNormal;
//Color.xyz=IN.Normal;
//Color.xyz=RNDColor;

//if (flatNormal.x<0) Color.xyz=1;



//Color.xyz=normalize(Color.xyz);

//Color.xyz=IN.Normal;
	
//Color.xyz=Normal;	



   
   Out_UVMap VS_UVMap(In_UVMap IN) 
     {
  	Out_UVMap OUT;		

	OUT.LocalPos=IN.Pos;
	OUT.Pos=mul(IN.Pos,WorldVP);				  	 		
 	OUT.Tex=IN.Tex0;	
	

	//float3 WNor=mul(IN.Normal,World);
	//WNor=normalize(WNor);
	
	
	
	//float tPos=mul(IN.Pos,WorldVP);


//float3 DeltaSrc=IN.Pos;//*sin(time);

//float3 DeltaSrc=IN.Normal*IN.Pos*0.01;


float RND=iqnoise(IN.Pos+IN.Tex0.xyx*1000);
float RND2=iqnoise(IN.Pos+IN.Normal*1000);


//float RND=randomv2(IN.Normal);
//float RND2=randomv2(IN.Pos);

  //	float3 delta = (float3(DeltaSrc.x*RND,DeltaSrc.y*(RND+RND2),DeltaSrc.z*RND2));
  	 
  	 
	//float3 delta = 


	OUT.RND=float3(IN.Pos.x*RND,IN.Pos.y*(RND+RND2),IN.Pos.z*RND2);
  	OUT.RNDFrac = frac(float3(IN.Pos.z*RND,IN.Pos.x*(RND/RND2),IN.Pos.y*RND2));

//delta=cross(delta,IN.Normal);

//delta=dot(delta,IN.Normal);
  //	float3 delta = frac(float3(IN.Normal.x,IN.Normal.y*2,IN.Normal.z*3));

//delta += dot(delta, delta.yzx + 19.19);
// delta=frac((delta.x + delta.y) * delta.z);


	//delta=sin(delta);

 //   fixed4 niceColor = fixed4(lerp(_Color.rgb, _SpecColor.rgb, abs(sin(_Time + delta * PI * 2))), 1);
    //return niceColor;


	//OUT.Color=IN.Pos;//mul(IN.Pos,World);
	
	OUT.Normal=IN.Normal;//IN.Pos*0.01*IN.Normal;
	
		
	return OUT;
     }     
     