home *** CD-ROM | disk | FTP | other *** search
ARB Fragment shader | 2004-12-21 | 10.5 KB | 322 lines |
- !!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 - |Ligr1os - 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
-
- #-----------------------------------------
- # 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 Ligr1osition = 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 const_val = { 0.5, 1.0, 2.0, 4.0 };
- PARAM const_val2 = { 0, 0.25, 4, 0 };
-
- TEMP DiffuseTexel;
- TEMP SpecularTexel;
- TEMP ProjMapTexel1;
- TEMP ProjMapTexel2;
- TEMP NormalMapTexel;
- TEMP AttribTexel;
- TEMP TransmissionTexel;
- 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 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, Ligr1osition, 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;
-
- #-----------------------------------------
- # 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)
- DP3 r0.a, NormalMapTexel, NormalMapTexel;
- RSQ r0.a, r0.a;
- MUL NormalMapTexel.rgb, NormalMapTexel, r0.a;
-
- #-----------------------------------------
- # Normalize TSLV
- DP3 TSLV.a, IPTSLV, IPTSLV;
- RSQ TSLV.a, TSLV.a;
- MUL TSLV.xyz, IPTSLV, TSLV.a;
-
- #-----------------------------------------
- # Normalize TSEV
- DP3 TSEV.a, IPTSEV, IPTSEV;
- RSQ TSEV.a, TSEV.a;
- MUL TSEV.xyz, IPTSEV, TSEV.a;
-
- #-----------------------------------------
- # Calc halfangle vector
- ADD H.xyz, TSEV, TSLV;
- DP3 H.a, H, H;
- RSQ H.a, H.a;
- MUL H.xyz, H, H.a;
-
- #-----------------------------------------
- # Normalize anisotropic direction (yes this is necessary)
- MAD TransmissionTexel.rgb, TransmissionTexel, 2, -1;
- DP3 TransmissionTexel.a, TransmissionTexel, TransmissionTexel;
- RSQ TransmissionTexel.a, TransmissionTexel.a;
- MUL TransmissionTexel.xyz, TransmissionTexel, TransmissionTexel.a;
-
- DP3_SAT l.a, TSLV, NormalMapTexel;
- #-----------------------------------------
- # Create a plane = H x A
- XPD r1.xyz, H, TransmissionTexel;
- DP3 r1.a, r1, r1;
- RSQ r1.a, r1.a;
- MUL r1.xyz, r1, r1.a;
-
- # 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
- ADD r0.a, 1, -l.a;
- MUL r0.a, r0.a, r0.a;
- MUL r0.a, r0.a, r0.a;
- ADD r0.a, 1, -r0.a;
- MUL r0.a, 0.8, r0.a;
- LRP NormalMapTexel, r0.a, r0, NormalMapTexel;
- DP3 r0.a, NormalMapTexel, NormalMapTexel;
- RSQ r0.a, r0.a;
- MUL NormalMapTexel.rgb, NormalMapTexel, r0.a;
-
- #-----------------------------------------
- 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;
-
- #-----------------------------------------
- # Calc HT
- MOV HT.gb, H;
- MOV HT.r, 0;
- DP3 HT.a, HT, HT;
- RSQ HT.a, HT.a;
- MUL HT.xyz, HT, HT.a;
-
- #-----------------------------------------
- # Calc w, cosine of angle between eye vector and anisotropic direction
- DP3 w.a, HT, TransmissionTexel;
- #DP3 w.a, HT, { 1, 0, 0 };
-
- #-----------------------------------------
- # 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;
-
- SUB_SAT r0.a, 1, u.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 r1.rgb, r1, const_val.b; # Scale by 2 for correct brightness
- MUL Result.rgb, l.a, r1; # Diffuse color * Diffuse dotprod
-
- #-----------------------------------------
- # Anisotropic specular
- # D = 1 / (mn(p - pt^2 + t^2)^2) = 1 / (mn(p + (1-p)*t^2)^2)
- # p = w^2/m^2 + (1-w^2)/n^2
-
- MUL m, SpecularTexel.a, 0.25;
- MOV m, 0.05;
- MUL m.g, m.r, 0.2;
-
- MUL r0.rg, m, m; # r0.rg = { m^2, n^2 }
- RCP r0.r, r0.r; # r0.r = 1/m^2
- RCP r0.g, r0.g; # r0.g = 1/n^2
- MUL r1.r, w.a, w.a;
- LRP r0.g, r1.r, r0.r, r0.g; # r0.g = p = w^2/m^2 + (1-w^2)/n^2
-
- MUL r0.a, t.a, t.a;
- LRP r0.a, r0.g, 1, r0.a; # r0.a = (p + (1-p)*t^2)
- MUL r0.a, r0.a, r0.a;
- MUL r0.r, m.g, m.r;
- MUL r0.a, r0.a, r0.r;
- RCP r1.a, r0.a; # 1 / (mn*(p + (1-p)*t^2)^2)
- MUL r1.a, r1.a, 0.125;
-
- #-----------------------------------------
- # Anisotropic specular v2
- MOV m, 1048;
- MUL m.g, m.r, 0.02;
- MUL r1.r, w.a, w.a;
- LRP r1.r, r1.r, m.r, m.g; # r0.g = p = w^2/m^2 + (1-w^2)/n^2
- #POW r1.a, t.a, r1.r;
-
- #-----------------------------------------
- # Specular + EnvMap
- #MUL SpecularTexel.a, SpecularTexel.a, SpecColor.a;
- MOV SpecularTexel.a, 512;
- POW r1.a, t.a, SpecularTexel.a;
- MAD r1.rgb, EnvMapTexel, EnvMapColor, r1.a;
- MUL r1.rgb, r1, F.a; # Mul specular with fresnel factor
- #MUL r1.rgb, r1, DiffuseTexel; # Mul specular with specular map
- MUL r1.rgb, r1, SpecularTexel; # Mul specular with specular map
- MAD Result.rgb, SpecColor, 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
- # MAD Result.rgb, lt.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;
- END
-