Counter Strike : Global Offensive Source Code
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.

127 lines
4.3 KiB

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