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.

92 lines
3.8 KiB

  1. //===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
  2. //
  3. // Example pixel shader that can be applied to models
  4. //
  5. //==================================================================================================
  6. // STATIC: "CONVERT_TO_SRGB" "0..0"
  7. // STATIC: "FLASHLIGHT" "0..1"
  8. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps20b]
  9. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  10. // DYNAMIC: "PIXELFOGTYPE" "0..1"
  11. // DYNAMIC: "NUM_LIGHTS" "0..4"
  12. // DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA" "0..1" [ps20b]
  13. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps20b]
  14. // SKIP: ($PIXELFOGTYPE == 0) && ($WRITEWATERFOGTODESTALPHA != 0)
  15. // We don't care about flashlight depth unless the flashlight is on
  16. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 )
  17. // Flashlight shadow filter mode is irrelevant if there is no flashlight
  18. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTDEPTHFILTERMODE != 0 ) [ps20b]
  19. #include "common_flashlight_fxc.h"
  20. #include "shader_constant_register_map.h"
  21. const float4 g_DiffuseModulation : register( PSREG_DIFFUSE_MODULATION );
  22. const float4 g_ShadowTweaks : register( PSREG_ENVMAP_TINT__SHADOW_TWEAKS );
  23. const float3 cAmbientCube[6] : register( PSREG_AMBIENT_CUBE );
  24. const float4 g_EyePos : register( PSREG_EYEPOS_SPEC_EXPONENT );
  25. const float4 g_FogParams : register( PSREG_FOG_PARAMS );
  26. const float4 g_FlashlightAttenuationFactors : register( PSREG_FLASHLIGHT_ATTENUATION ); // On non-flashlight pass
  27. const float4 g_FlashlightPos_RimBoost : register( PSREG_FLASHLIGHT_POSITION_RIM_BOOST );
  28. const float4x4 g_FlashlightWorldToTexture : register( PSREG_FLASHLIGHT_TO_WORLD_TEXTURE );
  29. PixelShaderLightInfo cLightInfo[3] : register( PSREG_LIGHT_INFO_ARRAY ); // 2 registers each - 6 registers total (4th light spread across w's)
  30. #define g_FlashlightPos g_FlashlightPos_RimBoost.xyz
  31. sampler BaseTextureSampler : register( s0 ); // Base map, selfillum in alpha
  32. sampler ShadowDepthSampler : register( s4 ); // Flashlight shadow depth map sampler
  33. sampler NormalizeRandRotSampler : register( s5 ); // Normalization / RandomRotation samplers
  34. sampler FlashlightSampler : register( s6 ); // Flashlight cookie
  35. struct PS_INPUT
  36. {
  37. float2 baseTexCoord : TEXCOORD0;
  38. float4 lightAtten : TEXCOORD1;
  39. float3 worldNormal : TEXCOORD2;
  40. float3 worldPos : TEXCOORD3;
  41. float3 projPos : TEXCOORD4;
  42. };
  43. float4 main( PS_INPUT i ) : COLOR
  44. {
  45. float4 baseColor = tex2D( BaseTextureSampler, i.baseTexCoord );
  46. float3 diffuseLighting;
  47. if ( FLASHLIGHT != 0 )
  48. {
  49. float4 flashlightSpacePosition = mul( float4( i.worldPos, 1.0f ), g_FlashlightWorldToTexture );
  50. diffuseLighting = DoFlashlight( g_FlashlightPos, i.worldPos, flashlightSpacePosition,
  51. i.worldNormal, g_FlashlightAttenuationFactors.xyz,
  52. g_FlashlightAttenuationFactors.w, FlashlightSampler, ShadowDepthSampler,
  53. NormalizeRandRotSampler, FLASHLIGHTDEPTHFILTERMODE, FLASHLIGHTSHADOWS, true, i.projPos, false, g_ShadowTweaks );
  54. }
  55. else // non-flashlight path
  56. {
  57. // Summation of diffuse illumination from all local lights
  58. diffuseLighting = PixelShaderDoLighting( i.worldPos, i.worldNormal,
  59. float3( 0.0f, 0.0f, 0.0f ), false, true, i.lightAtten,
  60. cAmbientCube, NormalizeRandRotSampler, NUM_LIGHTS, cLightInfo, true,
  61. // These are dummy parameters:
  62. false, 1.0f,
  63. false, BaseTextureSampler );
  64. }
  65. float3 result = baseColor.rgb * g_DiffuseModulation.rgb * diffuseLighting;
  66. float alpha = g_DiffuseModulation.a * baseColor.a;
  67. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos.z, i.projPos.z );
  68. #if WRITEWATERFOGTODESTALPHA && ( PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT )
  69. alpha = fogFactor;
  70. #endif
  71. bool bWriteDepthToAlpha = ( WRITE_DEPTH_TO_DESTALPHA != 0 ) && ( WRITEWATERFOGTODESTALPHA == 0 );
  72. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, bWriteDepthToAlpha, i.projPos.z );
  73. }