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.

147 lines
4.6 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. // STATIC: "MODEL" "0..1"
  7. // STATIC: "USE_STATIC_CONTROL_FLOW" "0..1" [vs20]
  8. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  9. // DYNAMIC: "DOWATERFOG" "0..1"
  10. // DYNAMIC: "SKINNING" "0..1"
  11. // DYNAMIC: "MORPHING" "0..1" [vs30]
  12. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20]
  13. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  14. // SKIP: $USE_STATIC_CONTROL_FLOW && ( $NUM_LIGHTS > 0 ) [vs20]
  15. #include "common_vs_fxc.h"
  16. static const bool g_bSkinning = SKINNING ? true : false;
  17. static const int g_FogType = DOWATERFOG;
  18. #ifdef SHADER_MODEL_VS_3_0
  19. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  20. // cMorphTargetTextureDim.z = 4tuples/morph
  21. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  22. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  23. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  24. #endif
  25. struct VS_INPUT
  26. {
  27. float4 vPos : POSITION;
  28. float4 vBoneWeights : BLENDWEIGHT;
  29. float4 vBoneIndices : BLENDINDICES;
  30. float4 vNormal : NORMAL;
  31. float4 vBaseTexCoord : TEXCOORD0;
  32. float4 vUserData : TANGENT;
  33. // Position and normal/tangent deltas
  34. float3 vPosFlex : POSITION1;
  35. float3 vNormalFlex : NORMAL1;
  36. #ifdef SHADER_MODEL_VS_3_0
  37. float vVertexID : POSITION2;
  38. #endif
  39. };
  40. struct VS_OUTPUT
  41. {
  42. float4 vSetupProjPos : POSITION;
  43. float fFog : FOG;
  44. float2 vBaseTexCoord : TEXCOORD0;
  45. float3x3 tangentSpaceTranspose : TEXCOORD1;
  46. // second row : TEXCOORD2;
  47. // third row : TEXCOORD3;
  48. float3 worldPos : TEXCOORD4;
  49. float3 projPos : TEXCOORD5;
  50. float4 lightAtten : TEXCOORD6;
  51. float3 vRefract : TEXCOORD7;
  52. };
  53. VS_OUTPUT main( const VS_INPUT v )
  54. {
  55. VS_OUTPUT o = ( VS_OUTPUT )0;
  56. float4 vPosition = v.vPos;
  57. float3 vNormal;
  58. float4 vTangent;
  59. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );
  60. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  61. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal, vTangent.xyz );
  62. #else
  63. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ),
  64. vPosition.xyz, vNormal, vTangent.xyz );
  65. #endif
  66. // Perform skinning
  67. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  68. SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
  69. v.vBoneWeights, v.vBoneIndices, worldPos,
  70. worldNormal, worldTangentS, worldTangentT );
  71. // Always normalize since flex path is controlled by runtime
  72. // constant not a shader combo and will always generate the normalization
  73. worldNormal = normalize( worldNormal );
  74. worldTangentS = normalize( worldTangentS );
  75. worldTangentT = normalize( worldTangentT );
  76. // Projected position
  77. float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  78. o.vSetupProjPos = vProjPos;
  79. // Map projected position to the refraction texture
  80. float2 vRefractPos;
  81. vRefractPos.x = vProjPos.x;
  82. vRefractPos.y = -vProjPos.y; // invert Y
  83. vRefractPos = (vRefractPos + vProjPos.w) * 0.5f;
  84. #if defined ( SHADER_MODEL_VS_2_0 ) && ( !USE_STATIC_CONTROL_FLOW )
  85. o.lightAtten = float4(0,0,0,0);
  86. #if ( NUM_LIGHTS > 0 )
  87. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, false );
  88. #endif
  89. #if ( NUM_LIGHTS > 1 )
  90. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, false );
  91. #endif
  92. #if ( NUM_LIGHTS > 2 )
  93. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, false );
  94. #endif
  95. #if ( NUM_LIGHTS > 3 )
  96. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, false );
  97. #endif
  98. #else
  99. // Scalar light attenuation
  100. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, true );
  101. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, true );
  102. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, true );
  103. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, true );
  104. #endif
  105. // Compute fog based on the position
  106. float3 vWorldPos = mul( v.vPos, cModel[0] );
  107. #if !defined( _X360 )
  108. o.fFog = CalcFog( vWorldPos, vProjPos, FOGTYPE_RANGE );
  109. #endif
  110. // World position
  111. o.worldPos = worldPos;
  112. // Refract position
  113. o.projPos = float3(vRefractPos.x, vRefractPos.y, vProjPos.w);
  114. // Tranform bump coordinates
  115. o.vBaseTexCoord = v.vBaseTexCoord;
  116. // Tangent space transform
  117. o.tangentSpaceTranspose[0] = float3( worldTangentS.x, worldTangentT.x, worldNormal.x );
  118. o.tangentSpaceTranspose[1] = float3( worldTangentS.y, worldTangentT.y, worldNormal.y );
  119. o.tangentSpaceTranspose[2] = float3( worldTangentS.z, worldTangentT.z, worldNormal.z );
  120. return o;
  121. }