



	float PackFloat;
   float time:Time;

//--------------
//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_FlatShading
     {
 	float4 Pos:POSITION;
 	float2 Tex0:TEXCOORD0;
    float3 Normal:NORMAL;	 
     };
   struct Out_FlatShading
     {
	float4 Pos:POSITION; 
   	float2 Tex:TEXCOORD1;       	
   	float3 LocalPos:TEXCOORD3; 
   	float3 Normal:TEXCOORD4;   	
   	float3 RND:TEXCOORD5; 
   	float3 RNDFrac:TEXCOORD6;    	  	  		 	   
     };  
     
  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_FlatShading VS_FlatShading(In_FlatShading IN) 
     {
  	Out_FlatShading OUT;		

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


	float3 PosMax=max(IN.Pos.xyz,IN.Pos.yzx);


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

	//float PosMax=IN.Pos.x+IN.Pos.y+IN.Pos.z;
	
	
	OUT.RND=float3(IN.Pos.x*RND,IN.Pos.y*(RND2+RND)*10,IN.Pos.z*RND2);
  
  	OUT.RNDFrac = frac(float3((IN.Pos.x)+RND,(IN.Pos.y)+(RND/RND2),(IN.Pos.z)+RND2));
	OUT.Normal=IN.Normal;
		
	return OUT;
     }     
     
     
     
//--------------------------------------------------------
//       COLOR + Luminance
//--------------------------------------------------------
float4 PS_FlatShading1(Out_FlatShading IN) : COLOR
{ 

float4 Color=1;
float3 dpdx,dpdy,Normal;

//---------------------------------------------
// style1 Generate Flat shading
dpdx = ddx(IN.RNDFrac);
dpdy = ddy(IN.RNDFrac);
Normal = normalize(cross(dpdx, dpdy));
Color.xyz=abs(Normal);
//---------------------------------------------	

return Color;	
}  

float4 PS_FlatShading2(Out_FlatShading IN) : COLOR
{ 
float4 Color=1;
float3 dpdx,dpdy,Normal;

//---------------------------------------------
// style2 Generate Flat shading
dpdx = ddx(IN.RND);
dpdy = ddy(IN.RND);
Normal = normalize(cross(dpdx, dpdy));
Color.xyz=abs(Normal);
//---------------------------------------------	

return Color;	
}  

float4 PS_FlatShading3(Out_FlatShading IN) : COLOR
{ 
float4 Color=1;
float3 dpdx,dpdy,Normal;

//---------------------------------------------
// style3 Generate Flat Normals
dpdx = ddx(IN.LocalPos);
dpdy = ddy(IN.LocalPos);
Normal = normalize(cross(dpdx, dpdy));
Color.xyz=Normal;
//---------------------------------------------	


return Color;	
}      


float4 PS_White(Out_FlatShading IN) : COLOR
{ 
return float4(1,1,1,0.25);	
}  

   technique FlatColor1
      {
 	pass p0
      {	           	
 	vertexShader = compile vs_3_0 VS_FlatShading(); 
 	pixelShader  = compile ps_3_0 PS_FlatShading1();     
    cullmode=0;
      }     
              
   	pass p1
      {		
 		vertexShader = compile vs_3_0 VS_FlatShading(); 
 		pixelShader  = compile ps_3_0 PS_White(); 		 		
        AlphaBlendEnable = True;
        cullmode=0;
        FillMode = WireFrame;
      }       
      }
      
   technique FlatColor2
      {
 	pass p0
      {	           	
 	vertexShader = compile vs_3_0 VS_FlatShading(); 
 	pixelShader  = compile ps_3_0 PS_FlatShading2();     
    cullmode=0;
      }     
              
   	pass p1
      {		
 		vertexShader = compile vs_3_0 VS_FlatShading(); 
 		pixelShader  = compile ps_3_0 PS_White(); 		 		
        AlphaBlendEnable = True;
        cullmode=0;
        FillMode = WireFrame;
      }       
      }      
      
   technique FlatColor3
      {
 	pass p0
      {	           	
 	vertexShader = compile vs_3_0 VS_FlatShading(); 
 	pixelShader  = compile ps_3_0 PS_FlatShading3();     
    cullmode=0;
      }     
              
   	pass p1
      {		
 		vertexShader = compile vs_3_0 VS_FlatShading(); 
 		pixelShader  = compile ps_3_0 PS_White(); 		 		
        AlphaBlendEnable = True;
        cullmode=0;
        FillMode = WireFrame;
      }       
      }      
            
      
      
      
      
      
