!!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]		Framebuffer
#	texture[1]		Distortion map
#
# 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, _, _, _ }
#
# Register controled parameters:
#   program.env[3]	{ Speed, Frequency, _, _ }
#
#-----------------------------------

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];				# { Speed, Frequency, _, _ }

# Constants
PARAM const_val0 = { 0, 0.5, 1, 2 };	# Used (xyzw)

# Registers
TEMP t0;
TEMP r0;
TEMP r1;

TEMP r2;
TEMP r3;
TEMP r4;

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

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

# Create vector 1,0,tc1.z,1
MAD r2, const_val0.xxzx, tc1.zzzz, const_val0.zxxz;

DP4 r3, r2, c3;
DP4 r4, r2, c4;

MAX r4, r4, 1;

RCP r4, r4.w;
MUL r3, r3, r4;

MIN r3, r3, 0.02;
MUL r3, r3, p0.y;

# Scroll coords and load normal map texel
MOV r1, tc0;
MAD r1.y, p0.x, c2.x, r1.y;
TEX r1, r1, texture[1], 2D;

# Reconstruct from b,g component
MAD r1.rgba, r1, const_val0.w, -const_val0.z;
SWZ r1.rgb, r1, 0, a, r, 0;
DP3 r1.r, r1, r1;
SUB r1.r, const_val0.z, r1.r;
RSQ r1.r, r1.r;
RCP r1.r, r1.r;

MOV r1.x, const_val0.x;

# Offset 
MAD_SAT r0, r1, r3, r0;

# Clamp inside framebuffer coordinates
LRP r0.xy, r0.xyxy, c1.zwxy, c1.xyzw;

# Load texel
TEX t0, r0, texture[0], 2D;
MOV oCol, t0;

# Debug color
#MAD oCol, r0, const_val0.zzxx, const_val0.xxxz;

END

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