//--------------------------------------------------------------------------
//
// WHALE ENGINE 1.0
// LGM GAMES
// DIRECT X9c
// SHADER v2.0
//          
//--------------------------------------------------------------------------

/*
texture TextureDifuse;            
texture TextureLightMap;                 
texture TextureNormalMap;
texture TextureCubeMap;                 
texture TextureShadowMap;
*/
float4	 g_fScale;
float    g_fTime;                    // App's time in seconds
float4x4 g_mWorld;                    // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
float4x4 g_mShadowProjection;
vector   CameraPosition;
vector   g_LightAmbient =  {1.0f, 1.0f, 1.0f, 1.0f};
float4   UV; //Umin, Umax, Vmin, Vmax
vector   ExtraVector = { 0.5f, 0.5f, 0.5f, 0.5f }; 
float4   SpecilaParam;
//--------------------------------------------------------------------------
// Globals
//--------------------------------------------------------------------------
// The per-color weighting to be used for luminance calculations in RGB order.
static const float3 LUMINANCE_VECTOR  = float3(0.2125f, 0.7154f, 0.0721f);

// The per-color weighting to be used for blue shift under low light.
static const float3 BLUE_SHIFT_VECTOR = float3(1.05f, 0.97f, 1.27f); 
static const float BLUR_FACTOR = 0.1f;

bool bUseBlueShift = true;

float4		tcLumOffsets[2];			// The offsets used by GreyScaleDownSample()
float4		tcDSOffsets[5];				// The offsets used by DownSample()

float gDeltaTime;
float Exposure = 1.0;
float BlurFactor = 0.8;//0.5f;

static const float  GAMA = 0.75f;

static const float  MIDDLE_GRAY = 0.3;///0.35f;
static const float  LUM_WHITE = 1.6;//1.3f;

static const float  MIDDLE_GRAY_BLOOM = 0.25f;
static const float  LUM_WHITE_BLOOM = 1.6f;

texture Diffuse;    // difuse map
texture Glow;       // glow map
texture Refraction; // refraction map
texture Star;       // star texture
texture Tone;       // tone map
texture Depth;       // tone map

vector FrameBufferColor = float4(1.0f, 1.0f, 1.0f, 1.0f);

matrix ViewProjMatrix;

//--------------------------------------------------------------------------
// Vertex shader
//--------------------------------------------------------------------------
struct VS_INPUT
{
 float4 Position : POSITION;
 float2 TextureUV : TEXCOORD0;
};
//--------------------------------------------------------------------------
struct VS_OUTPUT
{
 float4 Position : POSITION;
 float2 TextureUV : TEXCOORD0;
};
//--------------------------------------------------------------------------
VS_OUTPUT VShade(VS_INPUT input)
{
 // zero out members of output
 VS_OUTPUT output;
 
 // transform to view space and project
 output.Position  = mul(input.Position, ViewProjMatrix);
 
 output.TextureUV = input.TextureUV;

 return output;
}
//--------------------------------------------------------------------------



//--------------------------------------------------------------------------
// Pixel shader
//--------------------------------------------------------------------------
uniform sampler   BaseTex: register(s0) = 
sampler_state 
{
 texture = <Diffuse>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Mirror; 
 AddressV = Mirror; 
 AddressW = Mirror; 
};
//--------------------------------------------------------------------------
uniform sampler   DepthTex = 
sampler_state 
{
 texture = <Depth>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Clamp; 
 AddressV = Clamp; 
 AddressW = Clamp; 
};/**/

//--------------------------------------------------------------------------
uniform sampler   GlowTex = 
sampler_state 
{
 texture = <Glow>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Clamp; 
 AddressV = Clamp; 
 AddressW = Clamp; 
};
//--------------------------------------------------------------------------
uniform sampler   StarTex = 
sampler_state 
{
 texture = <Star>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Clamp; 
 AddressV = Clamp; 
 AddressW = Clamp; 
};
//--------------------------------------------------------------------------
uniform sampler   RefractionTex = 
sampler_state 
{
 texture = <Refraction>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Point;

 AddressU = Clamp; 
 AddressV = Clamp; 
 AddressW = Clamp; 
};
//--------------------------------------------------------------------------
uniform sampler   ToneTex: register(s1) = 
sampler_state 
{
 texture = <Tone>;
 MinFilter = Point;
 MagFilter = Point;
 MipFilter = Point;

 AddressU = Clamp; 
 AddressV = Clamp; 
 AddressW = Clamp; 
};
//--------------------------------------------------------------------------
struct PS_OUTPUT
{
 vector diffuse : COLOR0;
};
//--------------------------------------------------------------------------
half luminance(half3 c)
{
 return dot(c, float3(0.3, 0.59, 0.11));
}
//--------------------------------------------------------------------------
float vignette(const float2 win_bias)
{
	// convert window coord to [-1, 1] range
	float2 wpos = 2*(win_bias - (float2)(0.5, 0.5)); 

	// calculate distance from origin
	float r = length(wpos.xy);
	r = 1.0 - smoothstep(0.8, 1.5, r);
	
	return  r;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShade1_4(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02f);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);

 half4 c1 = tex2D(BaseTex, NewCor);
 //half4 c2 = tex2D(GlowTex, NewCor);
 
 output.diffuse = c1;//+c2;//(c1*1.0f)+(c2*(c2.a/*0.7f*/));
 //output.diffuse *= Exposure;
 //output.diffuse *= FrameBufferColor;
 
 return output;
}
PS_OUTPUT Simple(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 half4 c1 = tex2D(BaseTex, Tex0);
 
 output.diffuse = c1;
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShadeBloom(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02f);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);
 half4 c1 = tex2D(BaseTex, NewCor);
 half4 c2 = tex2D(GlowTex, NewCor);
 
 output.diffuse = (c1*0.8f)+(c2*0.65f);
 //output.diffuse *= FrameBufferColor;
 
 float v = vignette(NewCor.xy); 
 //output.diffuse = lerp(luminance(output.diffuse.rgb), output.diffuse, saturate(v-0.2));
 output.diffuse.a = lerp(BLUR_FACTOR, 0.8f, saturate(v-0.2));
  
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShadeNormal(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02f);
 //offset = 0.01*cos(Tex0.y*16)*sin(g_fTime*6)*cos(Tex0.x*24);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);
 //float3 test = tex2D(RefractionTex, NewCor);
 //offset *= (test.r+test.g+test.b);

 //NewCor = /*Tex0;//*/float2(Tex0.x+offset, Tex0.y+offset);
 //if (test.r+test.g+test.b == 0) NewCor = float2(Tex0.x, Tex0.y);
 half4 c1 = tex2D(BaseTex, NewCor);
 half4 c2 = tex2D(GlowTex, NewCor);
 
 output.diffuse = (c1*1.0)+(c2*0.55);
 //output.diffuse = (c1*0.8)+(c2*0.55);
 //output.diffuse *= Exposure;
 //output.diffuse *= FrameBufferColor;
 
 //float v = vignette(NewCor.xy); 
 //output.diffuse = lerp(luminance(output.diffuse.rgb), output.diffuse, saturate(v-0.2));
 output.diffuse.a = 1.0f;//lerp(BLUR_FACTOR, 0.8f, saturate(v-0.2));
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShadeNormalLuminance(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02f);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);

 half4 c1 = tex2D(BaseTex, NewCor);
 half4 c2 = tex2D(GlowTex, NewCor);
 float3 lum = luminance(c1.rgb);
 
 output.diffuse = (vector(lum, 1)*0.8)+(c2*0.55);

 output.diffuse.a = 1.0f;
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShadeNormalLuminanceDream(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02f);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);
 NewCor.y = NewCor.y+sin(g_fTime+Tex0.y*200.0f)*0.001f;
 NewCor.x = NewCor.x+cos(g_fTime+Tex0.x*200.0f)*0.001f;

 half4 c1 = tex2D(BaseTex, NewCor);
 half4 c2 = tex2D(GlowTex, NewCor);
 float3 lum = luminance(c1.rgb);
 
 output.diffuse = (vector(lum, 1)*0.8)+(c2*0.55);

 output.diffuse.a = 1.0f;
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT PShadeHDR(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 float4 c3 = tex2D(RefractionTex, Tex0);
 float offset = ((c3.r+c3.g+c3.b)*0.02);
 float2 NewCor = float2(Tex0.x+offset, Tex0.y+offset);
 float3 test = tex2D(RefractionTex, NewCor);
 offset *= (test.r+test.g+test.b);
 NewCor = float2(Tex0.x+offset, Tex0.y+offset);

 //float4 c1 = (tex2D(BaseTex, NewCor)+tex2D(BaseTex, float2(NewCor.x+0.00097, NewCor.y)))*0.5f;
 float4 c1 = tex2D(BaseTex, NewCor);
 float4 c2 = tex2D(GlowTex, NewCor);
 float4 StarColor = tex2D(StarTex, NewCor);
 float fAdaptedLum = tex2D(ToneTex, float2(0.5, 0.5)).r;
	
 // BLUE SHIFT
 // Define a linear blending from -1.5 to 2.6 (log scale) which
 // determines the lerp amount for blue shift
 if (bUseBlueShift)
 {
  float fBlueShiftCoefficient = 1.0 - ((fAdaptedLum) + 1.5)/4.1;
  fBlueShiftCoefficient = saturate(fBlueShiftCoefficient);

  // Lerp between current color and blue, desaturated copy
  float3 vRodColor = dot( (float3)c1, LUMINANCE_VECTOR ) * BLUE_SHIFT_VECTOR;
  c1.rgb = lerp( c1.rgb, vRodColor, saturate(fBlueShiftCoefficient-0.28));	
 }
 
 // TONE MAPPING
 c1.rgb *= MIDDLE_GRAY / (fAdaptedLum.r+0.095);
 c1.rgb *= (1.0 + c1/LUM_WHITE);        
 c1.rgb /= (1.0 + c1);     
 //c1.rgb = pow(c1.rgb, GAMA);
 
 /*  
 c2.rgb *= MIDDLE_GRAY_BLOOM / (fAdaptedLum.r+0.08);
 c2.rgb *= (1.0 + c2/LUM_WHITE_BLOOM);        
 c2.rgb /= (1.0 + c2);      
 */
 //c2.rgb = pow(c2.rgb, GAMA);
 
 //StarColor.rgb *= 0.40 / (fAdaptedLum.r+0.08);
 //StarColor.rgb *= (1.0 + StarColor/1.6);        
 //StarColor.rgb /= (1.0 + StarColor);      
 	
 c1.rgb += (c2)+(StarColor);
 output.diffuse = c1;//*FrameBufferColor;

 float v = vignette(NewCor.xy); 
 //output.diffuse = lerp(luminance(output.diffuse.rgb), output.diffuse, saturate(v-0.2));
 output.diffuse.a = lerp(BLUR_FACTOR, 0.8f, saturate(v-0.2));
    
 return output;
}

//--------------------------------------------------------------------------
float4 GreyScaleDownSample( in float2 t : TEXCOORD0 ) : COLOR0
{
	// Compute the average of the 4 necessary samples
	float average = 0.0f;
	float4 color = 0.0f;
	float3 WEIGHT = float3( 0.33f, 0.34f, 0.33f );
			
	for( int i = 0; i < 2; i++ )
	{
		for( int j = 0; j < 2; j++ )
		{
			color = tex2D( ToneTex, t + float2(tcLumOffsets[i][j * 2], tcLumOffsets[i][(j * 2) + 1]) );
			average += dot( color.rgb, WEIGHT );
		}
	}
						
 average /= 4;
			
	// Output the luminance to the render target
	return float4( average, average, average, 1.0f );
}		
//--------------------------------------------------------------------------
float4 DownSample( in float2 t : TEXCOORD0 ) : COLOR0
{
 // Compute the average of the 10 necessary samples
	float4 color = 0.0f;
	float average = 0.0f;
			
	for( int i = 0; i < 5; i++ )
	{
		for( int j = 0; j < 2; j++ )
		{
			color = tex2D( ToneTex, t + float2(tcDSOffsets[i][j * 2], tcDSOffsets[i][(j * 2) + 1]) );
			// Because the input (and output) should be either
			// R32F or R16F in format, we're only interested in the RED channel
			// for computing the average.
			average += color.r;
		}
	}
			
	// We've just taken 10 samples from the
	// high resolution texture, so compute the
	// actual average that is written to the
	// lower resolution texture (render target).
	average /= 10.0f;
			
 // Return the new average luminance
	return float4( average, average, average, 1.0f );
}
//------------------------------------------------------------------	
float4 LuminanceDisplay( in float2 t : TEXCOORD0 ) : COLOR0
{
	// Acquire the luminance from the texture
		float4 l = tex2D(BaseTex, t);
			
	// Compute a simple scalar, due to the values being > 1.0f
	// the output is often all white, so just to make it a little
	// more informative we can apply a scalar to bring it closer
	// to the 0..1 range
	float scalar = 1.0f;
			
	// Due to being an R32F or R16F texture we only want
	// to use the red channel
	return float4( l.r * scalar, l.r * scalar, l.r * scalar, 1.0f );
}
//--------------------------------------------------------------------------
PS_OUTPUT CalculateAdaptedLum(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 half4 Color = tex2D(BaseTex, Tex0);
 float value = 0.75f;
 float4 m = min(Color, 0.5);
 float4 n = max((Color - 0.5)*2, 0);
 float4 a = m+n;//m, 0.0);

 float3 DarkColor = float3(0.2,0.05,0);
    
 float3 result = lerp(a,Color ,0.5f);

 result.r = result.r * 1.06f;
 result.g = result.g * 1.04f;
 result.b = result.b * 0.90f;
 
 float3 resultB = result;


float EdelDep = 0;
 float depthSample1 = tex2D(DepthTex , Tex0).r;
//output.diffuse = float4(depthSample1, 0, 0, 1);
 float depthSample2 = tex2D(DepthTex , float2(Tex0.x+0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) 
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 
 depthSample2 = tex2D(DepthTex , float2(Tex0.x+3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
// if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;/**/
// if(abs(depthSample1 - depthSample2) > 0.0005f) result *= 0;//result*result;


 if(EdelDep < 0) EdelDep = abs(EdelDep*100);//-depthSample1*8);///8;
 else EdelDep = 1;
 
 output.diffuse =  float4(EdelDep, EdelDep, EdelDep, 1);//float4(resultB*EdelDep, 1);//float4(resultB - result,1);//Color*Color;

 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT CrnoBijeliCartoon(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 half4 Color = tex2D(BaseTex, Tex0);
 float value = 0.75f;
 float4 m = min(Color, 0.5);
 float4 n = max((Color - 0.5)*2, 0);
 float4 a = m+n;//m, 0.0);

 float3 DarkColor = float3(0.2,0.05,0);
    
 float3 result = lerp(a,Color ,0.5f);

 result.r = result.r * 1.06f;
 result.g = result.g * 1.04f;
 result.b = result.b * 0.90f;
 
 float3 resultB = result;


float EdelDep = 0;
 float depthSample1 = tex2D(DepthTex , Tex0).r;
//output.diffuse = float4(depthSample1, 0, 0, 1);
 float depthSample2 = tex2D(DepthTex , float2(Tex0.x+0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) 
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 
 depthSample2 = tex2D(DepthTex , float2(Tex0.x+3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 //if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
// if(abs(depthSample1 - depthSample2) < 0.00005f) result *= 0.6f;/**/
// if(abs(depthSample1 - depthSample2) > 0.0005f) result *= 0;//result*result;


 if(EdelDep < 0) EdelDep = abs(EdelDep*100);//-depthSample1*8);///8;
 else EdelDep = 1;
 
 output.diffuse =  float4(EdelDep, EdelDep, EdelDep, 1);//float4(resultB*EdelDep, 1);//float4(resultB - result,1);//Color*Color;

 
 return output;
}

//--------------------------------------------------------------------------
PS_OUTPUT CrnoBijeliRubovi(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 half4 Color = tex2D(BaseTex, Tex0);
 float value = 0.75f;
 float4 m = min(Color, 0.5);
 float4 n = max((Color - 0.5)*2, 0);
 float4 a = m+n;//m, 0.0);

 float3 DarkColor = float3(0.2,0.05,0);
    
 float3 result = lerp(a,Color ,0.5f);

 result.r = result.r * 1.06f;
 result.g = result.g * 1.04f;
 result.b = result.b * 0.90f;
 
 float3 resultB = result;


float EdelDep = 0;
 float depthSample1 = tex2D(DepthTex , Tex0).r;
 float depthSample2 = tex2D(DepthTex , float2(Tex0.x+0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 
 depthSample2 = tex2D(DepthTex , float2(Tex0.x+3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x-3*0.001171875f, Tex0.y)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y+3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;
 depthSample2 = tex2D(DepthTex , float2(Tex0.x, Tex0.y-3*0.001171875f)).r;
 EdelDep += depthSample2 - depthSample1;


 if(EdelDep > 0.00001f) EdelDep = abs(EdelDep*100);
 else EdelDep = 1;
 
 //
 output.diffuse =  float4(EdelDep, EdelDep, EdelDep, 1);//float4(resultB*EdelDep, 1);

 
 return output;
}
float rand(float2 co)
{
	return 0.5+(frac(sin(dot(co.xy , float2(12.9898,78.233))) * 43758.5453))*0.5;
}
float occlusion(const float ZD) 
{
    //return (1.0 / (1.0 + ZD * ZD);
    return (1.0 / (1.0 + ZD*ZD));
}
//--------------------------------------------------------------------------
float AmbinetOcclusion(float2 Tex0, float depth, int samplecount)
{
	float fSampleingOffsetX = /*(0.5f) **/5* ( 1.0f / 1024);
	float fSampleingOffsetY = /*(0.5f) **/5* ( 1.0f / 768);

	float fAO = 0;
	int hsamplecnt = samplecount/2;
	float infl = saturate(1-depth);
	infl = saturate(infl);
	float rnd = rand(Tex0)*infl*3;
	float terminator = 1;
/*	for (int i=0; i<samplecount; i++)
	{
		for (int j=0; j<samplecount; j++)
		{
				float2 soffset = Tex0+float2(fSampleingOffsetX*(i-hsamplecnt)*rnd*infl, fSampleingOffsetY*(j-hsamplecnt)*rnd*infl);
			float sample_depth = tex2D(DepthTex, soffset).r;
			
			// get the (scaled) Z difference
			const float ZSCALE = 20000;
			//float ZSCALE = SpecilaParam.x;
			float ZD = ZSCALE * max(depth - sample_depth, 0);

			// quadratic falloff is both accurate
			// and it avoids the halo effect (mostly)
			const float MAX_DIFF = 0.005;
			const float NO_OCC = 1;
			fAO += (ZD < ZSCALE * MAX_DIFF ? occlusion(ZD) : NO_OCC);
		}
	}
	*/	
	fAO /= (samplecount*samplecount); 
	//float final = lerp(0.04, 0.4, fAO)*2.5;
	float final = lerp(0.01, 0.4, fAO)*2.5;
	
	final = min(final+0.3f, 1);
	final = pow(final, 1.2);
	return saturate(final);//;
}
//--------------------------------------------------------------------------
PS_OUTPUT AmbientOclusionFX(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 half4 Color = tex2D(BaseTex, Tex0);
 float value = 0.75f;
 float4 m = min(Color, 0.5);
 float4 n = max((Color - 0.5)*2, 0);
 float4 a = m+n;//m, 0.0);

 float3 DarkColor = float3(0.2,0.05,0);
    
 float3 result = lerp(a,Color ,0.5f);

 result.r = result.r * 1.06f;
 result.g = result.g * 1.04f;
 result.b = result.b * 0.90f;
 
 float3 resultB = result;


 //SpecilaParam.x
 float depthSample1 = tex2D(DepthTex , Tex0).r;
 float EdelDep = AmbinetOcclusion(Tex0, depthSample1, 4);

 output.diffuse = float4(resultB*EdelDep, 1);//float4(EdelDep, EdelDep, EdelDep, 1);// 

/*
 float depthSample1 = tex2D(DepthTex , Tex0).r;
 output.diffuse = float4(depthSample1, depthSample1, depthSample1, 1);
 */
 return output;
}

//------------------------------------------------------------------
float4 NormalDisplay( in float2 t : TEXCOORD0 ) : COLOR0
{
		return tex2D(BaseTex, t)*FrameBufferColor;
}
//------------------------------------------------------------------

//------------------------------------------------------------------
//Novo
//--------------------------------------------------------------------------
PS_OUTPUT PassThru(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

    half4 Color = tex2D(BaseTex, Tex0);

 
 output.diffuse = Color;

 
 return output;
}

PS_OUTPUT PlusBlur(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

  	float SampleOffset = 0.005859f*g_fScale.x;
    half4 Color = 0;
    float Eweight = 0;
    // this loop will be unrolled by compiler
    for(int i=0; i<11; i++) 
    {
		float offset = (i-5)*SampleOffset;
		float weight = 1.0f-(abs(i-5)*0.17f);
		Eweight += weight*2; 
    	Color += tex2D(BaseTex, float2(Tex0.x+offset, Tex0.y)) * weight;
    	Color += tex2D(BaseTex, float2(Tex0.x, Tex0.y+offset)) * weight;    	
   	} 
 
 output.diffuse = Color/Eweight;

 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT XBlur(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

  	float SampleOffset = 0.005859f*g_fScale.x;
    half4 Color = 0;
    float Eweight = 0;

    for(int i=0; i<11; i++) 
    {
		float offset = (i-5)*SampleOffset;
		float weight = 1.0f-(abs(i-5)*0.17f);
		Eweight += weight*2; 
    	Color += tex2D(BaseTex, float2(Tex0.x+offset, Tex0.y+offset)) * weight;
    	Color += tex2D(BaseTex, float2(Tex0.x+offset, Tex0.y-offset)) * weight;
   	}  	

 output.diffuse = Color/Eweight;
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT Sepia(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;
 float4 Color = 0;
 float3 scnColor = tex2D(BaseTex, Tex0);
 
    float3 grayXfer = float3(0.3,0.59,0.11);
    float3 LightColor = float3(1,0.9,0.5);
    float3 DarkColor = float3(0.2,0.05,0);
    
    float gray = dot(grayXfer,scnColor);
    float3 muted = lerp(scnColor,gray.xxx,g_fScale.x);
    float3 sepia = lerp(DarkColor,LightColor,gray);
    float3 result = lerp(muted,sepia,g_fScale.y);
    Color = float4(result,1);   

 output.diffuse = Color;
 
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT Underwater(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

  //Underwater2
  float2 offset = float2(0.2*g_fScale.y*sin(g_fTime*17+Tex0.y*g_fScale.x*90), g_fScale.y*sin(g_fTime*20+Tex0.x*g_fScale.x*60));
  half4 Color = tex2D(BaseTex, Tex0+offset);
  output.diffuse = Color;
	
 return output;
}


//--------------------------------------------------------------------------
PS_OUTPUT RedCorner(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

	half4 Color = tex2D(BaseTex, Tex0);
    float3 delta = float3(0.5,0.5,0) - float3(Tex0,0);
    float fall = 0.5f * g_fScale.x / dot(delta,delta);

	output.diffuse = Color + float4(1,0,0,0) *(1-saturate(fall));
	
 return output;
}

PS_OUTPUT Test1(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

 /*   
 //Tone
 half4 Color = tex2D(BaseTex, Tex0);
    half4 ColorBloom = pow(Color*2*g_fScale.y, 1+2*g_fScale.x);
    float Bright = 1.3*(Color.x + Color.y + Color.z)/3;

 output.diffuse = lerp(Color,ColorBloom,saturate(Bright-g_fScale.w));
 */
 /*
 //Bijela tocka
	half4 Color = tex2D(BaseTex, Tex0);
    float3 delta = float3(0.5,0.5,0) - float3(Tex0,0);
    float fall = 0.5f * g_fScale.x / dot(delta,delta);

	output.diffuse = Color + float4(1,1,1,1) *fall;
  */
  /*
  //Underwater1
    float2 offset = float2(0, g_fScale.y*sin(g_fTime*20+Tex0.x*g_fScale.x*60));
  half4 Color = tex2D(BaseTex, Tex0+offset);
  output.diffuse = Color;*/
  /*
  //Underwater2
  float2 offset = float2(0.2*g_fScale.y*sin(g_fTime*17+Tex0.y*g_fScale.x*90), g_fScale.y*sin(g_fTime*20+Tex0.x*g_fScale.x*60));
  half4 Color = tex2D(BaseTex, Tex0+offset);
  output.diffuse = Color;
  }*/
   /*
  //Underwater s krugom 
	float2 offset = float2(0.1*g_fScale.y*sin(g_fTime*17+Tex0.y*2*90), 0.5*g_fScale.y*sin(g_fTime*20+Tex0.x*2*60));
	half4 Color2 = tex2D(BaseTex, Tex0+offset);
  
  	half4 Color = tex2D(BaseTex, Tex0);
    float3 delta = float3(0.5,0.5,0) - float3(Tex0,0);
    float fall = 0.5f * g_fScale.x / dot(delta,delta);
	fall = saturate(fall);
	fall = fall*fall;
	output.diffuse = Color*(1-fall) + Color2 *fall;
	*/
	

	//float2 offset = float2(dist, dist);
	half4 Color = tex2D(BaseTex, Tex0);
	float a = 0;//Color.r;
	float df = abs(sin(g_fTime*37+Tex0.x*g_fScale.x*60+cos(Tex0.y*g_fTime*51)));//a+abs(sin(g_fTime*1));//sin(g_fTime*20));//  Tex0.x ++Tex0.x*2*60
	float dist = saturate(df);
	
	output.diffuse = Color + Color*dist*Color.x;
	
 return output;
}
//--------------------------------------------------------------------------
PS_OUTPUT Multiply(float2 Tex0 : TEXCOORD0)
{
 PS_OUTPUT output;

    half4 Color = tex2D(BaseTex, Tex0);
   float value = 0.75f;
  //  Color < 0.5 ? 2*Color*value : 1 - 2*(1-Color)*(1-value);
  float4 m = min(Color, 0.5);
  float4 n = max((Color - 0.5)*2, 0);// * 0.5) * 2.0;
 float4 a = m+n;//m, 0.0);

 output.diffuse = a;//Color*Color;

 
 return output;
}
//--------------------------------------------------------------------------
technique RenderScreen
{ 
 pass p0
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 PassThru();
 }
 pass p1
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 Underwater();//PShadeNormal
 }
 pass p2
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 Sepia();//PShadeHDR();
 }
 pass p3
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 PlusBlur();
 }
 pass p4
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 XBlur();
 }
 pass p5
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 RedCorner();
 }
 pass p6
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 Multiply();
 }
 
 pass p7
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 AmbientOclusionFX();
 }
 pass p8
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 PShadeBloom();
 }
 pass p9
 {      
  VertexShader = NULL;

  PixelShader = compile ps_2_0 NormalDisplay();
 }
 pass p10
 {
   VertexShader = NULL;

   PixelShader = compile ps_2_0 PShadeNormalLuminance();
 }
 pass p11
 {
   VertexShader = NULL;

   PixelShader = compile ps_2_0 PShadeNormalLuminanceDream();
 }

}
//--------------------------------------------------------
