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.

130 lines
4.3 KiB

  1. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  2. // STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
  3. // STATIC: "CUBEMAP" "0..1"
  4. // STATIC: "VERTEXCOLOR" "0..1"
  5. // STATIC: "ENVMAPMASK" "0..1"
  6. // STATIC: "BASEALPHAENVMAPMASK" "0..1"
  7. // STATIC: "HDRTYPE" "0..2"
  8. // DYNAMIC: "HDRENABLED" "0..1"
  9. // DYNAMIC: "PIXELFOGTYPE" "0..1"
  10. // HDRFIXME: Need to make this work.
  11. #define USE_32BIT_LIGHTMAPS_ON_360 //uncomment to use 32bit lightmaps, be sure to keep this in sync with the same #define in materialsystem/cmatlightmaps.cpp
  12. #include "common_ps_fxc.h"
  13. #include "common_lightmappedgeneric_fxc.h"
  14. const HALF4 g_EnvmapTint : register( c0 );
  15. const HALF3 g_DiffuseModulation : register( c1 );
  16. const HALF3 g_EnvmapContrast : register( c2 );
  17. const HALF3 g_EnvmapSaturation : register( c3 );
  18. const HALF4 g_FresnelReflection : register( c4 );
  19. const HALF3 g_EyePos : register( c5 );
  20. const HALF3 g_OverbrightFactor : register( c6 );
  21. const HALF4 g_FogParams : register( c12 );
  22. // CENTROID: TEXCOORD2
  23. sampler BaseTextureSampler : register( s0 );
  24. sampler LightmapSampler : register( s1 );
  25. sampler EnvmapSampler : register( s2 );
  26. sampler DetailSampler : register( s3 );
  27. sampler EnvmapMaskSampler : register( s5 );
  28. sampler NormalizeSampler : register( s6 );
  29. struct PS_INPUT
  30. {
  31. HALF2 baseTexCoord : TEXCOORD0;
  32. HALF2 detailTexCoord : TEXCOORD1;
  33. HALF2 lightmapTexCoord : TEXCOORD2;
  34. HALF2 envmapMaskTexCoord : TEXCOORD3;
  35. HALF4 worldPos_projPosZ : TEXCOORD4;
  36. HALF3 worldSpaceNormal : TEXCOORD5;
  37. HALF4 vertexColor : COLOR;
  38. HALF4 fogFactorW : COLOR1;
  39. };
  40. float4 main( PS_INPUT i ) : COLOR
  41. {
  42. bool bCubemap = CUBEMAP ? true : false;
  43. bool bVertexColor = VERTEXCOLOR ? true : false;
  44. bool bEnvmapMask = ENVMAPMASK ? true : false;
  45. bool bBaseAlphaEnvmapMask = BASEALPHAENVMAPMASK ? true : false;
  46. HALF4 baseColor = tex2D( BaseTextureSampler, i.baseTexCoord );
  47. HALF4 detailColor = tex2D( DetailSampler, i.detailTexCoord );
  48. HALF2 lightmapCoordinates = i.lightmapTexCoord;
  49. HALF3 lightmapColor = LightMapSample( LightmapSampler, lightmapCoordinates );
  50. HALF3 specularFactor = 1.0f;
  51. if( bEnvmapMask )
  52. {
  53. specularFactor = tex2D( EnvmapMaskSampler, i.detailTexCoord ).xyz;
  54. }
  55. if( bBaseAlphaEnvmapMask )
  56. {
  57. specularFactor *= 1.0 - baseColor.a; // this blows!
  58. }
  59. HALF3 diffuseLighting = lightmapColor;
  60. diffuseLighting *= g_DiffuseModulation;
  61. diffuseLighting *= LIGHT_MAP_SCALE;
  62. HALF3 albedo = baseColor;
  63. HALF alpha = 1.0f;
  64. if( !bBaseAlphaEnvmapMask )
  65. {
  66. alpha *= baseColor.a;
  67. }
  68. albedo *= detailColor;
  69. alpha *= detailColor.a;
  70. // FIXME: seperate vertexcolor and vertexalpha?
  71. // vertex alpha is ignored if vertexcolor isn't set. . need to check other version.
  72. if( bVertexColor )
  73. {
  74. albedo *= i.vertexColor;
  75. alpha *= i.vertexColor.a; // not sure about this one
  76. }
  77. HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
  78. if( bCubemap )
  79. {
  80. float3 worldVertToEyeVector = g_EyePos - i.worldPos_projPosZ.xyz;
  81. worldVertToEyeVector = NormalizeWithCubemap( NormalizeSampler, worldVertToEyeVector );
  82. HALF3 reflectVect = CalcReflectionVectorUnnormalized( i.worldSpaceNormal, worldVertToEyeVector );
  83. // Calc Fresnel factor
  84. HALF3 worldSpaceNormal = NormalizeWithCubemap( NormalizeSampler, i.worldSpaceNormal );
  85. HALF fresnel = 1.0 - dot( worldSpaceNormal, worldVertToEyeVector );
  86. fresnel = pow( fresnel, 5.0 );
  87. fresnel = fresnel * g_FresnelReflection.b + g_FresnelReflection.a;
  88. specularLighting = texCUBE( EnvmapSampler, reflectVect );
  89. specularLighting *= specularFactor;
  90. specularLighting *= g_EnvmapTint;
  91. #if HDRTYPE == HDR_TYPE_NONE
  92. HALF3 specularLightingSquared = specularLighting * specularLighting;
  93. specularLighting = lerp( specularLighting, specularLightingSquared, g_EnvmapContrast );
  94. HALF3 greyScale = dot( specularLighting, HALF3( 0.299f, 0.587f, 0.114f ) );
  95. specularLighting = lerp( greyScale, specularLighting, g_EnvmapSaturation );
  96. #endif
  97. specularLighting *= fresnel;
  98. }
  99. // Do it somewhat unlit
  100. HALF3 result = albedo*(g_OverbrightFactor.z*diffuseLighting + g_OverbrightFactor.y) + specularLighting;
  101. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );
  102. return FinalOutput( HALF4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR );
  103. }