Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
9.1 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  7. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps30][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  8. // STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
  9. // STATIC: "LIGHTWARPTEXTURE" "0..1"
  10. // DYNAMIC: "PIXELFOGTYPE" "0..1"
  11. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  12. // DYNAMIC: "NUM_LIGHTS" "0..2" [ps20]
  13. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps20b]
  14. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps30]
  15. #include "shader_constant_register_map.h"
  16. sampler BaseSampler : register( s0 ); // Base map
  17. sampler DiffuseWarpSampler : register( s1 ); // 1D texture for diffuse lighting modification
  18. sampler RefractSampler : register( s2 ); // Refraction map copied from back buffer
  19. sampler NormalSampler : register( s3 ); // Normal map
  20. sampler SpecExponentSampler : register( s4 ); // Flashlight cookie
  21. sampler NormalizeSampler : register( s5 ); // Normalization cube map
  22. const float3x3 g_ViewProj : register( c0 ); // 1st row of Projection matrix
  23. // c1 // 2nd row
  24. // c2 // 4th row
  25. const float2 g_CloakControl : register( c3 ); // { refract amount, cloak, ?, ? }
  26. const float3 cAmbientCube[6] : register( PSREG_AMBIENT_CUBE );
  27. const float4 g_EyePos_SpecExponent : register( PSREG_EYEPOS_SPEC_EXPONENT );
  28. const float4 g_FogParams : register( PSREG_FOG_PARAMS );
  29. const float4 g_FlashlightAttenuationFactors_RimMask : register( PSREG_FLASHLIGHT_ATTENUATION ); // On non-flashlight pass, x has rim mask control
  30. const float4 g_RimBoost : register( PSREG_FLASHLIGHT_POSITION_RIM_BOOST );
  31. const float4 g_FresnelSpecParams : register( PSREG_FRESNEL_SPEC_PARAMS ); // xyz are fresnel, w is specular boost
  32. const float4 g_SpecularRimParams : register( PSREG_SPEC_RIM_PARAMS ); // xyz are specular tint color, w is rim power
  33. PixelShaderLightInfo cLightInfo[3] : register( PSREG_LIGHT_INFO_ARRAY ); // 2 registers each - 6 registers total
  34. #define g_fRimBoost g_RimBoost.w
  35. #define g_FresnelRanges g_FresnelSpecParams.xyz
  36. #define g_SpecularBoost g_FresnelSpecParams.w
  37. #define g_SpecularTint g_SpecularRimParams.xyz
  38. #define g_RimExponent g_SpecularRimParams.w
  39. #define g_FlashlightAttenuationFactors g_FlashlightAttenuationFactors_RimMask
  40. #define g_RimMaskControl g_FlashlightAttenuationFactors_RimMask.x
  41. // 8 2D Poisson offsets (designed to use .xy and .wz swizzles (not .zw)
  42. static const float4 gPoissonOffset[4] = { float4 (-0.0876f, 0.9703f, 0.5651f, 0.4802f ),
  43. float4 ( 0.1851f, 0.1580f, -0.0617f, -0.2616f ),
  44. float4 (-0.5477f, -0.6603f, 0.0711f, -0.5325f ),
  45. float4 (-0.0751f, -0.8954f, 0.4054f, 0.6384f ) };
  46. struct PS_INPUT
  47. {
  48. float2 vBaseTexCoord : TEXCOORD0;
  49. float3x3 tangentSpaceTranspose : TEXCOORD1;
  50. // second row : TEXCOORD2;
  51. // third row : TEXCOORD3;
  52. float3 worldPos : TEXCOORD4;
  53. float3 projPos : TEXCOORD5;
  54. float4 lightAtten : TEXCOORD6;
  55. };
  56. float4 main( PS_INPUT i ) : COLOR
  57. {
  58. float3 vSpecular = float3( 0.0f, 0.0f, 0.0f );
  59. bool bDoDiffuseWarp = LIGHTWARPTEXTURE ? true : false;
  60. int nNumLights = NUM_LIGHTS;
  61. // Base color
  62. float4 albedo = tex2D( BaseSampler, i.vBaseTexCoord );
  63. // Load normal and expand range
  64. float4 vNormalSample = tex2D( NormalSampler, i.vBaseTexCoord );
  65. float3 tangentSpaceNormal = 2.0f * vNormalSample.xyz - 1.0f;
  66. // We need a world space normal if we're doing any lighting
  67. float3 vWorldNormal = normalize( mul( i.tangentSpaceTranspose, tangentSpaceNormal ) );
  68. float3 vWorldEyeDir = normalize( g_EyePos_SpecExponent.xyz - i.worldPos );
  69. // Vanilla 1-(N.V) fresnel term used later in transition lerp
  70. float fresnel = 1-saturate( dot( vWorldNormal, vWorldEyeDir ) );
  71. // Summation of diffuse illumination from all local lights
  72. float3 diffuseLighting = PixelShaderDoLighting( i.worldPos, vWorldNormal,
  73. float3( 0.0f, 0.0f, 0.0f ), false,
  74. true, i.lightAtten, cAmbientCube, NormalizeSampler,
  75. nNumLights, cLightInfo, true, false, 1.0f,
  76. bDoDiffuseWarp, DiffuseWarpSampler );
  77. // Transform world space normal into clip space and project
  78. float2 vProjNormal;
  79. vProjNormal.x = dot( vWorldNormal, g_ViewProj[0] ); // 1st row
  80. vProjNormal.y = dot( vWorldNormal, g_ViewProj[1] ); // 2nd row
  81. // Compute coordinates for sampling refraction
  82. float2 vRefractTexCoordNoWarp = i.projPos.xy / i.projPos.z;
  83. float2 vRefractTexCoord = vProjNormal.xy;
  84. float scale = lerp( g_CloakControl.x, 0.0f, g_CloakControl.y );
  85. vRefractTexCoord *= scale;
  86. vRefractTexCoord += vRefractTexCoordNoWarp;
  87. #ifdef SHADER_MODEL_PS_2_0
  88. float3 vRefract = tex2D( RefractSampler, vRefractTexCoordNoWarp );
  89. #endif
  90. // Extra refraction rays, specular, rim etc are only done on ps_2_b
  91. #if defined( SHADER_MODEL_PS_2_B ) || defined( SHADER_MODEL_PS_3_0 )
  92. // Blur by scalable Poisson filter
  93. float fBlurAmount = lerp( 0.05f, 0.0f, g_CloakControl.y );
  94. float3 vRefract = tex2D( RefractSampler, vRefractTexCoord );
  95. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[0].xy * fBlurAmount );
  96. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[0].wz * fBlurAmount );
  97. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[1].xy * fBlurAmount );
  98. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[1].wz * fBlurAmount );
  99. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[2].xy * fBlurAmount );
  100. vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[2].wz * fBlurAmount );
  101. // We're right at the hairy edge of constant register usage and hence have to drop these taps...
  102. // vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[3].xy * fBlurAmount );
  103. // vRefract += tex2D( RefractSampler, vRefractTexCoord + gPoissonOffset[3].wz * fBlurAmount );
  104. vRefract /= 7.0f;
  105. float3 rimLighting = float3( 0.0f, 0.0f, 0.0f );
  106. float3 specularLighting = float3( 0.0f, 0.0f, 0.0f );
  107. float fSpecExp = g_EyePos_SpecExponent.w;
  108. float fSpecMask = vNormalSample.a;
  109. float4 vSpecExpMap = tex2D( SpecExponentSampler, i.vBaseTexCoord );
  110. float fSpecExpMap = vSpecExpMap.r;
  111. float fRimMask = 1.0f;//lerp( 1.0f, vSpecExpMap.a, g_RimMaskControl ); // Select rim mask
  112. float3 vSpecularTint;
  113. // If the exponent passed in as a constant is zero, use the value from the map as the exponent
  114. if ( fSpecExp == 0 )
  115. fSpecExp = 1.0f - fSpecExpMap + 150.0f * fSpecExpMap;
  116. // If constant tint is negative, tint with albedo, based upon scalar tint map
  117. if ( g_SpecularTint.r == -1 )
  118. vSpecularTint = lerp( float3(1.0f, 1.0f, 1.0f), albedo, vSpecExpMap.g );
  119. else
  120. vSpecularTint = g_SpecularTint.rgb;
  121. // Fresnel to match regular specular lighting
  122. float fFresnelRanges = Fresnel( vWorldNormal, vWorldEyeDir, g_FresnelRanges );
  123. // Summation of specular from all local lights besides the flashlight
  124. PixelShaderDoSpecularLighting( i.worldPos, vWorldNormal, fSpecExp, vWorldEyeDir,
  125. i.lightAtten, nNumLights, cLightInfo, false, 1.0f, false,
  126. NormalizeSampler, 1.0f, true, g_RimExponent,
  127. // Outputs
  128. specularLighting, rimLighting );
  129. // Modulate with spec mask, boost, tint and fresnel ranges
  130. specularLighting *= fSpecMask * g_SpecularBoost * fFresnelRanges * vSpecularTint;
  131. float fRimFresnel = Fresnel4( vWorldNormal, vWorldEyeDir );
  132. // Add in rim light modulated with tint, mask and traditional Fresnel (not using Fresnel ranges)
  133. rimLighting *= vSpecularTint * fRimMask * fRimFresnel;
  134. // Fold rim lighting into specular term by using the max so that we don't really add light twice...
  135. specularLighting = max (specularLighting, rimLighting);
  136. // Add in view-ray lookup from ambient cube
  137. specularLighting += fRimFresnel * fRimMask * vSpecularTint /* g_fRimBoost */ * PixelShaderAmbientLight( vWorldEyeDir, cAmbientCube) * saturate(dot(vWorldNormal, float3(0, 0 , 1)) );
  138. float tintLerpFactor = saturate(lerp( 1, fresnel-1.1, saturate(g_CloakControl.y)));
  139. tintLerpFactor = smoothstep( 0.4f, 0.425f, tintLerpFactor );
  140. float3 vTintedRefract = lerp( vRefract, albedo * vRefract, 0.7f );
  141. vRefract = lerp( vRefract, vTintedRefract, tintLerpFactor );
  142. vSpecular = specularLighting * smoothstep( 0.98, 0.8, saturate(g_CloakControl.y ));
  143. #endif
  144. // Blend refraction component with diffusely lit model
  145. float diffuseLerpFactor = saturate(lerp( 1, fresnel - 1.35, saturate(g_CloakControl.y)));
  146. diffuseLerpFactor = smoothstep( 0.4f, 0.425f, diffuseLerpFactor );
  147. float3 fDiffuse = lerp( vRefract, albedo * diffuseLighting, diffuseLerpFactor );
  148. float3 result = fDiffuse + vSpecular;
  149. float alpha = 1.0f;
  150. // Emulate LinearColorToHDROutput() when uncloaked
  151. result = lerp( result.xyz * LINEAR_LIGHT_SCALE, result, saturate(g_CloakControl.y) );
  152. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos_SpecExponent.z, i.worldPos.z, i.projPos.z );
  153. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  154. alpha = fogFactor;
  155. #endif
  156. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_NONE );
  157. }