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.

122 lines
3.6 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. // STATIC: "NORMALMAP" "0..1"
  7. // STATIC: "WORLDVERTEXTRANSITION" "0..1"
  8. // STATIC: "VERTEXCOLOR" "0..1"
  9. // DYNAMIC: "DOWATERFOG" "0..1"
  10. #include "common_vs_fxc.h"
  11. const float3 g_FlashlightPos : register( SHADER_SPECIFIC_CONST_0 );
  12. const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_1 );
  13. const float4 g_FlashlightAttenuationFactors : register( SHADER_SPECIFIC_CONST_5 );
  14. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_6 );
  15. const float4 cNormalMapTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_8 );
  16. static const int g_FogType = DOWATERFOG;
  17. struct VS_INPUT
  18. {
  19. // If this is float4, and the input is float3, the w component default to one.
  20. float4 vPos : POSITION;
  21. float3 vNormal : NORMAL;
  22. float2 vBaseTexCoord : TEXCOORD0;
  23. #if NORMALMAP
  24. float3 vTangentS : TANGENT;
  25. float3 vTangentT : BINORMAL;
  26. #endif
  27. float4 vColor : COLOR0;
  28. };
  29. struct VS_OUTPUT
  30. {
  31. float4 projPos : POSITION;
  32. #if !defined( _X360 )
  33. float fog : FOG;
  34. #endif
  35. float4 spotTexCoord : TEXCOORD0;
  36. float2 baseTexCoord : TEXCOORD1;
  37. #if NORMALMAP
  38. float3 tangentPosToLightVector : TEXCOORD2;
  39. float2 normalMapTexCoord : TEXCOORD3;
  40. #else
  41. float3 worldPosToLightVector : TEXCOORD2;
  42. float3 normal : TEXCOORD3;
  43. #endif
  44. float4 vertAtten : COLOR0;
  45. };
  46. float RemapValClamped( float val, float A, float B )
  47. {
  48. float cVal = (val - A) / (B - A);
  49. cVal = saturate( cVal );
  50. return cVal;
  51. }
  52. VS_OUTPUT main( const VS_INPUT v )
  53. {
  54. VS_OUTPUT o;
  55. float4 projPos;
  56. float3 worldPos;
  57. float3 worldNormal;
  58. float3 eyeVector;
  59. projPos = mul( v.vPos, cModelViewProj );
  60. o.projPos = projPos;
  61. worldPos = mul( v.vPos, cModel[0] );
  62. worldNormal = mul( v.vNormal, ( float3x3 )cModel[0] );
  63. #if NORMALMAP
  64. float3 worldTangentS = mul( v.vTangentS, cModel[0] );
  65. float3 worldTangentT = mul( v.vTangentT, cModel[0] );
  66. #endif
  67. #if !defined( _X360 )
  68. o.fog = CalcFog( worldPos, projPos, g_FogType );
  69. #endif
  70. o.baseTexCoord.x = dot( v.vBaseTexCoord, cBaseTexCoordTransform[0] ) + cBaseTexCoordTransform[0].w;
  71. o.baseTexCoord.y = dot( v.vBaseTexCoord, cBaseTexCoordTransform[1] ) + cBaseTexCoordTransform[1].w;
  72. float4 spotTexCoord = mul( float4( worldPos, 1.0f ), g_FlashlightWorldToTexture );
  73. o.spotTexCoord = spotTexCoord.xyzw;
  74. float3 worldPosToLightVector = g_FlashlightPos - worldPos;
  75. #if NORMALMAP
  76. o.normalMapTexCoord.x = dot( v.vBaseTexCoord, cNormalMapTexCoordTransform[0] ) + cNormalMapTexCoordTransform[0].w;
  77. o.normalMapTexCoord.y = dot( v.vBaseTexCoord, cNormalMapTexCoordTransform[1] ) + cNormalMapTexCoordTransform[1].w;
  78. o.tangentPosToLightVector.x = dot( worldPosToLightVector, worldTangentS );
  79. o.tangentPosToLightVector.y = dot( worldPosToLightVector, worldTangentT );
  80. o.tangentPosToLightVector.z = dot( worldPosToLightVector, worldNormal );
  81. #else
  82. o.worldPosToLightVector = worldPosToLightVector;
  83. o.normal = worldNormal;
  84. #endif
  85. float3 delta = worldPosToLightVector;
  86. float distSquared = dot( delta, delta );
  87. float dist = sqrt( distSquared );
  88. float farZ = g_FlashlightAttenuationFactors.w;
  89. float endFalloffFactor = RemapValClamped( dist, farZ, 0.6 * farZ );
  90. o.vertAtten.xyz = saturate( endFalloffFactor * dot( g_FlashlightAttenuationFactors, float3( 1.0f, 1.0f/dist, 1.0f/distSquared ) ) );
  91. #if WORLDVERTEXTRANSITION
  92. o.vertAtten.w = 1 - v.vColor.w;
  93. #else
  94. #if VERTEXCOLOR
  95. o.vertAtten.w = v.vColor.w;
  96. #else
  97. o.vertAtten.w = 1.0f;
  98. #endif
  99. #endif
  100. return o;
  101. }