Counter Strike : Global Offensive Source Code
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.

146 lines
4.6 KiB

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