Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 420 core
  2. #extension GL_ARB_explicit_uniform_location : require
  3.  
  4. layout(binding = 0) uniform sampler2D gFinalImage;
  5. layout(binding = 1) uniform sampler2D gPosition;
  6. layout(binding = 2) uniform sampler2D gNormal;
  7. layout(binding = 3) uniform sampler2D blueNoise;
  8. layout(binding = 4) uniform sampler2D roughnessTex;
  9.  
  10. layout(location = 4)uniform mat4 projection;
  11. layout(location = 5)uniform mat4 view;
  12. layout(location = 6)uniform mat4 invprojection;
  13. layout(location = 7)uniform mat4 invView;
  14. layout(location = 99) uniform float fTime;
  15. layout(location = 100) uniform vec2 resolution;
  16.  
  17. noperspective in vec2 TexCoords;
  18.  
  19. out vec4 outColor;
  20.  
  21. const float step = 0.1;
  22. const float minRayStep = 0.1;
  23. const float maxSteps = 100;
  24. const int numBinarySearchSteps = 5;
  25.  
  26.  
  27.  
  28. #define Scale vec3(.8, .8, .8)
  29. #define K 19.19
  30.  
  31. vec3 BinarySearch(vec3 dir, inout vec3 hitCoord, out float dDepth);
  32.  
  33. vec4 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth);
  34.  
  35. vec3 hash(vec3 a, float i);
  36.  
  37. void main()
  38. {
  39.     vec3 viewPos = (textureLod(gPosition, TexCoords, 0)).xyz;
  40.  
  41.     if(viewPos.z < -100){
  42.         discard;
  43.     }
  44.  
  45.     vec3 col = vec3(0.0);
  46.     float roughness = texture(roughnessTex, TexCoords).r;
  47.     int samples = 5;
  48.     if(roughness == 0.0)
  49.         samples = 1;
  50.  
  51.     for(int i = 0; i < samples; i++){
  52.         vec3 wp = vec3(vec4(viewPos, 1.0) * invView);
  53.  
  54.         vec3 jitt = vec3(hash(wp, i)) * roughness * 0.1;
  55.  
  56.         vec3 viewNormal = texture(gNormal, TexCoords).xyz;
  57.  
  58.         vec3 cr = cross(viewNormal, vec3(1.0,0.0,0.0));
  59.         vec3 cr2 = cross(viewNormal, cr);
  60.         viewNormal = viewNormal + cr * jitt.x + cr2 * jitt.y;
  61.  
  62.         vec3 reflected = normalize(reflect(normalize(viewPos), normalize(viewNormal)));
  63.         vec3 hitPos = viewPos;
  64.         float dDepth;
  65.         vec4 coords = RayCast(reflected, hitPos, dDepth);
  66.  
  67.  
  68.  
  69.         vec3 SSR = vec3(textureLod(gFinalImage, coords.xy, 0).rgb);//textureLod(gFinalImage, coords.xy, 0).rgb * clamp(ReflectionMultiplier, 0.0, 0.9) * Fresnel;
  70.         if(coords == vec4(0.0))
  71.             SSR = vec3(1.0);
  72.         col += SSR;
  73.     }
  74.  
  75.     outColor = vec4((col / samples), 1.0);
  76.  
  77. }
  78.  
  79.  
  80. vec3 BinarySearch(vec3 dir, inout vec3 hitCoord, out float dDepth)
  81. {
  82.     float depth;
  83.  
  84.  
  85.     for(int i = 0; i < numBinarySearchSteps; i++)
  86.     {
  87.         vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
  88.         projectedCoord.xy /= projectedCoord.w;
  89.         projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  90.  
  91.  
  92.         depth = texture2D(gPosition, projectedCoord.xy).z;
  93.  
  94.  
  95.         dDepth = hitCoord.z - depth;
  96.  
  97.  
  98.         if(dDepth > 0.0)
  99.             hitCoord += dir;
  100.  
  101.  
  102.         dir *= 0.5;
  103.         hitCoord -= dir;
  104.     }
  105.  
  106.  
  107.     vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
  108.     projectedCoord.xy /= projectedCoord.w;
  109.     projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  110.  
  111.  
  112.     return vec3(projectedCoord.xy, depth);
  113. }
  114.  
  115.  
  116. vec4 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
  117. {
  118.     vec3 stepDir = dir * step;
  119.  
  120.  
  121.     float depth;
  122.     vec4 projectedCoord;
  123.  
  124.  
  125.     for(int i = 0; i < maxSteps; i++)
  126.     {
  127.         stepDir = dir * step * -hitCoord.z;
  128.  
  129.         hitCoord += stepDir;
  130.  
  131.         projectedCoord = projection * vec4(hitCoord, 1.0);
  132.         projectedCoord.xy /= projectedCoord.w;
  133.         projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
  134.  
  135.         depth = textureLod(gPosition, projectedCoord.xy, 0).z;
  136.         if(depth > 1000.0)
  137.             continue;
  138.  
  139.         dDepth = hitCoord.z - depth;
  140.  
  141.         if((dir.z - dDepth) < 0.2)
  142.         {
  143.             if(dDepth <= 0.0)
  144.             {
  145.                 vec4 Result;
  146.                 Result = vec4(BinarySearch(stepDir, hitCoord, dDepth), 1.0);
  147.                 return Result;
  148.             }
  149.         }
  150.     }
  151.  
  152.     //discard;
  153.     return vec4(0.0);
  154.     //return vec4(projectedCoord.xy, depth, 0.0);
  155. }
  156.  
  157.  
  158. vec3 hash(vec3 a, float i)
  159. {
  160.     //fTime * 600 +
  161.     float scale = 0.01;
  162.     float x = texture(blueNoise, gl_FragCoord.st*resolution*2 +(i*123123 * scale)).r;
  163.     float y = texture(blueNoise, gl_FragCoord.st*resolution*2 + (i*123123+3.1412)*scale).r;
  164.     float z = texture(blueNoise, gl_FragCoord.st*resolution*2 + (i*123123+1.4142)*scale+i).r;
  165.  
  166.  
  167.  
  168.     return vec3(x-0.5,y-0.5,z-0.5) * 2.0;
  169.     /*
  170.     a = fract(a * Scale);
  171.     a += dot(a, a.yxz + K);
  172.     return fract((a.xxy + a.yxx)*a.zyx);*/
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement