!!ARBfp1.0
#OPTION ARB_precision_hint_fastest;

#/*ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\
#	File:			Program for darkness vision
#					
#	Author:			Magnus Högdahl
#					
#	Copyright:		Starbreeze AB 2004
#					
#	History:
#
#\*____________________________________________________________________________________________*/

#-----------------------------------------
OUTPUT oCol = result.color;

ATTRIB v0 = fragment.color;
ATTRIB tc0 = fragment.texcoord[0];
ATTRIB tc1 = fragment.texcoord[1];
ATTRIB tc2 = fragment.texcoord[2];
ATTRIB tc3 = fragment.texcoord[3];

PARAM VPParam = program.env[0]; # { F, B, 1/B, ... }
PARAM VPConst = program.env[1];	# { 2B, B+F, 2BF, 2(B-F) }
PARAM dUV0 = program.env[2];
PARAM dUV1 = program.env[3];
PARAM dUV2 = program.env[4];
PARAM dUV3 = program.env[5];
PARAM Time = program.env[6];

PARAM const_val = { 1.0, 1.0, 1.0, 1.0 };
PARAM const_val2 = { 0.5, 0.0005, 0.0, 0.0 };

TEMP t0;
TEMP t1;
TEMP t2;
TEMP t3;
TEMP tc_depth0;
TEMP tc_depth1;
TEMP tc_depth2;
TEMP tc_depth3;
TEMP td0;
TEMP td1;
TEMP td2;
TEMP td3;
TEMP tdfracb;

TEMP r0;
TEMP PixelPosV;		# View space pixel position
TEMP PixelPosW;
TEMP FogPosW;
TEMP r1;
TEMP r2;
TEMP r3;
TEMP r4;
TEMP r5;
#-----------------------------------------

TEX t0, tc0, texture[0], 2D;
TEX t1, tc0, texture[1], 2D;
ADD tc_depth0, tc0, dUV0;
ADD tc_depth1, tc0, dUV1;
ADD tc_depth2, tc0, dUV2;
ADD tc_depth3, tc0, dUV3;
TEX td0, tc_depth0, texture[1], 2D;
TEX td1, tc_depth1, texture[1], 2D;
TEX td2, tc_depth2, texture[1], 2D;
TEX td3, tc_depth3, texture[1], 2D;

#-----------------------------------------
@if xenon
MOV td0.y, td1.x;
MOV td0.z, td1.x;
MOV td0.w, td1.x;
MOV t1.a, t1.r;
SUB td0, const_val.w, td0;
SUB t1.a, const_val.w, t1.a;

@else
MUL t1.rgb, t1, 0.99609375;
MAD t1.a, t1.g, 0.00390625, t1.r;
MAD t1.a, t1.b, 0.0000152587890625, t1.a;
MUL td0.rgb, td0, 0.99609375;
MUL td1.rgb, td1, 0.99609375;
MUL td2.rgb, td2, 0.99609375;
MUL td3.rgb, td3, 0.99609375;
MOV tdfracb.x, td0.b;
MOV tdfracb.y, td1.b;
MOV tdfracb.z, td2.b;
MOV tdfracb.w, td3.b;
MAD td0.r, td0.g, 0.00390625, td0.r;
MAD td0.g, td1.g, 0.00390625, td1.r;
MAD td0.b, td2.g, 0.00390625, td2.r;
MAD td0.a, td3.g, 0.00390625, td3.r;
MAD td0.rgba, tdfracb, 0.0000152587890625, td0;

@endif

MAD td0, td0, VPConst.w, -VPConst.x;
RCP td0.x, td0.x;
RCP td0.y, td0.y;
RCP td0.z, td0.z;
RCP td0.w, td0.w;
MUL td0, td0, -VPConst.z;

#(z*2 - 1)*(B-F)-(F+B) = 2(B-F)z + F-B-F-B = 2(B-F)z - 2B

# Convert depth to linear z
MAD r2.y, t1.a, VPConst.w, -VPConst.x;
RCP r2.y, r2.y;
MUL t1.a, r2.y, -VPConst.z;

SUB r0, td0, t1.a;
MAX r0, r0, -r0;
MAX r0.x, r0.x, r0.y;
MAX r0.z, r0.z, r0.w;
MAX r0.x, r0.x, r0.z;
SUB r0.x, r0.x, 0;
MAX r0.x, r0.x, 0;

MUL_SAT r2.rgb, r0.x, 0.04;

MOV r1.x, tc0.x;
MOV r1.x, 0;
MUL r1.y, t1.a, -0.0001;
MAD r1.y, Time.x, 4, r1.y;
TEX t2, r1, texture[2], 2D;
MAD r1.y, Time.x, 5, r1.y;
TEX t3, r1, texture[2], 2D;
MAD_SAT t2, t2, 2, -0.25;
MAD_SAT t3, t3, 4, -3.5;
MUL t2, t2, 2;
MUL t3, t3, 8;
ADD t2.r, t2, t3;
MUL r2.rgb, r2, t2.r;

# Distance fall off
MUL_SAT r0.x, t1.a, const_val2.y;
MUL r0.x, r0.x, r0.x;
SUB r0.x, 1, r0.x;
MUL r2.rgb, r2, r0.x;

#MUL r2, t1.a, 0.01;
MOV oCol, r2;

END
