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.

141 lines
3.8 KiB

  1. // STATIC: "MAXTEXTURESTAGES" "0..2"
  2. // STATIC: "HASALPHAMASK" "0..1"
  3. // STATIC: "HASSTATICTEXTURE" "0..1"
  4. // STATIC: "USEALTERNATEVIEW" "0..1"
  5. // DYNAMIC: "SKINNING" "0..1"
  6. // DYNAMIC: "ADDSTATIC" "0..1"
  7. //in multipass configurations, this specifies whether we're adding static on this pass
  8. #define TEXTURESTAGES (MAXTEXTURESTAGES + 1)
  9. #define USESTATICTEXTURE (((ADDSTATIC == 1) && (HASSTATICTEXTURE == 1))?(1):(0))
  10. #include "common_vs_fxc.h"
  11. static const int g_bSkinning = SKINNING ? true : false;
  12. #if ( USEALTERNATEVIEW == 1 )
  13. const float4x4 g_CustomViewProj : register( SHADER_SPECIFIC_CONST_0 );
  14. #endif
  15. struct VS_INPUT
  16. {
  17. float4 vPos : POSITION;
  18. float4 vBoneWeights : BLENDWEIGHT;
  19. float4 vBoneIndices : BLENDINDICES;
  20. float2 vMappingTexCoord : TEXCOORD0;
  21. };
  22. struct VS_OUTPUT
  23. {
  24. float4 vProjPos : POSITION;
  25. float2 vPrimaryTexCoord : TEXCOORD0; //either the portal cutout, or static
  26. # if( TEXTURESTAGES == 3 )
  27. # if( (HASALPHAMASK == 1) || (USESTATICTEXTURE == 1) )
  28. float2 vSecondaryTexCoord : TEXCOORD1;
  29. # if( (HASALPHAMASK == 1) && (USESTATICTEXTURE == 1) )
  30. float2 vTertiaryTexCoord : TEXCOORD2;
  31. # endif
  32. # endif
  33. # elif( TEXTURESTAGES > 1 && HASALPHAMASK == 1 )
  34. float2 vSecondaryTexCoord : TEXCOORD1;
  35. # endif
  36. # if( !defined( _X360 ) )
  37. float vFog : FOG;
  38. # endif
  39. };
  40. float2 GetPortalTextureCoordinate( float3 worldPos, float4 projPos )
  41. {
  42. float2 result;
  43. float4 vTextureProjectedPos;
  44. #if ( USEALTERNATEVIEW == 1 )
  45. vTextureProjectedPos = mul( float4( worldPos, 1 ), g_CustomViewProj );
  46. #else
  47. vTextureProjectedPos = projPos;
  48. #endif
  49. //Screen coordinates mapped back to texture coordinates for the portal texture
  50. result.x = vTextureProjectedPos.x;
  51. result.y = -vTextureProjectedPos.y; // invert Y
  52. result.xy = (result.xy + vTextureProjectedPos.w) * 0.5f;
  53. result.xy = result.xy / vTextureProjectedPos.w;
  54. #if ( USEALTERNATEVIEW == 1 )
  55. result.xy = saturate( result.xy ); //stretch instead of clipping.
  56. #endif
  57. return result;
  58. }
  59. VS_OUTPUT main( const VS_INPUT v )
  60. {
  61. VS_OUTPUT o = ( VS_OUTPUT )0;
  62. float3 worldPos;
  63. SkinPosition(
  64. g_bSkinning,
  65. v.vPos,
  66. v.vBoneWeights, v.vBoneIndices,
  67. worldPos );
  68. o.vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  69. //Because we're pairing this with ps11, we can't divide the portal cutout texture coordinates in the pixel shader by w
  70. //So, we had to divide it by w here in the vertex shader. Unfortunately that causes some interpolation inconsistencies
  71. //between the position and the texture coordinate. So we also divide the position by w here. Causing the exact same projection
  72. //errors for both position and texture coordinate, thereby eliminating the difference between the two that cause the "warping" look.
  73. o.vProjPos.xyz = o.vProjPos.xyz * (1.0f / o.vProjPos.w);
  74. o.vProjPos.w = 1.0f;
  75. #if !defined( _X360 )
  76. o.vFog = CalcFog( worldPos, o.vProjPos.xyz, FOGTYPE_RANGE );
  77. #endif
  78. # if( TEXTURESTAGES == 3 )//single pass configuration
  79. {
  80. o.vPrimaryTexCoord = GetPortalTextureCoordinate( worldPos, o.vProjPos );
  81. # if( HASALPHAMASK == 1 )
  82. {
  83. o.vSecondaryTexCoord = v.vMappingTexCoord.xy;
  84. # if( USESTATICTEXTURE == 1 )
  85. o.vTertiaryTexCoord = v.vMappingTexCoord.xy;
  86. # endif
  87. }
  88. # else
  89. {
  90. # if( USESTATICTEXTURE == 1 )
  91. o.vSecondaryTexCoord = v.vMappingTexCoord.xy;
  92. # endif
  93. }
  94. # endif
  95. }
  96. # else //multipass configuration
  97. {
  98. # if( ADDSTATIC == 0 ) //if addstatic is 0, we're rendering the cutout on this pass, if it's 1, we're rendering static on this pass
  99. {
  100. o.vPrimaryTexCoord = GetPortalTextureCoordinate( worldPos, o.vProjPos );
  101. }
  102. # else
  103. {
  104. o.vPrimaryTexCoord = v.vMappingTexCoord.xy;
  105. }
  106. # endif
  107. # if( (TEXTURESTAGES > 1) && (HASALPHAMASK == 1) ) //supports alpha mask as well
  108. o.vSecondaryTexCoord = v.vMappingTexCoord.xy;
  109. # endif
  110. }
  111. # endif
  112. return o;
  113. }