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.

217 lines
8.8 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. // STATIC: "INTRO" "0..1"
  3. // STATIC: "HALFLAMBERT" "0..1"
  4. // STATIC: "FLASHLIGHT" "0..1"
  5. // STATIC: "LIGHTWARPTEXTURE" "0..1"
  6. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  7. // DYNAMIC: "SKINNING" "0..1"
  8. // DYNAMIC: "DOWATERFOG" "0..1"
  9. // DYNAMIC: "DYNAMIC_LIGHT" "0..1"
  10. // DYNAMIC: "STATIC_LIGHT" "0..1"
  11. // DYNAMIC: "NUM_LIGHTS" "0..4"
  12. // DYNAMIC: "MORPHING" "0..1" [vs30]
  13. #include "vortwarp_vs20_helper.h"
  14. static const bool g_bSkinning = SKINNING ? true : false;
  15. static const int g_iFogType = DOWATERFOG;
  16. static const bool g_bHalfLambert = HALFLAMBERT ? true : false;
  17. const float3 g_cEyeOrigin : register( SHADER_SPECIFIC_CONST_0 );
  18. const float4 g_vIrisProjectionU : register( SHADER_SPECIFIC_CONST_2 );
  19. const float4 g_vIrisProjectionV : register( SHADER_SPECIFIC_CONST_3 );
  20. const float4 g_vFlashlightPosition : register( SHADER_SPECIFIC_CONST_4 );
  21. #if INTRO
  22. const float4 g_vConst4 : register( SHADER_SPECIFIC_CONST_5 );
  23. #define g_vModelOrigin g_vConst4.xyz
  24. #define g_flTime g_vConst4.w
  25. #endif
  26. const float4 g_vFlashlightMatrixRow1 : register( SHADER_SPECIFIC_CONST_6 );
  27. const float4 g_vFlashlightMatrixRow2 : register( SHADER_SPECIFIC_CONST_7 );
  28. const float4 g_vFlashlightMatrixRow3 : register( SHADER_SPECIFIC_CONST_8 );
  29. const float4 g_vFlashlightMatrixRow4 : register( SHADER_SPECIFIC_CONST_9 );
  30. #ifdef SHADER_MODEL_VS_3_0
  31. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  32. // cMorphTargetTextureDim.z = 4tuples/morph
  33. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
  34. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
  35. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  36. #endif
  37. struct VS_INPUT
  38. {
  39. float4 vPos : POSITION; // Position
  40. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  41. float4 vBoneIndices : BLENDINDICES; // Skin indices
  42. float4 vTexCoord0 : TEXCOORD0; // Base (sclera) texture coordinates
  43. // Position deltas
  44. float3 vPosFlex : POSITION1;
  45. #ifdef SHADER_MODEL_VS_3_0
  46. float vVertexID : POSITION2;
  47. #endif
  48. };
  49. struct VS_OUTPUT
  50. {
  51. float4 projPos : POSITION; // Projection-space position
  52. #if !defined( _X360 )
  53. float fog : FOG; // Fixed-function fog factor
  54. #endif
  55. float4 vAmbientOcclUv_fallbackCorneaUv : TEXCOORD0; // Base texture coordinate
  56. float4 cVertexLight : TEXCOORD1; // Vertex-lit color (Note: w is used for flashlight pass)
  57. float4 vTangentViewVector : TEXCOORD2; // Tangent view vector (Note: w is used for flashlight pass)
  58. float4 vWorldPosition_ProjPosZ : TEXCOORD3;
  59. float3 vWorldNormal : TEXCOORD4; // World-space normal
  60. float3 vWorldTangent : TEXCOORD5; // World-space tangent
  61. float4 vLightFalloffCosine01 : TEXCOORD6; // Light falloff and cosine terms for first two local lights
  62. float4 vLightFalloffCosine23 : TEXCOORD7; // Light falloff and cosine terms for next two local lights
  63. float3 vWorldBinormal : COLOR0; // World-space normal
  64. };
  65. VS_OUTPUT main( const VS_INPUT v )
  66. {
  67. VS_OUTPUT o;
  68. bool bDynamicLight = DYNAMIC_LIGHT ? true : false;
  69. bool bStaticLight = STATIC_LIGHT ? true : false;
  70. int nNumLights = NUM_LIGHTS;
  71. float4 vPosition = v.vPos;
  72. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  73. ApplyMorph( v.vPosFlex, vPosition.xyz );
  74. #else
  75. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ), vPosition.xyz );
  76. #endif
  77. // Transform the position
  78. float3 vWorldPosition;
  79. SkinPosition( g_bSkinning, vPosition, v.vBoneWeights, v.vBoneIndices, vWorldPosition );
  80. // Note: I'm relying on the iris projection vector math not changing or this will break
  81. float3 vEyeSocketUpVector = normalize( -g_vIrisProjectionV.xyz );
  82. float3 vEyeSocketLeftVector = normalize( -g_vIrisProjectionU.xyz );
  83. #if INTRO
  84. float3 dummy = float3( 0.0f, 0.0f, 0.0f );
  85. WorldSpaceVertexProcess( g_flTime, g_vModelOrigin, vWorldPosition, dummy, dummy, dummy );
  86. #endif
  87. o.vWorldPosition_ProjPosZ.xyz = vWorldPosition.xyz;
  88. // Transform into projection space
  89. //vWorldPosition -= ( vWorldPosition - g_cEyeOrigin ) * 0.9; //Debug to visualize eye origin
  90. float4 vProjPos = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  91. o.projPos = vProjPos;
  92. vProjPos.z = dot( float4( vWorldPosition, 1.0f ), cViewProjZ );
  93. o.vWorldPosition_ProjPosZ.w = vProjPos.z;
  94. #if !defined( _X360 )
  95. // Set fixed-function fog factor
  96. o.fog = CalcFog( vWorldPosition, vProjPos, g_iFogType );
  97. #endif
  98. // Normal = (Pos - Eye origin)
  99. float3 vWorldNormal = normalize( vWorldPosition.xyz - g_cEyeOrigin.xyz );
  100. o.vWorldNormal.xyz = vWorldNormal.xyz;
  101. // Tangent & binormal
  102. /*
  103. float3 vWorldBinormal = normalize( cross( vWorldNormal.xyz, vEyeSocketLeftVector.xyz ) );
  104. o.vWorldBinormal.xyz = vWorldBinormal.xyz * 0.5f + 0.5f;
  105. float3 vWorldTangent = normalize( cross( vWorldBinormal.xyz, vWorldNormal.xyz ) );
  106. o.vWorldTangent.xyz = vWorldTangent.xyz;
  107. //*/
  108. //*
  109. float3 vWorldTangent = normalize( cross( vEyeSocketUpVector.xyz, vWorldNormal.xyz ) );
  110. o.vWorldTangent.xyz = vWorldTangent.xyz;
  111. float3 vWorldBinormal = normalize( cross( vWorldNormal.xyz, vWorldTangent.xyz ) );
  112. o.vWorldBinormal.xyz = vWorldBinormal.xyz * 0.5f + 0.5f;
  113. //*/
  114. float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
  115. o.vTangentViewVector.xyz = Vec3WorldToTangentNormalized (vWorldViewVector.xyz, vWorldNormal.xyz, vWorldTangent.xyz, vWorldBinormal.xyz);
  116. // AV - I think this will effectively make the eyeball less rounded left to right to help vertext lighting quality
  117. // AV - Note: This probably won't look good if put on an exposed eyeball
  118. //float vNormalDotSideVec = -dot( vWorldNormal, g_vEyeballUp ) * 0.5f;
  119. float vNormalDotSideVec = -dot( vWorldNormal, vEyeSocketLeftVector) * 0.5f;
  120. float3 vBentWorldNormal = normalize(vNormalDotSideVec * vEyeSocketLeftVector + vWorldNormal);
  121. // Compute vertex lighting
  122. o.cVertexLight.a = 0.0f; //Only used for flashlight pass
  123. o.cVertexLight.rgb = DoLightingUnrolled( vWorldPosition, vBentWorldNormal, float3(0.0f, 0.0f, 0.0f), bStaticLight, bDynamicLight, g_bHalfLambert, nNumLights );
  124. // Only interpolate ambient light for TF NPR lighting
  125. bool bDoDiffuseWarp = LIGHTWARPTEXTURE ? true : false;
  126. if ( bDoDiffuseWarp )
  127. {
  128. if( bDynamicLight )
  129. {
  130. o.cVertexLight.rgb = AmbientLight( vBentWorldNormal.xyz );
  131. }
  132. else
  133. {
  134. o.cVertexLight.rgb = float3( 0.0f, 0.0f, 0.0f );
  135. }
  136. }
  137. // NOTE: it appears that o.vLightFalloffCosine01 and o.vLightFalloffCosine23 are filled in even if
  138. // we don't have enough lights, meaning we pass garbage to the pixel shader which then throws it away
  139. // Light falloff for first two local lights
  140. o.vLightFalloffCosine01.x = VertexAttenInternal( vWorldPosition.xyz, 0 );
  141. o.vLightFalloffCosine01.y = VertexAttenInternal( vWorldPosition.xyz, 1 );
  142. o.vLightFalloffCosine01.z = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 0, g_bHalfLambert );
  143. o.vLightFalloffCosine01.w = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 1, g_bHalfLambert );
  144. // Light falloff for next two local lights
  145. o.vLightFalloffCosine23.x = VertexAttenInternal( vWorldPosition.xyz, 2 );
  146. o.vLightFalloffCosine23.y = VertexAttenInternal( vWorldPosition.xyz, 3 );
  147. o.vLightFalloffCosine23.z = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 2, g_bHalfLambert );
  148. o.vLightFalloffCosine23.w = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 3, g_bHalfLambert );
  149. // Texture coordinates set by artists for ambient occlusion
  150. o.vAmbientOcclUv_fallbackCorneaUv.xy = v.vTexCoord0.xy;
  151. // Cornea uv for ps.2.0 fallback
  152. float2 vCorneaUv; // Note: Cornea texture is a cropped version of the iris texture
  153. vCorneaUv.x = dot( g_vIrisProjectionU, float4( vWorldPosition, 1.0f ) );
  154. vCorneaUv.y = dot( g_vIrisProjectionV, float4( vWorldPosition, 1.0f ) );
  155. float2 vSphereUv = ( vCorneaUv.xy * 0.5f ) + 0.25f;
  156. o.vAmbientOcclUv_fallbackCorneaUv.wz = vCorneaUv.xy; // Note: wz unpacks faster than zw in ps.2.0!
  157. // Step on the vertex light interpolator for the flashlight tex coords
  158. bool bFlashlight = ( FLASHLIGHT != 0 ) ? true : false;
  159. o.vTangentViewVector.w = 0.0f;
  160. if ( bFlashlight )
  161. {
  162. o.cVertexLight.x = dot( g_vFlashlightMatrixRow1.xyzw, float4( vWorldPosition, 1.0f ) );
  163. o.cVertexLight.y = dot( g_vFlashlightMatrixRow2.xyzw, float4( vWorldPosition, 1.0f ) );
  164. o.cVertexLight.z = dot( g_vFlashlightMatrixRow3.xyzw, float4( vWorldPosition, 1.0f ) );
  165. o.cVertexLight.w = dot( g_vFlashlightMatrixRow4.xyzw, float4( vWorldPosition, 1.0f ) );
  166. o.vTangentViewVector.w = saturate( dot( vBentWorldNormal.xyz, normalize ( g_vFlashlightPosition.xyz - vWorldPosition.xyz ) ) ); // Flashlight N.L with modified normal
  167. // Half lambert version
  168. //o.cVertexLight.z = dot( vBentWorldNormal.xyz, normalize ( g_vFlashlightPosition.xyz - vWorldPosition.xyz ) ); // Flashlight N.L with modified normal
  169. //o.cVertexLight.z = ( o.cVertexLight.z * 0.5f ) + 0.5f;
  170. //o.cVertexLight.z *= o.cVertexLight.z;
  171. }
  172. return o;
  173. }