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.

159 lines
6.7 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // Includes =======================================================================================
  3. #include "common_vertexlitgeneric_dx9.h"
  4. // Texture Samplers ===============================================================================
  5. sampler g_tRefractionSampler : register( s0 );
  6. sampler g_tBumpSampler : register( s1 );
  7. // Shaders Constants and Globals ==================================================================
  8. const float4 g_mViewProj0 : register( c0 ); // 1st row of matrix
  9. const float4 g_mViewProj1 : register( c1 ); // 2nd row of matrix
  10. const float4 g_vCameraPosition : register( c5 );
  11. const float4 g_vPackedConst6 : register( c6 );
  12. #define g_flBlurAmount g_vPackedConst6.x // 0.01f;
  13. #define g_flRefractAmount g_vPackedConst6.y // Default = 1.0f
  14. #define g_flTime g_vPackedConst6.w
  15. const float4 g_cColorTint : register( c7 );
  16. const float4 g_vPackedConst8 : register( c8 );
  17. #define g_cSilhouetteColor g_vPackedConst8 //= { 0.3, 0.3, 0.5 };
  18. #define g_flSilhouetteThickness g_vPackedConst8.w //= 0.2f;
  19. const float2 g_vGroundMinMax : register( c9 ); //= { -0.3, -0.1 };
  20. // 8 2D Poisson offsets (designed to use .xy and .wz swizzles (not .zw)
  21. static const float4 g_vPoissonOffset[4] = { float4 (-0.0876f, 0.9703f, 0.5651f, 0.4802f ),
  22. float4 ( 0.1851f, 0.1580f, -0.0617f, -0.2616f ),
  23. float4 (-0.5477f, -0.6603f, 0.0711f, -0.5325f ),
  24. float4 (-0.0751f, -0.8954f, 0.4054f, 0.6384f ) };
  25. // Interpolated values ============================================================================
  26. struct PS_INPUT
  27. {
  28. float3 vWorldNormal : TEXCOORD0; // World-space normal
  29. float3 vWorldTangent : TEXCOORD1;
  30. float3 vWorldBinormal : TEXCOORD2;
  31. float3 vProjPosForRefract : TEXCOORD3;
  32. float3 vWorldViewVector : TEXCOORD4;
  33. float4 vUv0 : TEXCOORD5; // uv.xy, uvScroll.xy
  34. float4 vUv1 : TEXCOORD6; // uv.xy, uvScroll.xy
  35. float2 vUvGroundNoise : TEXCOORD7;
  36. };
  37. // Main ===========================================================================================
  38. float4 main( PS_INPUT i ) : COLOR
  39. {
  40. /*
  41. // Bump layer 0
  42. float2 vUv0 = i.vUv0Uv1.xy;
  43. float2 vUv0Scroll = i.vUv0Uv1.xy * 3.0f;
  44. vUv0Scroll.y -= g_flTime * 0.1f;
  45. float4 vBumpTexel0 = tex2D( g_tBumpSampler, vUv0Scroll.xy );
  46. vBumpTexel0 = tex2D( g_tBumpSampler, vUv0.xy + (vBumpTexel0.xy*0.03) );
  47. // Bump layer 1
  48. float2 vUv1 = i.vUv0Uv1.xy * 10.0f;
  49. float2 vUv1Scroll = i.vUv0Uv1.xy * 32.0f;
  50. vUv1Scroll.y -= g_flTime * 0.1f;
  51. float4 vBumpTexel1 = tex2D( g_tBumpSampler, vUv1Scroll.xy );
  52. vBumpTexel1 = tex2D( g_tBumpSampler, vUv1.xy + (vBumpTexel1.xy*0.03) );
  53. //*/
  54. // Bump layer 0
  55. float4 vBumpTexel0 = tex2D( g_tBumpSampler, i.vUv0.wz );
  56. vBumpTexel0 = tex2D( g_tBumpSampler, i.vUv0.xy + ( vBumpTexel0.xy*0.03 ) );
  57. // Bump layer 1
  58. float4 vBumpTexel1 = tex2D( g_tBumpSampler, i.vUv1.wz );
  59. vBumpTexel1 = tex2D( g_tBumpSampler, i.vUv1.xy + ( vBumpTexel1.xy*0.03 ) );
  60. // Combine bump layers into tangetn normal
  61. float3 vTangentNormal = ( vBumpTexel0 * 2.0f ) - 1.0f;
  62. vTangentNormal.xyz += ( vBumpTexel1 * 2.0f - 1.0f ) * 0.5f; // * 0.5f;
  63. // Transform into world space
  64. float3 vWorldNormal = Vec3TangentToWorld( vTangentNormal.xyz, i.vWorldNormal, i.vWorldTangent, i.vWorldBinormal );
  65. // Effect mask
  66. //float flEffectMask = saturate( dot( -i.vWorldViewVector.xyz, i.vWorldNormal.xyz ) * lerp( 2.0f, 1.0f, g_flSilhouetteThickness ) );
  67. float flEffectMask = saturate( dot( -i.vWorldViewVector.xyz, i.vWorldNormal.xyz ) * ( (2.0f - g_flSilhouetteThickness) ) );
  68. // Simulate ground intersection
  69. flEffectMask *= smoothstep( g_vGroundMinMax.x, g_vGroundMinMax.y, i.vWorldNormal.z );
  70. // Soften mask by squaring term
  71. flEffectMask *= flEffectMask;
  72. // Silhouette mask
  73. float flSilhouetteHighlightMask = saturate( flEffectMask * ( 1.0f - flEffectMask ) * 4.0f );
  74. flSilhouetteHighlightMask *= flSilhouetteHighlightMask * flSilhouetteHighlightMask;
  75. // Transform world space normal into clip space and project
  76. float3 vProjNormal;
  77. vProjNormal.x = dot( vWorldNormal.xyz, g_mViewProj0.xyz ); // 1st row
  78. vProjNormal.y = dot( vWorldNormal.xyz, g_mViewProj1.xyz ); // 2nd row
  79. // Compute coordinates for sampling refraction
  80. float2 vRefractTexCoordNoWarp = i.vProjPosForRefract.xy / i.vProjPosForRefract.z;
  81. float2 vRefractTexCoord = vProjNormal.xy;
  82. float scale = lerp( 0.0f, g_flRefractAmount, flEffectMask );// * flEffectMask * flEffectMask ); // Using flEffectMask^3
  83. vRefractTexCoord.xy *= scale;
  84. vRefractTexCoord.xy += vRefractTexCoordNoWarp.xy;
  85. // Blur by scalable Poisson filter
  86. float flBlurAmount = g_flBlurAmount * flEffectMask;
  87. float3 cRefract = tex2D( g_tRefractionSampler, vRefractTexCoord.xy );
  88. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[0].xy * flBlurAmount ) );
  89. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[0].wz * flBlurAmount ) );
  90. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[1].xy * flBlurAmount ) );
  91. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[1].wz * flBlurAmount ) );
  92. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[2].xy * flBlurAmount ) );
  93. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[2].wz * flBlurAmount ) );
  94. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[3].xy * flBlurAmount ) );
  95. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[3].wz * flBlurAmount ) );
  96. cRefract /= 9.0f;
  97. // Undo tone mapping
  98. cRefract /= LINEAR_LIGHT_SCALE;
  99. // Refract color tint
  100. float fColorTintStrength = 1.0f - flEffectMask;
  101. float3 cRefractColorTint = lerp( g_cColorTint, 1.0f, fColorTintStrength );
  102. // Ground noise
  103. //float flGroundNoise = tex2D( g_tBumpSampler, i.vUvGroundNoise.xy ).g;
  104. //flGroundNoise *= smoothstep( g_vGroundMinMax.y, g_vGroundMinMax.y+0.4, -i.vWorldNormal.z );
  105. //flGroundNoise = smoothstep( 0.2, 0.9, flGroundNoise );
  106. //===============//
  107. // Combine terms //
  108. //===============//
  109. float4 result;
  110. result.rgb = cRefract.rgb * cRefractColorTint.rgb;
  111. result.rgb += result.rgb * ( flSilhouetteHighlightMask * g_cSilhouetteColor.rgb );
  112. //result.rgb += flGroundNoise;
  113. //result.rgb = float3( 0.0, 0.0, 0.0 );
  114. //result.rg = vRefractTexCoord.xy;
  115. //result.rg = i.vUv0Uv1.xy;
  116. //result.rgb = vBumpTexel0.rgb;
  117. //result.rgb = vTangentNormal;
  118. //result.rgb = vWorldNormal;
  119. //result = flEffectMask;
  120. //result = flSilhouetteHighlightMask;
  121. //result = tex2D( g_tBumpSampler, i.vUvGroundNoise.xy ).y;
  122. //result.rgb = flGroundNoise;
  123. // Set alpha to...
  124. result.a = flEffectMask;
  125. return FinalOutput( result, 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_LINEAR ); //go back to final output when it'll fit.
  126. }