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.

140 lines
4.0 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. // STATIC: "MODEL" "0..1"
  7. // STATIC: "COLORMODULATE" "0..1"
  8. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  9. // DYNAMIC: "SKINNING" "0..1"
  10. #include "common_vs_fxc.h"
  11. static const bool g_bSkinning = SKINNING ? true : false;
  12. static const bool g_bModel = MODEL ? true : false;
  13. const float4 cBumpTexCoordTransform[4] : register( SHADER_SPECIFIC_CONST_1 );
  14. const float g_flTime : register( SHADER_SPECIFIC_CONST_5 );
  15. struct VS_INPUT
  16. {
  17. float4 vPos : POSITION;
  18. float4 vBoneWeights : BLENDWEIGHT;
  19. float4 vBoneIndices : BLENDINDICES;
  20. float4 vNormal : NORMAL;
  21. float4 vBaseTexCoord : TEXCOORD0;
  22. #if !MODEL
  23. float3 vTangentS : TANGENT;
  24. float3 vTangentT : BINORMAL0;
  25. #else
  26. float4 vUserData : TANGENT;
  27. #endif
  28. #if COLORMODULATE
  29. float4 vColor : COLOR0;
  30. #endif
  31. };
  32. struct VS_OUTPUT
  33. {
  34. float4 vProjPos_POSITION : POSITION;
  35. #if !defined( _X360 )
  36. float vFog : FOG;
  37. #endif
  38. float4 vBumpTexCoord : TEXCOORD0;
  39. float3 vTangentEyeVect : TEXCOORD1;
  40. float3 vWorldNormal : TEXCOORD2;
  41. float3 vWorldTangent : TEXCOORD3;
  42. float3 vWorldBinormal : TEXCOORD4;
  43. float3 vRefractXYW : TEXCOORD5;
  44. float3 vWorldViewVector : TEXCOORD6;
  45. #if COLORMODULATE
  46. float4 vColor : COLOR0;
  47. #endif
  48. float4 fogFactorW : COLOR1;
  49. float4 worldPos_projPosZ : TEXCOORD7; // Necessary for pixel fog
  50. };
  51. VS_OUTPUT main( const VS_INPUT v )
  52. {
  53. VS_OUTPUT o = ( VS_OUTPUT )0;
  54. #if COLORMODULATE
  55. o.vColor = v.vColor;
  56. #endif
  57. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  58. float3 vObjNormal;
  59. #if MODEL
  60. float4 vObjTangent;
  61. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vObjNormal, vObjTangent );
  62. SkinPositionNormalAndTangentSpace(
  63. g_bSkinning,
  64. v.vPos, vObjNormal, vObjTangent,
  65. v.vBoneWeights, v.vBoneIndices,
  66. worldPos, worldNormal, worldTangentS, worldTangentT );
  67. #else
  68. DecompressVertex_Normal( v.vNormal, vObjNormal );
  69. worldPos = mul( v.vPos, cModel[0] );
  70. worldTangentS = mul( v.vTangentS, ( const float3x3 )cModel[0] );
  71. worldTangentT = mul( v.vTangentT, ( const float3x3 )cModel[0] );
  72. worldNormal = mul( vObjNormal, ( float3x3 )cModel[0] );
  73. #endif
  74. // World normal
  75. o.vWorldNormal.xyz = normalize( worldNormal.xyz );
  76. // Projected position
  77. float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  78. o.vProjPos_POSITION = vProjPos;
  79. vProjPos.z = dot( float4( worldPos, 1 ), cViewProjZ );
  80. o.worldPos_projPosZ = float4( worldPos.xyz, vProjPos.z );
  81. //o.projNormal.xyz = mul( worldNormal, cViewProj );
  82. // Map projected position to the refraction texture
  83. float2 vRefractPos;
  84. vRefractPos.x = vProjPos.x;
  85. vRefractPos.y = -vProjPos.y; // invert Y
  86. vRefractPos = (vRefractPos + vProjPos.w) * 0.5f;
  87. // Refraction transform
  88. o.vRefractXYW = float3(vRefractPos.x, vRefractPos.y, vProjPos.w);
  89. // Compute fog based on the position
  90. float3 vWorldPos = mul( v.vPos, cModel[0] );
  91. o.fogFactorW = CalcFog( vWorldPos, vProjPos, FOGTYPE_RANGE );
  92. #if !defined( _X360 )
  93. o.vFog = o.fogFactorW;
  94. #endif
  95. // Eye vector
  96. float3 vWorldEyeVect = normalize( cEyePos - vWorldPos );
  97. o.vWorldViewVector.xyz = -vWorldEyeVect.xyz;
  98. // Transform to the tangent space
  99. o.vTangentEyeVect.x = dot( vWorldEyeVect, worldTangentS );
  100. o.vTangentEyeVect.y = dot( vWorldEyeVect, worldTangentT );
  101. o.vTangentEyeVect.z = dot( vWorldEyeVect, worldNormal );
  102. // Tranform bump coordinates
  103. o.vBumpTexCoord.x = dot( v.vBaseTexCoord, cBumpTexCoordTransform[0] );
  104. o.vBumpTexCoord.y = dot( v.vBaseTexCoord, cBumpTexCoordTransform[1] );
  105. // Tranform bump coordinates (note wz, not zw)
  106. o.vBumpTexCoord.w = dot( v.vBaseTexCoord, cBumpTexCoordTransform[2] );
  107. o.vBumpTexCoord.z = dot( v.vBaseTexCoord, cBumpTexCoordTransform[3] );
  108. // Tangent space transform
  109. o.vWorldNormal.xyz = normalize( worldNormal.xyz );
  110. o.vWorldTangent.xyz = worldTangentS.xyz;
  111. o.vWorldBinormal.xyz = worldTangentT.xyz;
  112. return o;
  113. }