// Laughing Mirror
// 
// Copyright Outerspace Software, all rights reserved

string description = "Laughing Mirror Effect";

float4x4	WorldViewProjection;
float4		DiffuseCol;
float		PI=3.14159;

float4 Prop0
<
string UIName="Laughing Mirror Size";
float UIMin=0;
float UIMax=1;
float3 UIDefault=float3(0.25,0.25,0);
int UISliders=2;
float UIScale=1;
bool UIInteger=false;
int UIWidget=0;
> = float4(0.25,0.25,0,0);

float4 Prop1
<
string UIName="Laughing Mirror Position";
float UIMin=0;
float UIMax=1;
float3 UIDefault=float3(0.5,0.5,0);
int UISliders=2;
float UIScale=1;
bool UIInteger=false;
int UIWidget=0;
> = float4(0.5,0.5,0,0);

float4 Prop2
<
string UIName="Laughing Mirror Magnification";
float UIMin=-1;
float UIMax=10;
float3 UIDefault=float3(2,0,0);
int UISliders=1;
float UIScale=1;
bool UIInteger=false;
int UIWidget=0;
> = float4(2,0,0,0);


texture MyTexture0;
sampler MyTextureSampler0 = sampler_state
{
   Texture = <MyTexture0>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = Wrap;
   AddressV  = Wrap;
};

struct VS_INPUT{
	float4 position:	POSITION;
	float3 normal:		NORMAL;
	float3 diffuse:		COLOR;
	float2 tex:		TEXCOORD0;
};

struct VS_OUTPUT{
    float4 position:	POSITION;
    float2 tex:		TEXCOORD0;
};

struct PS_OUTPUT{
    float4 color:	COLOR;
};

VS_OUTPUT VS(VS_INPUT input){
	VS_OUTPUT output;
	output.position = mul(input.position, WorldViewProjection);
	output.tex = input.tex.xy;
	return output;
}


PS_OUTPUT PS(VS_OUTPUT input){
	PS_OUTPUT output;

	float2 uv=input.tex;
	float dx=uv.x-Prop1.x;
	float dy=uv.y-Prop1.y;
	dx/=Prop0.x;
	dy/=Prop0.y;
	float r=sqrt(dx*dx+dy*dy);
	if(r<1){
		r=pow(r,Prop2.x);
		uv.x = Prop1.x + dx*r*Prop0.x;
		uv.y = Prop1.y + dy*r*Prop0.y;
		float4 c = tex2D(MyTextureSampler0, uv);
		output.color = c;
	}
	else{
	   	float4 c = tex2D(MyTextureSampler0, input.tex);
		output.color = c;
	}

	
	output.color.w *= DiffuseCol.w;

	return output;
}


technique Shader
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

technique ALphaChannel{
    pass p0{   
		VertexShader = NULL;
		PixelShader  = NULL;
    }
}

