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. float3x3 mTangentSpaceTranspose : TEXCOORD3;
  43. // second row : TEXCOORD4;
  44. // third row : TEXCOORD5;
  45. float2 vTexCoord0 : TEXCOORD6;
  46. float4 vModelSpacePos : TEXCOORD7;
  47. };
  48. // Main
  49. VS_OUTPUT main( const VS_INPUT i )
  50. {
  51. VS_OUTPUT o;
  52. // Flexes coming in from a separate stream (contribution masked by cFlexScale.x)
  53. float4 vObjPosition = i.vPos;
  54. float3 vObjNormal;
  55. #if BUMPMAP
  56. float4 vObjTangent;
  57. DecompressVertex_NormalTangent( i.vNormal, i.vTangent, vObjNormal, vObjTangent );
  58. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  59. ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
  60. #else
  61. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ), vObjPosition.xyz, vObjNormal, vObjTangent.xyz );
  62. #endif
  63. #else // !BUMPMAP
  64. DecompressVertex_Normal( i.vNormal, vObjNormal );
  65. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  66. ApplyMorph( i.vPosFlex, i.vNormalFlex, vObjPosition.xyz, vObjNormal );
  67. #else
  68. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, i.vVertexID, float3( 0, 0, 0 ),
  69. vObjPosition.xyz, vObjNormal );
  70. #endif
  71. #endif
  72. // Transform the position
  73. float3 vWorldPosition = { 0.0f, 0.0f, 0.0f };
  74. float3 vWorldNormal = { 0.0f, 0.0f, 0.0f };
  75. float3 vWorldTangent = { 0.0f, 0.0f, 0.0f };
  76. float3 vWorldBinormal = { 0.0f, 0.0f, 0.0f };
  77. #if BUMPMAP
  78. SkinPositionNormalAndTangentSpace( g_bSkinning, vObjPosition, vObjNormal.xyz, vObjTangent.xyzw, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal, vWorldTangent, vWorldBinormal );
  79. #else
  80. SkinPositionAndNormal( g_bSkinning, vObjPosition, vObjNormal.xyz, i.vBoneWeights, i.vBoneIndices, vWorldPosition, vWorldNormal );
  81. #endif
  82. o.vWorldNormal.xyz = normalize( vWorldNormal.xyz );
  83. // Transform into projection space
  84. float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  85. o.vProjPosition = vProjPosition;
  86. // Map projected position to the refraction texture
  87. float2 vRefractPos;
  88. vRefractPos.x = vProjPosition.x;
  89. vRefractPos.y = -vProjPosition.y; // Invert Y
  90. vRefractPos = (vRefractPos + vProjPosition.w) * 0.5f;
  91. o.vProjPosForRefract.xyz = float3(vRefractPos.x, vRefractPos.y, vProjPosition.w);
  92. // View vector
  93. float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
  94. o.vWorldViewVector.xyz = vWorldViewVector.xyz;
  95. // Tangent space transform
  96. o.mTangentSpaceTranspose[0] = float3( vWorldTangent.x, vWorldBinormal.x, vWorldNormal.x );
  97. o.mTangentSpaceTranspose[1] = float3( vWorldTangent.y, vWorldBinormal.y, vWorldNormal.y );
  98. o.mTangentSpaceTranspose[2] = float3( vWorldTangent.z, vWorldBinormal.z, vWorldNormal.z );
  99. // Texture coordinates
  100. o.vTexCoord0.x = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[0] );
  101. o.vTexCoord0.y = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[1] );
  102. // model space
  103. o.vModelSpacePos = float4( i.vPos.xyz, 0) ;
  104. return o;
  105. }