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.

117 lines
3.6 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. #include "common_ps_fxc.h"
  3. sampler DepthSampler : register( s0 );
  4. sampler Tex1Sampler : register( s1 );
  5. const float4 g_Const0 : register( c0 );
  6. const float4 g_Const1 : register( c1 );
  7. const float4x4 g_viewProjMatrix : register( c11 );
  8. const float4x4 g_invViewMatrix : register( c15 );
  9. #define g_radius g_Const0.x
  10. #define g_offsetX g_Const0.y
  11. #define g_offsetY g_Const0.z
  12. struct PS_INPUT
  13. {
  14. float2 vBaseUV : TEXCOORD0;
  15. };
  16. float3 screenDepthToWorld( float2 vUV, float depth )
  17. {
  18. float x = vUV.x * 2 - 1;
  19. float y = (1 - vUV.y) * 2 - 1;
  20. float4 vProjectedPos = float4(x, y, depth, 1);
  21. float4 vPositionVS = mul(vProjectedPos, g_invViewMatrix);
  22. return (vPositionVS.xyz / vPositionVS.w);
  23. }
  24. float3 sampleDepthToWorld( float2 vUV )
  25. {
  26. float flDepth = tex2D( DepthSampler, vUV ).r;
  27. return screenDepthToWorld( vUV, flDepth );
  28. }
  29. float2 worldToScreen( float3 pos )
  30. {
  31. float4 uvPos = mul( float4(pos,1), g_viewProjMatrix );
  32. uvPos.xy /= uvPos.w;
  33. uvPos.x = ( uvPos.x + 1) * 0.5;
  34. uvPos.y = 1 - ( uvPos.y + 1) * 0.5;
  35. return uvPos.xy;
  36. }
  37. float4 main( PS_INPUT i ) : COLOR
  38. {
  39. float3 offset = float3( g_offsetX, g_offsetY, 0 );
  40. float flDepthCenter = tex2D( DepthSampler, i.vBaseUV ).r;
  41. clip( 0.9999f - flDepthCenter );
  42. float3 pos0 = screenDepthToWorld( i.vBaseUV, flDepthCenter );
  43. float3 random = normalize( tex2D(Tex1Sampler, i.vBaseUV * 32.0).rgb ) * 2 - 1;
  44. //float3 pos1 = sampleDepthToWorld( i.vBaseUV + offset.xz );
  45. //float3 pos2 = sampleDepthToWorld( i.vBaseUV + offset.zy );
  46. //float3 tangent = normalize( pos1 - pos0 + random );
  47. //float3 binormal = normalize( pos2 - pos0 );
  48. //float3 normal = cross(tangent, binormal);
  49. //float3x3 tbn = float3x3( tangent, binormal, abs(normal) );
  50. //return float4( pow(float3( normal.xy * 0.5 + 0.5, 1 ), 2.2), 1 );
  51. //// to screenspace
  52. //float3 camU = float3( g_invViewMatrix[1][0], g_invViewMatrix[1][1], g_invViewMatrix[1][2] );
  53. //float3 camV = float3( g_invViewMatrix[0][0], g_invViewMatrix[0][1], g_invViewMatrix[0][2] );
  54. //float dtV = dot( camU, normal );
  55. //float dtU = dot( camV, normal );
  56. //float3 screenNormal = float3( dtU, dtV, 1 );
  57. const float3 sample_sphere[16] =
  58. { // KERNEL IS TEMP - POPULATE WITH BETTER SAMPLES
  59. float3(0.2196607,0.9032637,0.2254677),
  60. float3(0.05916681,0.2201506,-0.1430302),
  61. float3(-0.4152246,0.1320857,0.7036734),
  62. float3(-0.3790807,0.1454145,0.100605),
  63. float3(0.3149606,-0.1294581,0.7044517),
  64. float3(-0.1108412,0.2162839,0.1336278),
  65. float3(0.658012,-0.4395972,-0.2919373),
  66. float3(0.5377914,0.3112189,0.426864),
  67. float3(-0.2752537,0.07625949,-0.1273409),
  68. float3(-0.1915639,-0.4973421,-0.3129629),
  69. float3(-0.2634767,0.5277923,-0.1107446),
  70. float3(0.8242752,0.02434147,0.06049098),
  71. float3(0.06262707,-0.2128643,-0.03671562),
  72. float3(-0.1795662,-0.3543862,0.07924347),
  73. float3(0.06039629,0.24629,0.4501176),
  74. float3(-0.7786345,-0.3814852,-0.2391262),
  75. };
  76. float occlusion = 0;
  77. for ( int j=0; j<16; j++ )
  78. {
  79. float3 worldSampleDir = sample_sphere[j];//mul( sample_sphere[j], tbn );
  80. worldSampleDir = reflect( worldSampleDir, random );
  81. float3 worldSamplePos = pos0 + (worldSampleDir.xyz * g_radius);
  82. float2 screenSamplePos = worldToScreen( worldSamplePos );
  83. float sampleDepth = tex2D( DepthSampler, screenSamplePos ).r;
  84. float difference = ( flDepthCenter - sampleDepth );
  85. occlusion += (step( difference, 0 ) - 0.5 )
  86. * step( 0.0001, abs(difference) );
  87. //* step( abs(difference), 0.002 );
  88. }
  89. occlusion = smoothstep( -8, 8, occlusion );
  90. return float4( pow(occlusion, 2.2).xxx, 1 );
  91. }