string Description = "This shader produces a transparent and reflective effect on an object with a choosable color";
string Thumbnail = "Colored Glass.png";

float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4 eyePos : CameraPosition;

float4 diffuse : COLOR = { 0.501961, 1.000000, 0.341176, 0.000000 };

float Opacity
<
	string UIWidget = "slider";
	float UIMax = 1.0;
	float UIMin = 0.0;
	float UIStep = 0.01;
> = 0.250000;

texture Background : DIFFUSE
<
	string ResourceName = "";
	string Type = "2D";
	string Dynamic = "Background";
>;

sampler texture1 = sampler_state
{
	Texture = <Background>;
	MinFilter = Anisotropic;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = Clamp;
	AddressV = Clamp;
};

texture ReflectionTex
<
	string ResourceName = "";
	string ResourceType = "Cube";
	string Dynamic = "Reflection"; 
>;

samplerCUBE Reflection_smp = sampler_state 
{
	Texture = <ReflectionTex>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct app_in
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
	float3 normal : NORMAL;
};

struct vs_out
{
	float4 pos : POSITION;
	float4 projUV : TEXCOORD0;
	float3 normal : TEXCOORD1;
	float3 viewDir : TEXCOORD2;
};

vs_out DefaultVertexShader( app_in IN )
{
	vs_out OUT;
	
	OUT.pos = mul( IN.pos, WorldViewProj );
	
	float4 wPos = mul( IN.pos, World );
	float3 wNormal = mul( IN.normal, (float3x3)World );
	
	OUT.normal = normalize( wNormal );
	OUT.viewDir = normalize( eyePos.xyz - wPos.xyz );
	
	OUT.projUV = float4( OUT.pos.x*0.5 + 0.5*OUT.pos.w, 0.5*OUT.pos.w - OUT.pos.y*0.5, OUT.pos.w, OUT.pos.w );
	
	return OUT;
}

float4 DefaultPixelShader( vs_out IN ) : COLOR
{
	float3 Dir = reflect( -IN.viewDir, IN.normal );
	float4 refl = texCUBE( Reflection_smp, Dir );
	refl = refl*refl*4;
	
	float4 bkgnd = lerp( tex2Dproj( texture1, IN.projUV ), float4(1,1,1,1), Opacity );
	
	float angle = 1 - saturate( dot( IN.normal, IN.viewDir ) );
	angle = 1 - angle*angle;
	float4 color = lerp( refl, bkgnd*diffuse, angle );
	
	return float4(color.xyz,1);
}

technique MyShader
{
	pass p0
	{
		VertexShader = compile vs_1_1 DefaultVertexShader( );
		PixelShader = compile ps_1_4 DefaultPixelShader( );
	}
}
