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.

156 lines
4.4 KiB

  1. // DYNAMIC: "SKINNING" "0..1"
  2. #include "common_vs_fxc.h"
  3. static const bool g_bSkinning = SKINNING ? true : false;
  4. const float4 cBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_1 );
  5. const float g_CoreColorTexCoordOffset : register( SHADER_SPECIFIC_CONST_3 );
  6. struct VS_INPUT
  7. {
  8. float4 vPos : POSITION;
  9. float4 vBoneWeights : BLENDWEIGHT;
  10. float4 vBoneIndices : BLENDINDICES;
  11. float3 vNormal : NORMAL;
  12. float4 vBaseTexCoord : TEXCOORD0;
  13. float4 vUserData : TANGENT;
  14. };
  15. struct VS_OUTPUT
  16. {
  17. float4 vProjPos : POSITION;
  18. float vFog : FOG;
  19. float4 oT0 : TEXCOORD0;
  20. float4 oT1 : TEXCOORD1;
  21. float4 oT2 : TEXCOORD2;
  22. // float3 oT3 : TEXCOORD3;
  23. float4 fogFactorW : COLOR1;
  24. };
  25. float LengthThroughSphere( float3 vecRayOrigin, float3 vecRayDelta,
  26. float3 vecSphereCenter, float flRadius, out float alpha )
  27. {
  28. // Solve using the ray equation + the sphere equation
  29. // P = o + dt
  30. // (x - xc)^2 + (y - yc)^2 + (z - zc)^2 = r^2
  31. // (ox + dx * t - xc)^2 + (oy + dy * t - yc)^2 + (oz + dz * t - zc)^2 = r^2
  32. // (ox - xc)^2 + 2 * (ox-xc) * dx * t + dx^2 * t^2 +
  33. // (oy - yc)^2 + 2 * (oy-yc) * dy * t + dy^2 * t^2 +
  34. // (oz - zc)^2 + 2 * (oz-zc) * dz * t + dz^2 * t^2 = r^2
  35. // (dx^2 + dy^2 + dz^2) * t^2 + 2 * ((ox-xc)dx + (oy-yc)dy + (oz-zc)dz) t +
  36. // (ox-xc)^2 + (oy-yc)^2 + (oz-zc)^2 - r^2 = 0
  37. // or, t = (-b +/- sqrt( b^2 - 4ac)) / 2a
  38. // a = DotProduct( vecRayDelta, vecRayDelta );
  39. // b = 2 * DotProduct( vecRayOrigin - vecCenter, vecRayDelta )
  40. // c = DotProduct(vecRayOrigin - vecCenter, vecRayOrigin - vecCenter) - flRadius * flRadius;
  41. float3 vecSphereToRay;
  42. vecSphereToRay = vecRayOrigin - vecSphereCenter;
  43. float a = dot( vecRayDelta, vecRayDelta );
  44. // This would occur in the case of a zero-length ray
  45. // if ( a == 0.0f )
  46. // {
  47. // *pT1 = *pT2 = 0.0f;
  48. // return vecSphereToRay.LengthSqr() <= flRadius * flRadius;
  49. // }
  50. float b = 2 * dot( vecSphereToRay, vecRayDelta );
  51. float c = dot( vecSphereToRay, vecSphereToRay ) - flRadius * flRadius;
  52. float flDiscrim = b * b - 4 * a * c;
  53. // if ( flDiscrim < 0.0f )
  54. // return 0.0f;
  55. float hack = flDiscrim;
  56. flDiscrim = sqrt( flDiscrim );
  57. float oo2a = 0.5f / a;
  58. if( hack < 0.0f )
  59. {
  60. alpha = 0.0f;
  61. return 0.0f;
  62. }
  63. else
  64. {
  65. alpha = 1.0f;
  66. return abs( flDiscrim ) * 2 * oo2a;
  67. }
  68. // *pT1 = ( - b - flDiscrim ) * oo2a;
  69. // *pT2 = ( - b + flDiscrim ) * oo2a;
  70. // return true;
  71. }
  72. VS_OUTPUT main( const VS_INPUT v )
  73. {
  74. VS_OUTPUT o = ( VS_OUTPUT )0;
  75. float4 projPos;
  76. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  77. SkinPositionNormalAndTangentSpace(
  78. g_bSkinning,
  79. v.vPos, v.vNormal, v.vUserData,
  80. v.vBoneWeights, v.vBoneIndices,
  81. worldPos, worldNormal, worldTangentS, worldTangentT );
  82. // Projected position
  83. o.vProjPos = projPos = mul( float4( worldPos, 1 ), cViewProj );
  84. // calculate fog
  85. o.fogFactorW = o.vFog = CalcFog( worldPos, projPos, FOGTYPE_RANGE );
  86. // Eye vector
  87. float3 vWorldEyeVect = cEyePos - worldPos;
  88. // Transform to the tangent space
  89. o.oT1.x = dot( vWorldEyeVect, worldTangentS );
  90. o.oT1.y = dot( vWorldEyeVect, worldTangentT );
  91. o.oT1.z = dot( vWorldEyeVect, worldNormal );
  92. // Tranform bump coordinates
  93. float2 bumpTexCoord;
  94. bumpTexCoord.x = dot( v.vBaseTexCoord, cBumpTexCoordTransform[0] );
  95. bumpTexCoord.y = dot( v.vBaseTexCoord, cBumpTexCoordTransform[1] );
  96. // dudv map
  97. o.oT0.xy = bumpTexCoord;
  98. // flip Y by multiplying by -1
  99. projPos.y *= -1.0f;
  100. // transform from [-w,w] to [0,2*w]
  101. // The reason this is w is because we are in perspective space/homogenous clip space.
  102. projPos.xy += projPos.w;
  103. // transform from [0,2*w] to [0,w]
  104. // We'll end up dividing by w in the pixel shader to get to [0,1]
  105. projPos.xy *= 0.5f;
  106. o.oT1.xy = projPos.xy;
  107. // emit w to both z and w in case the driver screws up and divides by z
  108. o.oT1.z = o.oT1.w = projPos.w;
  109. // hack
  110. float3 g_SphereCenter = { 2688.0f, 12139.0f, 5170.0f };
  111. // float g_SphereDiameter = 430.0f;
  112. float g_SphereDiameter = 530.0f;
  113. float g_SphereRadius = g_SphereDiameter * 0.5f;
  114. float dummyAlpha;
  115. float lengthThroughSphere = LengthThroughSphere( cEyePos, normalize( vWorldEyeVect ),
  116. g_SphereCenter, g_SphereRadius, dummyAlpha );
  117. float normalizedLengthThroughSphere = saturate( lengthThroughSphere / g_SphereDiameter );
  118. o.oT2.xy = saturate( float2( normalizedLengthThroughSphere, g_CoreColorTexCoordOffset ) );
  119. // hack texcoord shit
  120. // o.oT0.xy = 3.0f * ( worldPos.xy - g_SphereCenter.xy ) / g_SphereRadius;
  121. return o;
  122. }