Guest User

Untitled

a guest
Aug 31st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. uniform sampler2D gColor;
  2. uniform sampler2D gPosition;
  3. uniform sampler2D gNormal;
  4. uniform sampler2D gEffect;
  5. uniform vec2 gTexSizeInv;
  6.  
  7.  
  8. // Consts should help improve performance
  9. const float rayStep = 0.25;
  10. const float minRayStep = 0.1;
  11. const float maxSteps = 20;
  12. const float searchDist = 5;
  13. const float searchDistInv = 0.2;
  14. const int numBinarySearchSteps = 5;
  15. const float maxDDepth = 1.0;
  16. const float maxDDepthInv = 1.0;
  17.  
  18.  
  19. const float reflectionSpecularFalloffExponent = 3.0;
  20.  
  21.  
  22. uniform mat4 projection;
  23.  
  24.  
  25. vec3 BinarySearch(vec3 dir, inout vec3 hitCoord, out float dDepth)
  26. {
  27. float depth;
  28.  
  29.  
  30. for(int i = 0; i < numBinarySearchSteps; i++)
  31. {
  32. vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
  33. projectedCoord.xy /= projectedCoord.w;
  34. projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  35.  
  36.  
  37. depth = texture2D(gPosition, projectedCoord.xy).z;
  38.  
  39.  
  40. dDepth = hitCoord.z - depth;
  41.  
  42.  
  43. if(dDepth > 0.0)
  44. hitCoord += dir;
  45.  
  46.  
  47. dir *= 0.5;
  48. hitCoord -= dir;
  49. }
  50.  
  51.  
  52. vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
  53. projectedCoord.xy /= projectedCoord.w;
  54. projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  55.  
  56.  
  57. return vec3(projectedCoord.xy, depth);
  58. }
  59.  
  60.  
  61. vec4 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
  62. {
  63. dir *= rayStep;
  64.  
  65.  
  66. float depth;
  67.  
  68.  
  69. for(int i = 0; i < maxSteps; i++)
  70. {
  71. hitCoord += dir;
  72.  
  73.  
  74. vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
  75. projectedCoord.xy /= projectedCoord.w;
  76. projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  77.  
  78.  
  79. depth = texture2D(gPosition, projectedCoord.xy).z;
  80.  
  81.  
  82. dDepth = hitCoord.z - depth;
  83.  
  84.  
  85. if(dDepth < 0.0)
  86. return vec4(BinarySearch(dir, hitCoord, dDepth), 1.0);
  87. }
  88.  
  89.  
  90. return vec4(0.0, 0.0, 0.0, 0.0);
  91. }
  92.  
  93.  
  94. void main()
  95. {
  96. vec2 gTexCoord = gl_FragCoord.xy * gTexSizeInv;
  97.  
  98.  
  99. // Samples
  100. float specular = texture2D(gColor, gTexCoord).a;
  101.  
  102.  
  103. if(specular == 0.0)
  104. {
  105. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  106. return;
  107. }
  108.  
  109.  
  110. vec3 viewNormal = texture2D(gNormal, gTexCoord).xyz;
  111. vec3 viewPos = texture2D(gPosition, gTexCoord).xyz;
  112.  
  113.  
  114. // Reflection vector
  115. vec3 reflected = normalize(reflect(normalize(viewPos), normalize(viewNormal)));
  116.  
  117.  
  118. // Ray cast
  119. vec3 hitPos = viewPos;
  120. float dDepth;
  121.  
  122.  
  123. vec4 coords = RayCast(reflected * max(minRayStep, -viewPos.z), hitPos, dDepth);
  124.  
  125.  
  126. vec2 dCoords = abs(vec2(0.5, 0.5) - coords.xy);
  127.  
  128.  
  129. float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0);
  130.  
  131.  
  132. // Get color
  133. gl_FragColor = vec4(texture2D(gEffect, coords.xy).rgb,
  134. pow(specular, reflectionSpecularFalloffExponent) *
  135. screenEdgefactor * clamp(-reflected.z, 0.0, 1.0) *
  136. clamp((searchDist - length(viewPos - hitPos)) * searchDistInv, 0.0, 1.0) * coords.w);
  137. }
Add Comment
Please, Sign In to add comment