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.

198 lines
7.2 KiB

  1. //======= Copyright � 1996-2006, Valve Corporation, All rights reserved. ======
  2. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  3. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps30][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  4. // STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
  5. // STATIC: "BASETEXTURE" "0..1"
  6. // STATIC: "CUBEMAP" "0..1"
  7. // STATIC: "DIFFUSELIGHTING" "0..1"
  8. // STATIC: "NORMALMAPALPHAENVMAPMASK" "0..1"
  9. // STATIC: "HALFLAMBERT" "0..1"
  10. // STATIC: "FLASHLIGHT" "0..1"
  11. // STATIC: "TRANSLUCENT" "0..1"
  12. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  13. // DYNAMIC: "PIXELFOGTYPE" "0..1"
  14. // DYNAMIC: "WARPINGIN" "0..1"
  15. // DYNAMIC: "AMBIENT_LIGHT" "0..1"
  16. // DYNAMIC: "NUM_LIGHTS" "0..2" [ps20]
  17. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps20b]
  18. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps30]
  19. // We don't use other lights when doing the flashlight, so just skip that
  20. // SKIP: ( $FLASHLIGHT != 0 ) && ( $NUM_LIGHTS > 0 ) [PC]
  21. #include "common_flashlight_fxc.h"
  22. #include "common_vertexlitgeneric_dx9.h"
  23. const HALF4 g_EnvmapTint : register( c0 );
  24. const HALF4 g_DiffuseModulation : register( c1 );
  25. #if !FLASHLIGHT
  26. const HALF3 g_EnvmapContrast : register( c2 );
  27. const HALF3 g_EnvmapSaturation : register( c3 );
  28. #endif
  29. const HALF4 g_SelfIllumTint : register( c4 );
  30. const float3 cAmbientCube[6] : register( c5 );
  31. // 2 registers each - 6 register total
  32. PixelShaderLightInfo cLightInfo[3] : register( c13 ); // through c18
  33. const HALF3 g_EyePos : register( c20 );
  34. const HALF4 g_FogParams : register( c21 );
  35. #if FLASHLIGHT
  36. const float4 g_FlashlightAttenuationFactors : register( c22 );
  37. const HALF3 g_FlashlightPos : register( c23 );
  38. const float4x4 g_FlashlightWorldToTexture : register( c24 ); // through c27
  39. #else
  40. const float g_Time : register( c22 );
  41. #endif
  42. sampler BaseTextureSampler : register( s0 );
  43. sampler EnvmapSampler : register( s1 );
  44. sampler FlowMapSampler : register( s2 );
  45. sampler BumpmapSampler : register( s3 );
  46. sampler EnvmapMaskSampler : register( s4 );
  47. sampler NormalizeSampler : register( s5 );
  48. sampler SelfIllumMapSampler : register( s6 );
  49. sampler FlashlightSampler : register( s7 );
  50. struct PS_INPUT
  51. {
  52. HALF4 baseTexCoord2_tangentSpaceVertToEyeVectorXY : TEXCOORD0;
  53. // bump mapping and a separate envmap mask texture are mutually exclusive.
  54. float4 lightAtten : TEXCOORD1;
  55. float4 worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ : TEXCOORD2;
  56. float3x3 tangentSpaceTranspose : TEXCOORD3;
  57. // second row : TEXCOORD4;
  58. // third row : TEXCOORD5;
  59. float4 worldPos_projPosZ : TEXCOORD6;
  60. float4 fogFactorW : COLOR1;
  61. };
  62. float4 main( PS_INPUT i ) : COLOR
  63. {
  64. bool bBaseTexture = BASETEXTURE ? true : false;
  65. bool bCubemap = CUBEMAP ? true : false;
  66. bool bDiffuseLighting = DIFFUSELIGHTING ? true : false;
  67. bool bNormalMapAlphaEnvmapMask = NORMALMAPALPHAENVMAPMASK ? true : false;
  68. bool bHalfLambert = HALFLAMBERT ? true : false;
  69. bool bFlashlight = (FLASHLIGHT!=0) ? true : false;
  70. bool bAmbientLight = AMBIENT_LIGHT ? true : false;
  71. int nNumLights = NUM_LIGHTS;
  72. HALF4 baseColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
  73. if( bBaseTexture )
  74. baseColor = tex2D( BaseTextureSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  75. float specularFactor = 1.0f;
  76. HALF4 normalTexel = tex2D( BumpmapSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  77. HALF3 tangentSpaceNormal = 2.0f * normalTexel - 1.0f;
  78. if( bNormalMapAlphaEnvmapMask )
  79. specularFactor = normalTexel.a;
  80. HALF3 diffuseLighting = HALF3( 1.0f, 1.0f, 1.0f );
  81. if( bDiffuseLighting )
  82. {
  83. float3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  84. worldSpaceNormal = NormalizeWithCubemap( NormalizeSampler, worldSpaceNormal );
  85. diffuseLighting = PixelShaderDoLighting( i.worldPos_projPosZ.xyz, worldSpaceNormal,
  86. float3( 0.0f, 0.0f, 0.0f ), false, bAmbientLight, i.lightAtten,
  87. cAmbientCube, NormalizeSampler, nNumLights, cLightInfo, bHalfLambert,
  88. false, 0, false, NormalizeSampler );
  89. }
  90. HALF3 albedo = HALF3( 1.0f, 1.0f, 1.0f );
  91. HALF alpha = 1.0f;
  92. if( bBaseTexture )
  93. {
  94. albedo *= baseColor;
  95. alpha *= baseColor.a;
  96. }
  97. // If we only have specularity, assume that we want a black diffuse component, and
  98. // get alpha from the envmapmask
  99. if( !bBaseTexture && bCubemap )
  100. {
  101. diffuseLighting = HALF3( 0.0f, 0.0f, 0.0f );
  102. if( bNormalMapAlphaEnvmapMask )
  103. {
  104. alpha *= specularFactor;
  105. }
  106. }
  107. #if FLASHLIGHT
  108. if( bFlashlight )
  109. {
  110. float3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  111. float4 flashlightSpacePosition = mul( float4( i.worldPos_projPosZ.xyz, 1.0f ), g_FlashlightWorldToTexture );
  112. diffuseLighting = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, flashlightSpacePosition,
  113. worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  114. g_FlashlightAttenuationFactors.w, FlashlightSampler,
  115. EnvmapMaskSampler, EnvmapMaskSampler, 0, false, false, float2(0, 0), true );
  116. }
  117. #endif
  118. diffuseLighting *= g_DiffuseModulation.rgb;
  119. alpha *= g_DiffuseModulation.a;
  120. HALF3 diffuseComponent = albedo * diffuseLighting;
  121. HALF4 flowmapSample = tex2D( FlowMapSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  122. #if !FLASHLIGHT
  123. flowmapSample.xy += float2( .11, .124 ) * g_Time.xx;
  124. #endif
  125. HALF4 selfIllumSample = tex2D( SelfIllumMapSampler, flowmapSample.xy );
  126. // float thing = ( 0.5f * ( cos( g_Time * 3 ) + 1.0f ) );
  127. // diffuseComponent.xyz += albedo * 10.0f * pow( selfIllumSample.xyz, thing );
  128. diffuseComponent.xyz += albedo * g_SelfIllumTint.xyz * selfIllumSample.xyz;
  129. HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
  130. #if !FLASHLIGHT
  131. if( bCubemap )
  132. {
  133. // If we've *only* specified a cubemap, blow off the diffuse component
  134. if ( !bBaseTexture && !bDiffuseLighting && !bFlashlight )
  135. {
  136. diffuseComponent = HALF3( 0.0f, 0.0f, 0.0f );
  137. }
  138. HALF3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  139. HALF3 reflectVect = CalcReflectionVectorUnnormalized( worldSpaceNormal, i.worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ.xyz );
  140. specularLighting = ENV_MAP_SCALE * texCUBE( EnvmapSampler, reflectVect );
  141. specularLighting *= specularFactor;
  142. specularLighting *= g_EnvmapTint;
  143. HALF3 specularLightingSquared = specularLighting * specularLighting;
  144. specularLighting = lerp( specularLighting, specularLightingSquared, g_EnvmapContrast );
  145. HALF3 greyScale = dot( specularLighting, HALF3( 0.299f, 0.587f, 0.114f ) );
  146. specularLighting = lerp( greyScale, specularLighting, g_EnvmapSaturation );
  147. }
  148. #endif
  149. HALF3 result = diffuseComponent + specularLighting;
  150. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );
  151. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  152. alpha = fogFactor;
  153. #endif
  154. // result.xyz = float3( 1.0f, 1.0f, 1.0f );
  155. #if TRANSLUCENT==0
  156. # if WARPINGIN
  157. alpha = 0.0f; // write alpha where the vortigaunt is so that we can selectivly draw pixels when refracting
  158. #else
  159. alpha = 1.0f; // write alpha where the vortigaunt is so that we can selectivly draw pixels when refracting
  160. # endif
  161. #endif
  162. //FIXME: need to take dowaterfog into consideration
  163. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR );
  164. }