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.

149 lines
4.7 KiB

  1. //======= Copyright � 1996-2007, Valve Corporation, All rights reserved. ======
  2. // STATIC: "INTRO" "0..1"
  3. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  4. // DYNAMIC: "DOWATERFOG" "0..1"
  5. // DYNAMIC: "SKINNING" "0..1"
  6. // DYNAMIC: "MORPHING" "0..1" [vs30]
  7. #include "vortwarp_vs20_helper.h"
  8. static const int g_FogType = DOWATERFOG;
  9. static const bool g_bSkinning = SKINNING ? true : false;
  10. const float4 cFlashlightPosition : register( SHADER_SPECIFIC_CONST_0 );
  11. const float4 cSpotlightProj1 : register( SHADER_SPECIFIC_CONST_1 );
  12. const float4 cSpotlightProj2 : register( SHADER_SPECIFIC_CONST_2 );
  13. const float4 cSpotlightProj3 : register( SHADER_SPECIFIC_CONST_3 );
  14. const float4 cSpotlightProj4 : register( SHADER_SPECIFIC_CONST_4 );
  15. const float4 cFlashlighAtten : register( SHADER_SPECIFIC_CONST_5 ); // const, linear, quadratic & farZ
  16. const float4 cTeethLighting : register( SHADER_SPECIFIC_CONST_8 );
  17. #if INTRO
  18. const float4 const4 : register( SHADER_SPECIFIC_CONST_9 );
  19. #define g_Time const4.w
  20. #define modelOrigin const4.xyz
  21. #endif
  22. #ifdef SHADER_MODEL_VS_3_0
  23. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  24. // cMorphTargetTextureDim.z = 4tuples/morph
  25. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  26. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  27. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  28. #endif
  29. struct VS_INPUT
  30. {
  31. // This is all of the stuff that we ever use.
  32. float4 vPos : POSITION;
  33. float4 vBoneWeights : BLENDWEIGHT;
  34. float4 vBoneIndices : BLENDINDICES;
  35. float4 vNormal : NORMAL;
  36. float2 vTexCoord0 : TEXCOORD0;
  37. // Position and normal/tangent deltas
  38. float3 vPosFlex : POSITION1;
  39. float3 vNormalFlex : NORMAL1;
  40. #ifdef SHADER_MODEL_VS_3_0
  41. float vVertexID : POSITION2;
  42. #endif
  43. };
  44. struct VS_OUTPUT
  45. {
  46. float4 projPos : POSITION;
  47. #if !defined( _X360 )
  48. float fog : FOG;
  49. #endif
  50. float2 baseTexCoord : TEXCOORD0; // Base texture coordinates
  51. float4 spotTexCoord : TEXCOORD1; // Spotlight texture coordinates
  52. float3 vertAtten : TEXCOORD2; // Distance/spot attenuation
  53. float4 vProjPos : TEXCOORD3; // Projective space position
  54. float3 worldPos : TEXCOORD4; // Necessary for pixel fog
  55. };
  56. float RemapValClamped_01( float val, float A, float B )
  57. {
  58. float cVal = (val - A) / (B - A);
  59. cVal = saturate( cVal );
  60. return cVal;
  61. }
  62. VS_OUTPUT main( const VS_INPUT v )
  63. {
  64. VS_OUTPUT o = ( VS_OUTPUT )0;
  65. float4 vPosition = v.vPos;
  66. float3 vNormal;
  67. DecompressVertex_Normal( v.vNormal, vNormal );
  68. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  69. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal );
  70. #else
  71. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ), vPosition.xyz, vNormal );
  72. #endif
  73. // Normalize the flexed normal
  74. vNormal.xyz = normalize( vNormal.xyz );
  75. // Transform the position
  76. float3 worldPos, worldNormal;
  77. SkinPositionAndNormal( g_bSkinning, vPosition, vNormal, v.vBoneWeights, v.vBoneIndices, worldPos, worldNormal );
  78. #if INTRO
  79. float3 dummy = float3( 0.0f, 0.0f, 0.0f );
  80. WorldSpaceVertexProcess( g_Time, modelOrigin, worldPos, worldNormal, dummy, dummy );
  81. #endif
  82. // Transform into projection space
  83. o.projPos = mul( float4( worldPos, 1 ), cViewProj );
  84. o.worldPos = worldPos.xyz;
  85. o.vProjPos = o.projPos;
  86. #if !defined( _X360 )
  87. // Set fixed-function fog factor
  88. o.fog = CalcFog( worldPos, o.projPos, g_FogType );
  89. #endif
  90. // Spotlight texture coordinates
  91. o.spotTexCoord.x = dot( cSpotlightProj1, float4(worldPos, 1) );
  92. o.spotTexCoord.y = dot( cSpotlightProj2, float4(worldPos, 1) );
  93. o.spotTexCoord.z = dot( cSpotlightProj3, float4(worldPos, 1) );
  94. o.spotTexCoord.w = dot( cSpotlightProj4, float4(worldPos, 1) );
  95. // Compute vector to light
  96. float3 vWorldPosToLightVector = cFlashlightPosition.xyz - worldPos;
  97. float3 vDistAtten = float3(1, 1, 1);
  98. vDistAtten.z = dot( vWorldPosToLightVector, vWorldPosToLightVector );
  99. vDistAtten.y = rsqrt( vDistAtten.z );
  100. float flDist = vDistAtten.z * vDistAtten.y; // Distance to light
  101. vDistAtten.z = 1.0f / vDistAtten.z; // 1 / distsquared
  102. float fFarZ = cFlashlighAtten.w;
  103. float NdotL = saturate( dot( worldNormal, normalize( vWorldPosToLightVector ) ) );
  104. float endFalloffFactor = RemapValClamped_01( flDist, fFarZ, 0.6 * fFarZ );
  105. o.vertAtten.xyz = endFalloffFactor * dot( vDistAtten, cFlashlighAtten.xyz );
  106. // Final attenuation from flashlight only...
  107. float linearAtten = NdotL * dot( vDistAtten, cFlashlighAtten.xyz ) * endFalloffFactor;
  108. // Forward vector
  109. float3 vForward = cTeethLighting.xyz;
  110. float fIllumFactor = cTeethLighting.w;
  111. // Modulate flashlight by mouth darkening
  112. o.vertAtten = linearAtten * fIllumFactor * saturate( dot( worldNormal, vForward ) );
  113. o.baseTexCoord = v.vTexCoord0;
  114. return o;
  115. }