!!ARBfp1.0

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


# LightIntensity = ((Diffuse.rgb + FresnelFactor * Specular.rgb) * SilkFactor + Transmission.rgb) * DistanceAttenuation * Projection

# Diffuse = DiffuseMap.rgb * DiffuseColor.rgb * Max(0, N*L)

# Specular = SpecularMap.rgb * SpecColor.rgb * (Max(0, H*N)^(SpecularMap.a * SpecColor.a) + Environment);

# Environment = EnvironmentMap.rgb * EnvColor.rgb * Max(0, N*L)

# Transmission = TransmissionMap.rgb * (TransmissionMap.a * Max(0, -N*L) + AttribMap.a * (1 - Abs(L*N - HSCenter))^HSExp )

# FresnelFactor = (1 - AttribMap.r) + AttribMap.r * (1 - H*V)^5

# SilkFactor = (1 - AttribMap.g) + AttribMap.g*(1 - V*N);

# Projection = DistanceAttenuation * ProjectionMap1.rgb + (1 - DistanceAttenuation) * ProjectionMap2.rgb

# DistanceAttenuation = (1 - |LightPos - PixelPos| / LightRange^2)^2

# N = Normal
# L = Light vector
# H = Half angle vector
# V = Eye vector

#-----------------------------------------
# Texture0 = Diffuse Map		// Default = 1,1,1,1
# Texture1 = Specular Map		// Default = 1,1,1,1
# Texture2 = Normal map			// Default = 1, 0.5, 0.5
# Texture3 = Attribute Map		// r = Fresnel, g = Silkness, b = ?, a = ?, Default = 0,0,0,0
# Texture4 = Transmission Diffuse Map	// Default = 0,0,0,0
# Texture5 = Projection Map 1		// Default = 1,1,1
# Texture6 = Projection Map 2		// Default = 1,1,1
# Texture7 = Environment Map		// Default = 0,0,0
# Texture8 = Anisotropic Direction Map	// Default = 1,0,0,0

# Texture10..15 = LFM

#-----------------------------------------
# TexCoord0 = Mapping tex coord
# TexCoord1 = LFM tex coord
# TexCoord2 = Animated model space pixel position
# TexCoord3 = Interpolated tangent space eye vector (IPTSEV)
# TexCoord4 = ProjMap tex coord
# TexCoord5 = Tangentspace-2-world transform, row 0
# TexCoord6 = Tangentspace-2-world transform, row 1
# TexCoord7 = Tangentspace-2-world transform, row 2

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

OUTPUT oCol = result.color;

#ATTRIB vCol = fragment.color;

ATTRIB MappingTexCoord = fragment.texcoord[0];
ATTRIB LFMTexCoord = fragment.texcoord[1];
#ATTRIB PixelPosition = fragment.texcoord[2];
ATTRIB IPTSEV = fragment.texcoord[3];
ATTRIB LMIntensityScale = fragment.texcoord[4];
#ATTRIB TS2W_Mat_0 = fragment.texcoord[5];
#ATTRIB TS2W_Mat_1 = fragment.texcoord[6];
#ATTRIB TS2W_Mat_2 = fragment.texcoord[7];

#PARAM LightPosition = program.env[0];	# { X, Y, Z, 0 }
#PARAM LightRange = program.env[1];	# { 1.0 / Range, Range, 1.0 / Range^2, Range^2 }
PARAM LightColor = program.env[2];	# { R, G, B, 0 } (0-2 range)
PARAM SpecColor = program.env[3];	# { R, G, B, SpecPower } (0-2 range)
#PARAM AttribScale = program.env[4];
#PARAM EnvMapColor = program.env[5];
#PARAM TransmissionColor = program.env[6];

PARAM const_val = { 0.5, 1.0, 2.0, 4.0 };
PARAM const_val2 = { 0, 0.25, 4, 0 };
PARAM const_val3 = { 1,2,1,0 };

TEMP DiffuseTexel;
TEMP SpecularTexel;
TEMP ProjMapTexel1;
TEMP ProjMapTexel2;
TEMP NormalMapTexel;
TEMP AttribTexel;
TEMP TransmissionTexel;
TEMP AnisoDirTexel;
TEMP EnvMapTexel;
TEMP LFM0Texel;
TEMP LFM1Texel;
TEMP LFM2Texel;
TEMP LFM3Texel;
TEMP LFM4Texel;
TEMP TSLV;
TEMP TSEV;		# Tangent space eye vector
TEMP H;
TEMP HT;		# Half angle vector projected onto tangent plane
TEMP R;
TEMP r0;
TEMP r1;
TEMP r2;
TEMP m;
TEMP F;
TEMP u;	# u = H * TSEV
TEMP t;	# t = H * N
TEMP v; # v = TSEV * N
TEMP v_nosat;
TEMP l; # l = TSLV * N
TEMP lt;
TEMP w;
TEMP Result;

#-----------------------------------------
# Fetch Textures
TEX DiffuseTexel, MappingTexCoord, texture[0], 2D;	# Sample diffusemap
TEX SpecularTexel, MappingTexCoord, texture[1], 2D;	# Sample diffusemap
TEX NormalMapTexel, MappingTexCoord, texture[2], 2D;	# Sample normalmap
#TEX AttribTexel, MappingTexCoord, texture[3], 2D;
#TEX TransmissionTexel, MappingTexCoord, texture[4], 2D;
#TEX AnisoDirTexel, MappingTexCoord, texture[8], 2D;
TEX LFM0Texel, LFMTexCoord, texture[10], 2D;
TEX LFM1Texel, LFMTexCoord, texture[11], 2D;
TEX LFM2Texel, LFMTexCoord, texture[12], 2D;
TEX LFM3Texel, LFMTexCoord, texture[13], 2D;

# LightFieldMap dynamic range scale
MUL LFM0Texel, LFM0Texel, LMIntensityScale.r;
MUL LFM1Texel, LFM1Texel, LMIntensityScale.r;
MUL LFM2Texel, LFM2Texel, LMIntensityScale.r;
MUL LFM3Texel, LFM3Texel, LMIntensityScale.r;

# Unbake lightfieldmap data
MOV LFM4Texel.r, LFM1Texel.a;
MOV LFM4Texel.g, LFM2Texel.a;
MOV LFM4Texel.b, LFM3Texel.a;

#TEX TSLV, IPTSLV, texture[3], CUBE;			# Normalize TSLV
#TEX TSEV, IPTSEV, texture[3], CUBE;			# Normalize TSLV
#MAD TSLV.rgb, TSLV, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
#MAD TSEV.rgb, TSEV, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)

#MAD NormalMapTexel.rgb, NormalMapTexel, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
#TEX NormalMapTexel.rgb, NormalMapTexel, texture[3], CUBE;			# Normalize normal
#MAD NormalMapTexel.rgb, NormalMapTexel, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)

#MUL AttribTexel.rgba, AttribTexel, AttribScale;

#-----------------------------------------
# Reconstruct normal from g,b components
MAD NormalMapTexel.rgba, NormalMapTexel, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
SWZ NormalMapTexel.rgb, NormalMapTexel, 0, a, g, 0;
DP3 NormalMapTexel.r, NormalMapTexel, NormalMapTexel;	# r = g^2 + b^2
SUB NormalMapTexel.r, const_val.y, NormalMapTexel.r;	# r = 1 - r;
RSQ NormalMapTexel.r, NormalMapTexel.r;			# r = 1/sqrt(r)
RCP NormalMapTexel.r, NormalMapTexel.r;			# r = 1/r = sqrt(1 - g^2 - b^2)

#MOV NormalMapTexel.r, -1;
#MOV NormalMapTexel.g, 0;
#MUL NormalMapTexel.rgb, NormalMapTexel, 0.25;
#ADD oCol.rgb, NormalMapTexel, 0.25;
#MAD oCol, NormalMapTexel, 0.5, 0.5;

#-----------------------------------------
# Diffuse
MOV_SAT r0.rgb, NormalMapTexel;
MOV_SAT r1.rgb, -NormalMapTexel;
MUL Result.rgb, r0.r, LFM0Texel;
MAD Result.rgb, r1.b, LFM1Texel, Result;
MAD Result.rgb, r1.g, LFM2Texel, Result;
MAD Result.rgb, r0.b, LFM3Texel, Result;
MAD Result.rgb, r0.g, LFM4Texel, Result;

MUL r1.rgb, LightColor, DiffuseTexel;		# Multiply diffusemap with light color
MUL Result.rgb, Result, r1;

#MOV Result,0;

#-----------------------------------------
DP3 r0.r, LFM0Texel, const_val3;
DP3 r0.b, LFM3Texel, const_val3;
DP3 r0.g, LFM4Texel, const_val3;
MOV r1.r, 0;
DP3 r1.b, LFM1Texel, const_val3;
DP3 r1.g, LFM2Texel, const_val3;
SUB r0.rgb, r0, r1;
@if support_normalize
NRM TSLV.rgb, r0.rgb;
@else
DP3 r0.w, r0, r0;
RSQ r0.w, r0.w;
MUL TSLV.rgb, r0, r0.w;
@endif

#-----------------------------------------
# Normalize TSEV
@if support_normalize
NRM TSEV.rgb, IPTSEV.rgb;
@else
DP3 TSEV.a, IPTSEV, IPTSEV;
RSQ TSEV.a, TSEV.a;
MUL TSEV.xyz, IPTSEV, TSEV.a;
@endif

#-----------------------------------------
# Calc eye-reflection vector
DP3 v_nosat.a, TSEV, NormalMapTexel;
ADD r0.a, v_nosat.a, v_nosat.a;
MAD R.xyz, NormalMapTexel, r0.a, -TSEV;

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

MOV_SAT r0.rgb, R;
MOV_SAT r1.rgb, -R;
MUL r2.rgb, r0.r, LFM0Texel;
MAD r2.rgb, r1.b, LFM1Texel, r2;
MAD r2.rgb, r1.g, LFM2Texel, r2;
MAD r2.rgb, r0.b, LFM3Texel, r2;
MAD r2.rgb, r0.g, LFM4Texel, r2;
MUL r2.rgb, r2, 2;
MUL SpecularTexel.rgb, r2, SpecularTexel;

#-----------------------------------------
# Calc halfangle vector
#ADD H.xyz, TSEV, TSLV;
#DP3 H.a, H, H;
#RSQ H.a, H.a;
#MUL H.xyz, H, H.a;
#DP3_SAT t.a, H, NormalMapTexel;

#-----------------------------------------
# Specular + EnvMap
MUL SpecularTexel, SpecularTexel, SpecColor;
DP3_SAT r1.a, TSLV, R;
POW r1.a, r1.a, SpecularTexel.a;
MAD Result.rgb, SpecularTexel, r1.a, Result;

# Alpha from diffuse texture
MOV Result.a, DiffuseTexel.a;
#MUL Result.r, w.a, 0.5;

#MOV Result.g, 0.5;
#MUL r0.rgb, 0.25, t.a;

# Write result
MOV oCol, Result;
#MUL oCol, LFM0Texel, 1;
#MAD oCol, R, 0.25, 0.25;
#MOV oCol, { 0.5, 0, 0, 0 };
#MUL oCol, AnisoDirTexel, 0.5;

END
