/*ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\ File: Program for CXR_Shader::RenderShading_FP20 Author: Magnus Högdahl Copyright: Starbreeze AB 2004 History: Comments: ----------------------------------------- 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 = Environment Map // Default = 0,0,0 ----------------------------------------- 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 ----------------------------------------- \*____________________________________________________________________________________________*/ *flags { *envmap 0x0001 } *generate { *gen 0 *gen envmap } *program { *header "!!ARBfp1.0 " *declarations " OUTPUT oCol = result.color; #ATTRIB vCol = fragment.color; ATTRIB MappingTexCoord = fragment.texcoord[0]; ATTRIB LFMTexCoord = fragment.texcoord[1]; ATTRIB IPTSEV = fragment.texcoord[3]; ATTRIB TS2W_Mat_0 = fragment.texcoord[5]; ATTRIB TS2W_Mat_1 = fragment.texcoord[6]; ATTRIB TS2W_Mat_2 = fragment.texcoord[7]; PARAM WSLV = program.env[0]; # { X, Y, Z, 0 } PARAM LightColor = program.env[1]; # { R, G, B, 0 } (0-2 range) PARAM SpecularColor = program.env[2]; # { R, G, B, SpecPower } (0-2 range) PARAM LFM0Texel = program.env[3]; PARAM LFM1Texel = program.env[4]; PARAM LFM2Texel = program.env[5]; PARAM LFM3Texel = program.env[6]; PARAM LFM4Texel = program.env[7]; PARAM LFM5Texel = program.env[8]; PARAM LFMSpec = program.env[9]; # Next param is envmap way below PARAM const_val = { 0.5, 1.0, 2.0, 4.0 }; PARAM const_val2 = { 0, 0.25, 4, 0 }; TEMP DiffuseTexel; TEMP SpecularTexel; TEMP NormalMapTexel; TEMP NormalMapTexelW; #TEMP WSLV; TEMP TSEV; # Tangent space eye vector TEMP WSEV; # World space eye vector TEMP WSH; 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; TEMP Diffuse; " *do1 " #----------------------------------------- # Fetch Textures @if dynmip TEXDYN DiffuseTexel, MappingTexCoord, texture[0], 2D; # Sample diffusemap TEXDYN SpecularTexel, MappingTexCoord, texture[1], 2D; # Sample diffusemap TEXDYN NormalMapTexel, MappingTexCoord, texture[2], 2D; # Sample normalmap @else TEX DiffuseTexel, MappingTexCoord, texture[0], 2D; # Sample diffusemap TEX SpecularTexel, MappingTexCoord, texture[1], 2D; # Sample diffusemap TEX NormalMapTexel, MappingTexCoord, texture[2], 2D; # Sample normalmap @endif #TEX AttribTexel, MappingTexCoord, texture[3], 2D; #TEX TransmissionTexel, MappingTexCoord, texture[4], 2D; #TEX AnisoDirTexel, MappingTexCoord, texture[8], 2D; #----------------------------------------- # 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) #----------------------------------------- # Rotate normal into world-space DP3 NormalMapTexelW.r, NormalMapTexel, TS2W_Mat_0; # Transform reflection vector to world space DP3 NormalMapTexelW.g, NormalMapTexel, TS2W_Mat_1; DP3 NormalMapTexelW.b, NormalMapTexel, TS2W_Mat_2; #----------------------------------------- # Diffuse MOV_SAT r0.rgb, NormalMapTexelW; MOV_SAT r1.rgb, -NormalMapTexelW; MUL Result.rgb, r1.r, LFM0Texel; MAD Result.rgb, r1.g, LFM2Texel, Result; MAD Result.rgb, r1.b, LFM4Texel, Result; MAD Result.rgb, r0.r, LFM1Texel, Result; MAD Result.rgb, r0.g, LFM3Texel, Result; MAD Result.rgb, r0.b, LFM5Texel, Result; MOV Diffuse.rgb, Result; MUL r1.rgb, LightColor, DiffuseTexel; # Multiply diffusemap with light color MUL Result.rgb, Result, r1; #----------------------------------------- # 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 DP3 WSEV.r, TSEV, TS2W_Mat_0; # Transform eye vector to world space DP3 WSEV.g, TSEV, TS2W_Mat_1; DP3 WSEV.b, TSEV, TS2W_Mat_2; #----------------------------------------- #----------------------------------------- # Calc halfangle vector ADD WSH.xyz, WSEV, -WSLV; @if support_normalize NRM WSH.xyz, WSH.xyz; @else DP3 WSH.a, WSH, WSH; RSQ WSH.a, WSH.a; MUL WSH.xyz, WSH, WSH.a; @endif #----------------------------------------- # Specular + EnvMap #----------------------------------------- # Self shadowing for specular MOV r1.x, TS2W_Mat_0.x; MOV r1.y, TS2W_Mat_1.x; MOV r1.z, TS2W_Mat_2.x; DP3_SAT r0.a, r1, WSLV; SUB_SAT r0.a, 0.25, r0.a; MUL_SAT r0.a, r0.a, 4; DP3_SAT t.a, WSH, NormalMapTexelW; MUL SpecularTexel.a, SpecularTexel.a, SpecularColor.a; MOV SpecularTexel.a, SpecularColor.a; POW r1.a, t.a, SpecularTexel.a; MUL r1.a, r1.a, r0.a; MUL r1.rgb, SpecularColor, r1.a; MUL r1.rgb, r1, LFMSpec; MAD Result.rgb, SpecularTexel, r1, Result; # Mul specular with specular color, add to final fragment " *if_envmap " PARAM EnvMapColor = program.env[10]; # { R, G, B, SpecPower } (0-2 range) TEMP EnvMapTexel; TEMP RW; # Calc world space eye-reflection vector DP3 v_nosat.a, WSEV, NormalMapTexelW; ADD r0.a, v_nosat.a, v_nosat.a; MAD RW.xyz, NormalMapTexelW, r0.a, -WSEV; TEX EnvMapTexel, RW, texture[3], CUBE; SUB_SAT EnvMapTexel.rgb, EnvMapTexel, EnvMapColor.a; MUL EnvMapTexel.rgb, EnvMapTexel, Diffuse; MUL EnvMapTexel.rgb, EnvMapTexel, SpecularTexel; MAD Result.rgb, EnvMapTexel, EnvMapColor, Result; " *do2 " # Alpha from diffuse texture MOV Result.a, DiffuseTexel.a; # Write result MOV oCol, Result; END " }