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.

140 lines
4.7 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "STAGE" "0..2"
  3. // STATIC: "USEONSTATICPROP" "0..1"
  4. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  5. // Includes
  6. #include "common_vs_fxc.h"
  7. // Globals
  8. const float g_flTime : register( SHADER_SPECIFIC_CONST_0 );
  9. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_1 );
  10. const float2 g_vConst3 : register( SHADER_SPECIFIC_CONST_3 );
  11. #define g_flPortalOpenAmount g_vConst3.x
  12. #define g_flPortalStaticAmount g_vConst3.y
  13. const float g_vIsRecursivePortalView : register( SHADER_SPECIFIC_CONST_4 );
  14. // Structs
  15. struct VS_INPUT
  16. {
  17. float4 vPos : POSITION; // Position
  18. #if ( USEONSTATICPROP == 0 )
  19. float4 vNormal : NORMAL; // Normal
  20. #endif
  21. float2 vTexCoord0 : TEXCOORD0; // Base texture coordinates
  22. #if ( USEONSTATICPROP == 0 )
  23. float flDecalOffset : TEXCOORD1; // How far to move each vertex along the normal
  24. float4 vTangent : TANGENT; // Flip in w
  25. #endif
  26. };
  27. struct VS_OUTPUT
  28. {
  29. float4 vProjPosition : POSITION; // Projection-space position
  30. float2 vUv0 : TEXCOORD0;
  31. float3 vWorldTangent : TEXCOORD1;
  32. float3 vWorldBinormal : TEXCOORD2;
  33. float4 vWorldPosition : TEXCOORD3; // Proj pos z in w
  34. float3 vProjPosForRefract : TEXCOORD4;
  35. float4 vNoiseTexCoord : TEXCOORD5;
  36. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  37. float fog : FOG;
  38. #endif
  39. };
  40. // Main
  41. VS_OUTPUT main( const VS_INPUT i )
  42. {
  43. float kFlPortalOuterBorder = 0.075f; // Must match PS!
  44. VS_OUTPUT o = (VS_OUTPUT)0;
  45. float3 vObjNormal;
  46. #if ( USEONSTATICPROP == 0 )
  47. {
  48. DecompressVertex_Normal( i.vNormal, vObjNormal );
  49. }
  50. #endif
  51. // Move the portal decal away from the base surface a bit to avoid z fighting
  52. float4 vObjPos = i.vPos.xyzw;
  53. #if ( USEONSTATICPROP == 0 )
  54. {
  55. float flDecalOffset = 1.0f; // This offset is used in recursive portal views when using fast clipping (e.g. on the PS3).
  56. // It prevents ugly Z fighting between the portal and the surface it's on due to reduced Z precision when
  57. // a player is really really close to a portal.
  58. if ( g_vIsRecursivePortalView == 0.0f )
  59. {
  60. flDecalOffset = i.flDecalOffset;
  61. }
  62. vObjPos.xyz += flDecalOffset * vObjNormal.xyz;
  63. }
  64. #endif
  65. // Transform the position
  66. float3 vWorldPosition = mul4x3( vObjPos, cModel[0] );
  67. #if ( USEONSTATICPROP == 0 )
  68. float3 vWorldNormal = normalize( mul3x3( vObjNormal, ( float3x3 )cModel[0] ) );
  69. float3 vWorldTangent = normalize( mul3x3( i.vTangent.xyz, ( float3x3 )cModel[0] ) );
  70. float3 vWorldBinormal = normalize( cross( vWorldNormal, vWorldTangent ) * i.vTangent.w );
  71. #else
  72. float3 vWorldNormal = float3( 0.0f, 0.0f, 1.0f );
  73. float3 vWorldTangent = float3( 1.0f, 0.0f, 0.0f );
  74. float3 vWorldBinormal = float3( 0.0f, 1.0f, 0.0f );
  75. #endif
  76. o.vWorldPosition.xyz = vWorldPosition.xyz;
  77. //o.vWorldNormal.xyz = vWorldNormal.xyz;
  78. o.vWorldTangent.xyz = vWorldTangent.xyz + vWorldPosition.xyz;
  79. o.vWorldBinormal.xyz = vWorldBinormal.xyz + vWorldPosition.xyz;
  80. // Transform into projection space
  81. float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  82. // Map projected position to the refraction texture
  83. float2 vRefractPos;
  84. vRefractPos.x = vProjPosition.x;
  85. vRefractPos.y = -vProjPosition.y; // Invert Y
  86. vRefractPos = ( vRefractPos + vProjPosition.w ) * 0.5f;
  87. o.vProjPosForRefract.xyz = float3(vRefractPos.x, vRefractPos.y, vProjPosition.w);
  88. #ifdef _PS3
  89. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  90. vProjPosition.y = -vProjPosition.y;
  91. vProjPosition.z = 2.0f * vProjPosition.z - vProjPosition.w;
  92. #endif // _PS3
  93. o.vProjPosition.xyzw = vProjPosition.xyzw;
  94. o.vWorldPosition.w = vProjPosition.z;
  95. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  96. o.fog = CalcFixedFunctionFog( vWorldPosition, FOGTYPE_RANGE );
  97. #endif
  98. // View vector
  99. float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
  100. // Texture coordinates
  101. float2 vBaseUv;
  102. vBaseUv.x = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[0].xy );
  103. vBaseUv.y = dot( i.vTexCoord0.xy, cBaseTexCoordTransform[1].xy );
  104. //o.vUv0.xy = vBaseUv.xy;
  105. o.vUv0.xy = vBaseUv.xy * ( 1.0f + kFlPortalOuterBorder ) - ( kFlPortalOuterBorder * 0.5f ); // Adjust uv's for shrunken portal
  106. // Portal open time
  107. float flPortalOpenAmount = saturate( g_flPortalOpenAmount + 0.001f ); // 0.001f to avoid divide by zero
  108. // Noise UV
  109. float kFlBorderNoiseScale = 0.3f;
  110. float kFlNoiseUvScroll = g_flTime * 0.0275f;
  111. float2 vNoiseUv = ( ( vBaseUv.xy - 0.5f ) / flPortalOpenAmount ) + 0.5f;
  112. o.vNoiseTexCoord.xy = ( vNoiseUv.xy * kFlBorderNoiseScale ) + float2( kFlNoiseUvScroll, 0 );
  113. o.vNoiseTexCoord.zw = ( vNoiseUv.xy * kFlBorderNoiseScale ) - float2( kFlNoiseUvScroll, 0 ); // Will fetch as wz to avoid matching layers
  114. return o;
  115. }