!!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

#-----------------------------------------
# TexCoord0 = Mapping tex coord
# TexCoord1 = Animated model space pixel position
# TexCoord2 = Interpolated tangent space light vector (IPTSLV)
# 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 PixelPosition = fragment.texcoord[1];
ATTRIB IPTSLV = fragment.texcoord[2];
ATTRIB IPTSEV = fragment.texcoord[3];
ATTRIB ProjMapTexCoord = 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, SpecAnisotrophy } 
PARAM SpecularColor = program.env[3];	# { R, G, B, SpecPower }
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.125 };

TEMP DiffuseTexel;
TEMP SpecularTexel;
TEMP ProjMapTexel1;
TEMP ProjMapTexel2;
TEMP NormalMapTexel;
TEMP AttribTexel;
TEMP TransmissionTexel;
TEMP AnisoDirTexel;
TEMP EnvMapTexel;
TEMP TSLV;		# Tangent space light vector
TEMP TSEV;		# Tangent space eye vector
TEMP H;
TEMP HT;		# Half angle vector projected onto tangent plane
TEMP R;
TEMP r0;
TEMP r1;
TEMP m;
TEMP Attenuation;
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 ProjMapTexel1, ProjMapTexCoord, texture[5], CUBE;
TEX ProjMapTexel2, ProjMapTexCoord, texture[6], CUBE;

#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;

#-----------------------------------------
# Attenuation
SUB r1, LightPosition, PixelPosition;
DP3 r1.w, r1, r1;
#DP3 r1.w, IPTSLV, IPTSLV;
MUL_SAT r1.w, r1.w, LightRange.z;
ADD r1.w, const_val.g, -r1.w;
MUL Attenuation.w, r1.w, r1.w;

#-----------------------------------------
# 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)

#-----------------------------------------
# Normalize TSLV
@if support_normalize
NRM TSLV.xyz, IPTSLV.xyz;
@else
DP3 TSLV.a, IPTSLV, IPTSLV;
RSQ TSLV.a, TSLV.a;
MUL TSLV.xyz, IPTSLV, TSLV.a;
@endif

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

#-----------------------------------------
# Calc halfangle vector
ADD H.xyz, TSEV, TSLV;
@if support_normalize
NRM H.xyz, H.xyz;
@else
DP3 H.a, H, H;
RSQ H.a, H.a;
MUL H.xyz, H, H.a;
@endif

#-----------------------------------------
# Normalize anisotropic direction (yes this is necessary)
MAD AnisoDirTexel.rgb, AnisoDirTexel, 2, -1;
@if support_normalize
NRM AnisoDirTexel.xyz, AnisoDirTexel.xyz;
@else
DP3 r1.a, AnisoDirTexel, AnisoDirTexel;
RSQ r1.a, r1.a;
MUL AnisoDirTexel.xyz, AnisoDirTexel, r1.a;
@endif

DP3_SAT l.a, TSLV, NormalMapTexel;
#-----------------------------------------
# Create a plane = H x A
XPD r1.xyz, H, AnisoDirTexel;
@if support_normalize
NRM r1.xyz, r1.xyz;
@else
DP3 r1.a, r1, r1;
RSQ r1.a, r1.a;
MUL r1.xyz, r1, r1.a;
@endif

# Project normal onto plane
DP3 r0.a, r1, NormalMapTexel;
MAD r0.xyz, -r0.a, r1, NormalMapTexel;

# New normal is lerped between original normal and projected normal depending on desired anisotrophy
POW r0.a, l.a, const_val2.w;
MUL r1.a, LightColor.a, AnisoDirTexel.a;
MUL r1.a, r1.a, r0.a;
LRP r1.rgb, r1.a, r0, NormalMapTexel;
@if support_normalize
NRM NormalMapTexel.rgb, r1.rgb;
@else
DP3 r1.a, r1, r1;
RSQ r1.a, r1.a;
MUL NormalMapTexel.rgb, r1, r1.a;
@endif
DP3_SAT l.a, TSLV, NormalMapTexel;

#-----------------------------------------
DP3_SAT u.a, H, TSEV;
DP3_SAT t.a, H, NormalMapTexel;
DP3 v_nosat.a, TSEV, NormalMapTexel;
DP3_SAT lt.a, TSLV, -NormalMapTexel;
MOV_SAT v.a, v_nosat.a;

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

#-----------------------------------------
# Environment mapping
DP3 r0.r, R, TS2W_Mat_0;			# Transform reflection vector to world space
DP3 r0.g, R, TS2W_Mat_1;
DP3 r0.b, R, TS2W_Mat_2;
TEX EnvMapTexel, r0, texture[7], CUBE;

#-----------------------------------------
# Self shadowing
SUB r0.a, const_val2.y, -TSLV.x;
MUL_SAT Attenuation.r, r0.a, const_val2.z;

#-----------------------------------------
# Fresnel
MOV AttribTexel.r, 0.0;
MOV AttribTexel.g, 0.0;

SUB_SAT r0.a, 1, v.a;
MUL r0.r, r0.a, r0.a;
#MUL r0.r, r0.r, r0.r;
#MUL r0.a, r0.r, r0.a;
LRP F.a, AttribTexel.r, r0.r, 1;

#-----------------------------------------
# Silkness factor
SUB r0.a, 1, v.a;
LRP r0.a, AttribTexel.g, r0.a, 1;
MUL Attenuation.r, Attenuation.r, r0.a;

#-----------------------------------------
# Diffuse
MUL r1.rgb, LightColor, DiffuseTexel;		# Multiply diffusemap with light color
MUL Result.rgb, l.a, r1;			# Diffuse color * Diffuse dotprod

#-----------------------------------------
# Specular + EnvMap
MUL SpecularTexel.a, SpecularTexel.a, SpecularColor.a;
#MOV SpecularTexel.a, 512;
POW r1.a, t.a, SpecularTexel.a;
MUL r1.rgb, SpecularColor, r1.a;
SUB_SAT EnvMapTexel.rgb, EnvMapTexel, EnvMapColor.a;
MUL EnvMapTexel.rgb, EnvMapTexel, l.a;
MAD r1.rgb, EnvMapTexel, EnvMapColor, r1;
MUL r1.rgb, r1, F.a;				# Mul specular with fresnel factor
MAD Result.rgb, SpecularTexel, r1, Result;	# Mul specular with specular color, add to final fragment

#-----------------------------------------
MUL Result.rgb, Result, Attenuation.r;			# Multiply final fragment by geometric attenuation and silkness

#-----------------------------------------
# Transmitted light
DP3 r1.a, TSLV, NormalMapTexel;
MAX r1.a, r1.a, -r1.a;
SUB_SAT r1.a, 1.0, r1.a;
POW r1.a, r1.a, TransmissionColor.a;
MUL r1.a, r1.a, TransmissionColor.a;
#MAD r1.a, r1.a, TransmissionColor.a, lt.a;
MUL TransmissionTexel.rgb, TransmissionColor, TransmissionTexel;
#MAD Result.rgb, r1.a, TransmissionTexel, Result;

#-----------------------------------------
MUL Result.rgb, Result, Attenuation.w;			# Multiply final fragment by distance attenuation

#-----------------------------------------
# Projection map
#LRP ProjMapTexel1.rgb, Attenuation.w, ProjMapTexel1, ProjMapTexel2;
MUL Result.rgb, Result, ProjMapTexel1;			# Multiply final fragment by projmap

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

#MOV r0.rgb, l.a;
#MUL r0.rgb, 0.25, t.a;

# Write result
MOV oCol, Result;
#MUL oCol, t.a, 0.1;

#MOV oCol, { 0.5, 0, 0, 0 };
#MUL oCol, AnisoDirTexel, 0.5;

END
