!!ARBfp1.0
OPTION ARB_precision_hint_fastest;

#-----------------------------------
#
# EffectSystem Shader Object:
#
#		All effect systems shaders has predefines reserved in
#		program.env[0 - 2]. Some of the predefines can be controled
#		by setting flags in the effect object such as no animation etc.
#
#
# Reserved texture coordinates:
#	texcoord[1]		Generated coords for screen space calc.
#
# Texture coordinates:
#   texcoord[0]		Object texture coordinates
#
# Textures:
#   texture[0]		Depthbuffer
#	texture[1]		Diffuse Texture
#
# Predefines program parameters:
#   program.env[0]	{ 1/ScrW, 1/ScrH, (ScrW-1)/ScrTexW, (ScrH-1)/ScrTexH }
#	program.env[1]	{ MinX, MinY, MaxX, MaxY }
#   program.env[2]	{ Time, FrontPlane, BackPlane, _ }
#   program.env[3]	{ TransposedProjMatRow 0 }
#   program.env[4]	{ TransposedProjMatRow 3 }
#
# Register controled parameters:
#   program.env[5]	{ 1/Radius, _, _, _ }
#	program.env[6]	{ Red, Green, Blue, Alpha }
#
#-----------------------------------

OUTPUT oCol = result.color;

# Texture coordinates
ATTRIB tc0 = fragment.texcoord[0];
ATTRIB tc1 = fragment.texcoord[1];

# Predefines
PARAM c0 = program.env[0];
PARAM c1 = program.env[1];
PARAM c2 = program.env[2];
PARAM c3 = program.env[3];
PARAM c4 = program.env[4];

# Program parameters
PARAM p0 = program.env[5];				# { FadeOut, _, _, _ }
PARAM p1 = program.env[6];				# { Color RGBA }

# Constants
PARAM const_val0 = { 0, 0.5, 0.99609375, 0.00390625 };	# Used (xyzw)

# Registers
TEMP r0;
TEMP r1;
TEMP r2;
TEMP r3;

TEMP t1;
TEMP FBP;	# FrontBackPlane

#------------------------------------------------

# Load color texel
TEX t1, tc0, texture[1], 2D;

# Calculate screen texcoord in 0-1 range
RCP r0.w, tc1.w;
MUL r0.xyz, tc1, r0.w;
#;MOV FBP.w, r0.z;
MAD r0, r0.xyzw, c0.xyzw, const_val0.yyxx;

# Clamp inside depthbuffer coordinates and load depth/depthstencil texel
LRP r0.xy, r0.xyxy, c1.zwxy, c1.xyzw;
TEX r0, r0, texture[0], 2D;

# Calc raw depth value
MUL r0.rgb, r0, const_val0.z;
MAD r0.a, r0.g, const_val0.w, r0.r;

# Move these calculations to parameters perhaps?
SUB FBP.x, c2.z, c2.y;		# BackPlane - FrontPlane
MUL FBP.z, c2.z, c2.y;		# BackPlane * FrontPlane

# Convert depth buffer and fragment to z
MAD r1.x, r0.a, FBP.x, -c2.z;
RCP r1.y, -r1.x;
MUL r1.z, r1.y, FBP.z;

RCP r3.y, tc1.w;
MUL r3.y, r3.y, tc1.z;
MAD r3.y, r3.y, const_val0.y, const_val0.y;
MAD r3.y, r3.y, FBP.x, -c2.z;
RCP r3.z, -r3.y;
#;MUL r3.w, r3.z, FBP.z;
MAD r3.w, r3.z, FBP.z, -const_val0.w;

# Calculate z difference
SUB r1.w, r1.z, r3.w;

# Make sure z difference is positive and linearly interpolate it between 0 and "radius"
ABS r2.x, r1.w;
MUL_SAT r2.x, r2.x, p0.x;

# Blend fragment map and color togheter and multiply with strength
MUL_SAT r1, t1, p1;
MUL_SAT oCol, r1, r2.x;
MOV oCol.a, r1.a;

END

#------------------------------------------------