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.

116 lines
3.8 KiB

  1. // DYNAMIC: "FOGTYPE" "0..1"
  2. #include "common_vs_fxc.h"
  3. static const int g_FogType = FOGTYPE;
  4. const float4 cCustomConstants[6] : register( SHADER_SPECIFIC_CONST_0 );
  5. const float4 g_vLightPosition : register( SHADER_SPECIFIC_CONST_0 );
  6. const float4 g_vLightColor : register( SHADER_SPECIFIC_CONST_1 ); // range 0-1
  7. const float g_flLightIntensity : register( SHADER_SPECIFIC_CONST_2 ); // scales g_vLightColor
  8. struct VS_INPUT
  9. {
  10. // If this is float4, and the input is float3, the w component default to one.
  11. float4 vPos : POSITION;
  12. float2 vBumpTexCoord : TEXCOORD0;
  13. float4 vAmbientColor : COLOR0;
  14. };
  15. struct VS_OUTPUT
  16. {
  17. float4 projPos : POSITION;
  18. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  19. float fog : FOG;
  20. #endif
  21. float2 vBumpTexCoord : TEXCOORD0;
  22. float3 vTangentSpaceLightDir : TEXCOORD1;
  23. float3 vAmbientColor : TEXCOORD2;
  24. float4 vIteratedProjPos : TEXCOORD3;
  25. float4 vDirLightScale : COLOR0;
  26. float4 worldPos_projPosZ : TEXCOORD7; // Necessary for pixel fog
  27. };
  28. VS_OUTPUT main( const VS_INPUT v )
  29. {
  30. VS_OUTPUT o = (VS_OUTPUT)0;
  31. // Transform the input position.
  32. float4 projPos = mul( v.vPos, cModelViewProj );
  33. o.projPos = projPos;
  34. #ifdef _PS3
  35. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  36. o.projPos.y = -o.projPos.y;
  37. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  38. #endif // _PS3
  39. o.vIteratedProjPos = projPos;
  40. o.worldPos_projPosZ = float4( v.vPos.xyz, projPos.z );
  41. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  42. o.fog = CalcFixedFunctionFog( mul4x3( v.vPos, cModel[0] ), g_FogType );
  43. #endif
  44. // Copy texcoords over.
  45. o.vBumpTexCoord = v.vBumpTexCoord;
  46. // Copy the vertex color over.
  47. o.vAmbientColor = v.vAmbientColor;
  48. // ------------------------------------------------------------------------------
  49. // Generate a tangent space and rotate L.
  50. // This can be thought of as rotating the normal map to face the viewer.
  51. //
  52. // This is useful when a particle is way off to the side of the screen.
  53. // You should be looking at the half-sphere with a normal pointing from the
  54. // particle to the viewer. Instead, you're looking at the half-sphere with
  55. // a normal along Z. This tangent space builder code fixes the problem.
  56. //
  57. // Note that since the model and view matrices are identity, the coordinate
  58. // system has X=right, Y=up, and Z=behind you (negative Z goes into the screen).
  59. // ------------------------------------------------------------------------------
  60. // This basis wants Z positive going into the screen so flip it here.
  61. float4 vForward = normalize( float4( v.vPos.x, v.vPos.y, -v.vPos.z, 1 ) );
  62. // This is the same as CrossProduct( vForward, Vector( 1, 0, 0 ) )
  63. float4 vUp = normalize( float4( 0, vForward.z, -vForward.y, vForward.w ) );
  64. // vRight = CrossProduct( vUp, vForward )
  65. float4 vRight = vUp.yzxw * vForward.zxyw;
  66. vRight += -vUp.zxyw * vForward.yzxw;
  67. // Put the light in tangent space.
  68. float4 vToLight = g_vLightPosition - v.vPos;
  69. float4 vTangentSpaceLight = vRight*vToLight.x + vUp*vToLight.y + vForward*vToLight.z;
  70. // Output texcoord 1 holds the normalized transformed light direction.
  71. o.vTangentSpaceLightDir = normalize( vTangentSpaceLight ).xyz * 0.5 + 0.5; // make it 0-1 for the pixel shader
  72. // Handle oversaturation here. The shader code already scaled the light color so its max value is 1,
  73. // so if our intensity/distance scale is > 1, then all we need to do is use the light color.
  74. float flTransposedLenSqr = dot( vTangentSpaceLight, vTangentSpaceLight );
  75. float flScaledIntensity = g_flLightIntensity / flTransposedLenSqr;
  76. if ( flScaledIntensity > 1 )
  77. {
  78. o.vDirLightScale.xyz = g_vLightColor.xyz;
  79. }
  80. else
  81. {
  82. o.vDirLightScale.xyz = g_vLightColor.xyz * flScaledIntensity.xxx;
  83. }
  84. // Alpha comes right from the vertex color.
  85. o.vDirLightScale.a = v.vAmbientColor.a;
  86. return o;
  87. }