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.

132 lines
4.3 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. // STATIC: "BUMPMAP" "0..1"
  3. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  4. // DYNAMIC: "SKINNING" "0..1"
  5. // DYNAMIC: "MORPHING" "0..1" [vs30]
  6. // Includes
  7. #include "common_vs_fxc.h"
  8. // Globals
  9. static const bool g_bSkinning = SKINNING ? true : false;
  10. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  11. #ifdef SHADER_MODEL_VS_3_0
  12. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  13. // cMorphTargetTextureDim.z = 4tuples/morph
  14. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  15. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  16. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  17. #endif
  18. // Structs
  19. struct VS_INPUT
  20. {
  21. float4 vPos : POSITION; // Position
  22. float4 vNormal : NORMAL; // Normal
  23. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  24. float4 vBoneIndices : BLENDINDICES; // Skin indices
  25. float4 vTexCoord0 : TEXCOORD0; // Base texture coordinates
  26. #if BUMPMAP
  27. float4 vTangent : TANGENT;
  28. #endif
  29. // Position and normal/tangent deltas
  30. float3 vPosFlex : POSITION1;
  31. float3 vNormalFlex : NORMAL1;
  32. #ifdef SHADER_MODEL_VS_3_0
  33. float vVertexID : POSITION2;
  34. #endif
  35. };
  36. struct VS_OUTPUT
  37. {
  38. float4 vProjPosition : POSITION; // Projection-space position
  39. float3 vWorldNormal : TEXCOORD0; // World-space normal
  40. float3 vProjPosForRefract : TEXCOORD1;
  41. float3 vWorldViewVector : TEXCOORD2;
  42. #if BUMPMAP
  43. float3x3 mTangentSpaceTranspose : TEXCOORD3;
  44. // second row : TEXCOORD4;
  45. // third row : TEXCOORD5;
  46. float2 vTexCoord0 : TEXCOORD6;
  47. #endif
  48. };
  49. // Main
  50. VS_OUTPUT main( const VS_INPUT i )
  51. {
  52. VS_OUTPUT o;
  53. // Flexes coming in from a separate stream (contribution masked by cFlexScale.x)
  54. float4 vObjPosition = i.vPos;
  55. float3 vObjNormal;
  56. #if BUMPMAP
  57. float4 vObjTangent;
  58. DecompressVertex_NormalTangent( i.vNormal, i.vTangent, vObjNormal, vObjTangent );
  59. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  60. ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
  61. #else
  62. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ), vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
  63. #endif
  64. #else // !BUMPMAP
  65. DecompressVertex_Normal( i.vNormal, vObjNormal );
  66. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  67. ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal );
  68. #else
  69. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ),
  70. vObjPosition.xyz, vObjNormal );
  71. #endif
  72. #endif
  73. // Transform the position
  74. float3 vWorldPosition = { 0.0f, 0.0f, 0.0f };
  75. float3 vWorldNormal = { 0.0f, 0.0f, 0.0f };
  76. #if BUMPMAP
  77. float3 vWorldTangent = { 0.0f, 0.0f, 0.0f };
  78. float3 vWorldBinormal = { 0.0f, 0.0f, 0.0f };
  79. SkinPositionNormalAndTangentSpace( g_bSkinning, vObjPosition, vObjNormal.xyz, vObjTangent.xyzw, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal, vWorldTangent, vWorldBinormal );
  80. #else
  81. SkinPositionAndNormal( g_bSkinning, vObjPosition, vObjNormal.xyz, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal );
  82. #endif
  83. o.vWorldNormal.xyz = normalize( vWorldNormal.xyz );
  84. // Transform into projection space
  85. float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  86. o.vProjPosition = vProjPosition;
  87. // Map projected position to the refraction texture
  88. float2 vRefractPos;
  89. vRefractPos.x = vProjPosition.x;
  90. vRefractPos.y = -vProjPosition.y; // Invert Y
  91. vRefractPos = (vRefractPos + vProjPosition.w) * 0.5f;
  92. o.vProjPosForRefract.xyz = float3(vRefractPos.x, vRefractPos.y, vProjPosition.w);
  93. // View vector
  94. float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
  95. o.vWorldViewVector.xyz = vWorldViewVector.xyz;
  96. // Tangent space transform
  97. #if BUMPMAP
  98. o.mTangentSpaceTranspose[0] = float3( vWorldTangent.x, vWorldBinormal.x, vWorldNormal.x );
  99. o.mTangentSpaceTranspose[1] = float3( vWorldTangent.y, vWorldBinormal.y, vWorldNormal.y );
  100. o.mTangentSpaceTranspose[2] = float3( vWorldTangent.z, vWorldBinormal.z, vWorldNormal.z );
  101. #endif
  102. // Texture coordinates
  103. #if BUMPMAP
  104. o.vTexCoord0.x = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[0] );
  105. o.vTexCoord0.y = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[1] );
  106. #endif
  107. return o;
  108. }