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.

144 lines
4.9 KiB

  1. // ------------------------------------------------------------------------------
  2. // $cLight0Pos = world space light position
  3. // $SHADER_SPECIFIC_CONST_1 = spotlight projection
  4. // $SHADER_SPECIFIC_CONST_2 = spotlight projection
  5. // $SHADER_SPECIFIC_CONST_3 = spotlight projection
  6. // $SHADER_SPECIFIC_CONST_4 = spotlight projection
  7. // $SHADER_SPECIFIC_CONST_5 = far z
  8. // $SHADER_SPECIFIC_CONST_6 = eyeball origin
  9. // $SHADER_SPECIFIC_CONST_7 = eyeball up * 0.5
  10. // $SHADER_SPECIFIC_CONST_8 = iris projection U
  11. // $SHADER_SPECIFIC_CONST_9 = iris projection V
  12. // ------------------------------------------------------------------------------
  13. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  14. // DYNAMIC: "SKINNING" "0..1"
  15. // DYNAMIC: "DOWATERFOG" "0..1"
  16. // DYNAMIC: "MORPHING" "0..1" [vs30]
  17. #include "common_vs_fxc.h"
  18. static const bool g_bSkinning = SKINNING ? true : false;
  19. static const int g_FogType = DOWATERFOG;
  20. const float4 cLightPosition : register( SHADER_SPECIFIC_CONST_0 );
  21. const float4 cSpotlightProj1 : register( SHADER_SPECIFIC_CONST_1 );
  22. const float4 cSpotlightProj2 : register( SHADER_SPECIFIC_CONST_2 );
  23. const float4 cSpotlightProj3 : register( SHADER_SPECIFIC_CONST_3 );
  24. const float4 cSpotlightProj4 : register( SHADER_SPECIFIC_CONST_4 );
  25. const float4 cFlashlighAtten : register( SHADER_SPECIFIC_CONST_5 ); // const, linear, quadratic & farZ
  26. const float4 cIrisProjectionU : register( SHADER_SPECIFIC_CONST_8 );
  27. const float4 cIrisProjectionV : register( SHADER_SPECIFIC_CONST_9 );
  28. #ifdef SHADER_MODEL_VS_3_0
  29. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  30. // cMorphTargetTextureDim.z = 4tuples/morph
  31. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
  32. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
  33. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  34. #endif
  35. struct VS_INPUT
  36. {
  37. float4 vPos : POSITION; // Position
  38. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  39. float4 vBoneIndices : BLENDINDICES; // Skin indices
  40. float4 vNormal : NORMAL;
  41. float4 vTexCoord0 : TEXCOORD0; // Base (sclera) texture coordinates
  42. // Position and normal/tangent deltas
  43. float3 vPosFlex : POSITION1;
  44. float3 vNormalFlex : NORMAL1;
  45. #ifdef SHADER_MODEL_VS_3_0
  46. float vVertexID : POSITION2;
  47. #endif
  48. };
  49. struct VS_OUTPUT
  50. {
  51. float4 projPos : POSITION; // Projection-space position
  52. #if !defined( _X360 )
  53. float fog : FOG; // Fixed-function fog factor
  54. #endif
  55. float4 spotTexCoord : TEXCOORD0; // Spotlight texture coordinates
  56. float2 baseTexCoord : TEXCOORD1; // Base texture coordinates
  57. float2 irisTexCoord : TEXCOORD3; // Iris texture coordinates
  58. float3 vertAtten : TEXCOORD4; // vertex attenuation
  59. float3 worldPos : TEXCOORD5;
  60. float3 projPosXYZ : TEXCOORD7;
  61. };
  62. float RemapValClamped_01( float val, float A, float B )
  63. {
  64. float cVal = (val - A) / (B - A);
  65. cVal = saturate( cVal );
  66. return cVal;
  67. }
  68. VS_OUTPUT main( const VS_INPUT v )
  69. {
  70. VS_OUTPUT o = ( VS_OUTPUT )0;
  71. float4 vPosition = v.vPos;
  72. float3 vNormal;
  73. DecompressVertex_Normal( v.vNormal, vNormal );
  74. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  75. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal );
  76. #else
  77. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ), vPosition.xyz, vNormal );
  78. #endif
  79. // Perform skinning
  80. float3 worldNormal, worldPos;
  81. SkinPositionAndNormal(
  82. g_bSkinning,
  83. vPosition, vNormal,
  84. v.vBoneWeights, v.vBoneIndices,
  85. worldPos, worldNormal );
  86. worldNormal = normalize( worldNormal );
  87. // Transform into projection space
  88. float4 projPos = mul( float4( worldPos, 1 ), cViewProj );
  89. o.projPos = projPos;
  90. o.projPosXYZ = projPos.xyz;
  91. o.worldPos = worldPos.xyz;
  92. #if !defined( _X360 )
  93. // Set fixed-function fog factor
  94. o.fog = CalcFog( worldPos, o.projPos, g_FogType );
  95. #endif
  96. // Base texture coordinates
  97. o.baseTexCoord = v.vTexCoord0;
  98. // Spotlight texture coordinates
  99. o.spotTexCoord.x = dot( cSpotlightProj1, float4(worldPos, 1) );
  100. o.spotTexCoord.y = dot( cSpotlightProj2, float4(worldPos, 1) );
  101. o.spotTexCoord.z = dot( cSpotlightProj3, float4(worldPos, 1) );
  102. o.spotTexCoord.w = dot( cSpotlightProj4, float4(worldPos, 1) );
  103. // Compute vector to light
  104. float3 vWorldPosToLightVector = cLightPosition.xyz - worldPos;
  105. float3 vDistAtten = float3(1, 1, 1);
  106. vDistAtten.z = dot( vWorldPosToLightVector, vWorldPosToLightVector ); // distsquared
  107. vDistAtten.y = rsqrt( vDistAtten.z ); // 1 / dist
  108. float flDist = vDistAtten.z * vDistAtten.y; // dist
  109. vDistAtten.z = 1.0f / vDistAtten.z; // 1 / distsquared
  110. float fFarZ = cFlashlighAtten.w;
  111. float endFalloffFactor = RemapValClamped_01( flDist, fFarZ, 0.6 * fFarZ );
  112. o.vertAtten.xyz = endFalloffFactor * dot( vDistAtten, cFlashlighAtten.xyz );
  113. o.vertAtten *= dot( normalize( vWorldPosToLightVector ), worldNormal );
  114. o.irisTexCoord.x = dot( cIrisProjectionU, float4(worldPos, 1) );
  115. o.irisTexCoord.y = dot( cIrisProjectionV, float4(worldPos, 1) );
  116. return o;
  117. }