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.

217 lines
7.1 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  9. // STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
  10. // STATIC: "DETAILTEXTURE" "0..1"
  11. // STATIC: "BUMPMAP" "0..1"
  12. // STATIC: "VERTEXCOLOR" "0..1"
  13. // STATIC: "SELFILLUM" "0..1"
  14. // STATIC: "DIFFUSEBUMPMAP" "0..1"
  15. // STATIC: "DETAIL_ALPHA_MASK_BASE_TEXTURE" "0..1"
  16. // STATIC: "FLASHLIGHT" "0..1"
  17. // STATIC: "SEAMLESS" "0..1"
  18. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps20b] [PC]
  19. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..0" [ps20b] [XBOX]
  20. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  21. // DYNAMIC: "PIXELFOGTYPE" "0..1"
  22. // DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA" "0..1" [ps20b] [PC]
  23. // DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA" "0..0" [ps20b] [XBOX]
  24. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps20b]
  25. // SKIP: $DETAILTEXTURE && ( $BUMPMAP && !$DETAIL_ALPHA_MASK_BASE_TEXTURE )
  26. // SKIP: !$BUMPMAP && $DIFFUSEBUMPMAP
  27. // SKIP: $VERTEXCOLOR && $BUMPMAP
  28. // SKIP: FLASHLIGHT && $SELFILLUM
  29. // SKIP: FLASHLIGHT && $DETAIL_ALPHA_MASK_BASE_TEXTURE
  30. // SKIP: FLASHLIGHT && ($BUMPMAP || $DIFFUSEBUMPMAP)
  31. // We don't care about flashlight depth unless the flashlight is on
  32. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 ) [ps20b]
  33. #if defined( SHADER_MODEL_PS_2_0 )
  34. # define WRITE_DEPTH_TO_DESTALPHA 0
  35. #endif
  36. #define HDRTYPE HDR_TYPE_NONE
  37. #include "common_flashlight_fxc.h"
  38. #include "common_ps_fxc.h"
  39. const HALF4 g_SelfIllumTint : register( c7 );
  40. static const HALF g_OverbrightFactor = 2.0f;
  41. const HALF3 g_EyePos : register( c10 );
  42. const HALF4 g_FogParams : register( c11 );
  43. const HALF3 g_FlashlightPos : register( c15 );
  44. // flashlightfixme: Move this math into the vertex shader.
  45. const float4x4 g_FlashlightWorldToTexture : register( c16 );
  46. const float4 g_FlashlightAttenuationFactors : register( c20 );
  47. sampler BaseTextureSampler : register( s0 );
  48. sampler LightmapSampler : register( s1 );
  49. sampler FlashlightSampler : register( s2 );
  50. sampler DetailSampler : register( s3 );
  51. sampler BumpmapSampler : register( s4 );
  52. sampler NormalizeSampler : register( s6 );
  53. struct PS_INPUT
  54. {
  55. HALF2 baseTexCoord : TEXCOORD0;
  56. HALF4 detailOrBumpTexCoord : TEXCOORD1;
  57. HALF4 lightmapTexCoord1And2 : TEXCOORD2; // CENTROID: TEXCOORD2
  58. HALF2 lightmapTexCoord3 : TEXCOORD3; // CENTROID: TEXCOORD3
  59. HALF4 worldPos_projPosZ : TEXCOORD4;
  60. HALF3x3 tangentSpaceTranspose : TEXCOORD5;
  61. // tangentSpaceTranspose : TEXCOORD6;
  62. // tangentSpaceTranspose : TEXCOORD7;
  63. HALF4 vertexColor : COLOR;
  64. };
  65. float4 main( PS_INPUT i ) : COLOR
  66. {
  67. bool bDetailTexture = DETAILTEXTURE ? true : false;
  68. bool bBumpmap = BUMPMAP ? true : false;
  69. bool bDiffuseBumpmap = DIFFUSEBUMPMAP ? true : false;
  70. bool bVertexColor = VERTEXCOLOR ? true : false;
  71. bool bSelfIllum = SELFILLUM ? true : false;
  72. bool bDetailAlphaMaskBaseTexture = DETAIL_ALPHA_MASK_BASE_TEXTURE ? true : false;
  73. bool bFlashlight = FLASHLIGHT ? true : false;
  74. HALF3 lightmapColor1 = HALF3( 1.0f, 1.0f, 1.0f );
  75. HALF3 lightmapColor2 = HALF3( 1.0f, 1.0f, 1.0f );
  76. HALF3 lightmapColor3 = HALF3( 1.0f, 1.0f, 1.0f );
  77. if( bBumpmap && bDiffuseBumpmap )
  78. {
  79. HALF2 bumpCoord1;
  80. HALF2 bumpCoord2;
  81. HALF2 bumpCoord3;
  82. ComputeBumpedLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy,
  83. bumpCoord1, bumpCoord2, bumpCoord3 );
  84. HALF4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
  85. lightmapColor1 = lightmapSample1.rgb;
  86. lightmapColor2 = tex2D( LightmapSampler, bumpCoord2 );
  87. lightmapColor3 = tex2D( LightmapSampler, bumpCoord3 );
  88. }
  89. else
  90. {
  91. if( !bFlashlight )
  92. {
  93. HALF2 bumpCoord1 = ComputeLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy );
  94. HALF4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
  95. lightmapColor1 = lightmapSample1.rgb;
  96. }
  97. }
  98. HALF4 detailColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
  99. if( bDetailTexture )
  100. {
  101. detailColor = tex2D( DetailSampler, i.detailOrBumpTexCoord.xy );
  102. }
  103. HALF4 baseColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
  104. baseColor = tex2D( BaseTextureSampler, i.baseTexCoord );
  105. if ( bDetailAlphaMaskBaseTexture )
  106. {
  107. // This is what WorldTwoTextureBlend_DX6 does.
  108. baseColor.rgb = saturate( saturate( baseColor * 2 ) * detailColor.a + (1 - detailColor.a) );
  109. baseColor.rgb *= detailColor;
  110. }
  111. else
  112. {
  113. baseColor.rgb = lerp( baseColor, detailColor, detailColor.a );
  114. }
  115. HALF3 normal = HALF3( 0.0f, 0.0f, 1.0f );
  116. if( bBumpmap )
  117. {
  118. HALF3 normalTexel;
  119. normalTexel = tex2D( BumpmapSampler, i.detailOrBumpTexCoord.xy );
  120. normal = 2.0 * normalTexel - 1.0;
  121. }
  122. HALF3 albedo = HALF3( 1.0f, 1.0f, 1.0f );
  123. HALF alpha = 1.0f;
  124. albedo *= baseColor;
  125. if( !bSelfIllum )
  126. {
  127. alpha *= baseColor.a;
  128. }
  129. // The vertex color contains the modulation color + vertex color combined
  130. albedo *= i.vertexColor;
  131. alpha *= i.vertexColor.a; // not sure about this one
  132. HALF3 diffuseLighting;
  133. if( bFlashlight )
  134. {
  135. float3 worldSpaceNormal;
  136. // Make the unbumped version not so fucking stupid and not need tangentSpaceTranspose you knob.
  137. worldSpaceNormal = mul( normal, i.tangentSpaceTranspose );
  138. int nShadowSampleLevel = 0;
  139. bool bDoShadows = false;
  140. // On ps_2_b, we can do shadow mapping
  141. #if ( FLASHLIGHTSHADOWS && (defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0) ) )
  142. nShadowSampleLevel = FLASHLIGHTDEPTHFILTERMODE;
  143. bDoShadows = true;
  144. #endif
  145. float4 flashlightSpacePosition = mul( float4( i.worldPos_projPosZ.xyz, 1.0f ), g_FlashlightWorldToTexture );
  146. diffuseLighting = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, flashlightSpacePosition,
  147. worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  148. g_FlashlightAttenuationFactors.w, FlashlightSampler, FlashlightSampler, NormalizeSampler,
  149. nShadowSampleLevel, bDoShadows, false, float2(0, 0), false );
  150. }
  151. else
  152. {
  153. if( bBumpmap && bDiffuseBumpmap )
  154. {
  155. float dot1 = saturate( dot( normal, bumpBasis[0] ) );
  156. float dot2 = saturate( dot( normal, bumpBasis[1] ) );
  157. float dot3 = saturate( dot( normal, bumpBasis[2] ) );
  158. float sum = dot1 + dot2 + dot3;
  159. diffuseLighting = dot1 * lightmapColor1 +
  160. dot2 * lightmapColor2 +
  161. dot3 * lightmapColor3;
  162. diffuseLighting *= 1.0f / sum;
  163. }
  164. else
  165. {
  166. diffuseLighting = lightmapColor1;
  167. }
  168. // Only scale here since the flashlight will already be scaled properly
  169. diffuseLighting *= g_OverbrightFactor;
  170. }
  171. HALF3 diffuseComponent = albedo * diffuseLighting;
  172. if( bSelfIllum )
  173. {
  174. HALF3 selfIllumComponent = g_SelfIllumTint * albedo;
  175. diffuseComponent = lerp( diffuseComponent, selfIllumComponent, baseColor.a );
  176. }
  177. HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
  178. HALF3 result = diffuseComponent + specularLighting;
  179. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );
  180. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  181. alpha = fogFactor;
  182. #endif
  183. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, (WRITE_DEPTH_TO_DESTALPHA != 0), i.worldPos_projPosZ.w );
  184. }