Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 420 core
- #extension GL_ARB_explicit_uniform_location : require
- layout(binding = 0) uniform sampler2D gFinalImage;
- layout(binding = 1) uniform sampler2D gPosition;
- layout(binding = 2) uniform sampler2D gNormal;
- layout(binding = 3) uniform sampler2D blueNoise;
- layout(binding = 4) uniform sampler2D roughnessTex;
- layout(location = 4)uniform mat4 projection;
- layout(location = 5)uniform mat4 view;
- layout(location = 6)uniform mat4 invprojection;
- layout(location = 7)uniform mat4 invView;
- layout(location = 99) uniform float fTime;
- layout(location = 100) uniform vec2 resolution;
- noperspective in vec2 TexCoords;
- out vec4 outColor;
- const float step = 0.1;
- const float minRayStep = 0.1;
- const float maxSteps = 100;
- const int numBinarySearchSteps = 5;
- #define Scale vec3(.8, .8, .8)
- #define K 19.19
- vec3 BinarySearch(vec3 dir, inout vec3 hitCoord, out float dDepth);
- vec4 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth);
- vec3 hash(vec3 a, float i);
- void main()
- {
- vec3 viewPos = (textureLod(gPosition, TexCoords, 0)).xyz;
- if(viewPos.z < -100){
- discard;
- }
- vec3 col = vec3(0.0);
- float roughness = texture(roughnessTex, TexCoords).r;
- int samples = 5;
- if(roughness == 0.0)
- samples = 1;
- for(int i = 0; i < samples; i++){
- vec3 wp = vec3(vec4(viewPos, 1.0) * invView);
- vec3 jitt = vec3(hash(wp, i)) * roughness * 0.1;
- vec3 viewNormal = texture(gNormal, TexCoords).xyz;
- vec3 cr = cross(viewNormal, vec3(1.0,0.0,0.0));
- vec3 cr2 = cross(viewNormal, cr);
- viewNormal = viewNormal + cr * jitt.x + cr2 * jitt.y;
- vec3 reflected = normalize(reflect(normalize(viewPos), normalize(viewNormal)));
- vec3 hitPos = viewPos;
- float dDepth;
- vec4 coords = RayCast(reflected, hitPos, dDepth);
- vec3 SSR = vec3(textureLod(gFinalImage, coords.xy, 0).rgb);//textureLod(gFinalImage, coords.xy, 0).rgb * clamp(ReflectionMultiplier, 0.0, 0.9) * Fresnel;
- if(coords == vec4(0.0))
- SSR = vec3(1.0);
- col += SSR;
- }
- outColor = vec4((col / samples), 1.0);
- }
- vec3 BinarySearch(vec3 dir, inout vec3 hitCoord, out float dDepth)
- {
- float depth;
- for(int i = 0; i < numBinarySearchSteps; i++)
- {
- vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
- projectedCoord.xy /= projectedCoord.w;
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
- depth = texture2D(gPosition, projectedCoord.xy).z;
- dDepth = hitCoord.z - depth;
- if(dDepth > 0.0)
- hitCoord += dir;
- dir *= 0.5;
- hitCoord -= dir;
- }
- vec4 projectedCoord = projection * vec4(hitCoord, 1.0);
- projectedCoord.xy /= projectedCoord.w;
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
- return vec3(projectedCoord.xy, depth);
- }
- vec4 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
- {
- vec3 stepDir = dir * step;
- float depth;
- vec4 projectedCoord;
- for(int i = 0; i < maxSteps; i++)
- {
- stepDir = dir * step * -hitCoord.z;
- hitCoord += stepDir;
- projectedCoord = projection * vec4(hitCoord, 1.0);
- projectedCoord.xy /= projectedCoord.w;
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
- depth = textureLod(gPosition, projectedCoord.xy, 0).z;
- if(depth > 1000.0)
- continue;
- dDepth = hitCoord.z - depth;
- if((dir.z - dDepth) < 0.2)
- {
- if(dDepth <= 0.0)
- {
- vec4 Result;
- Result = vec4(BinarySearch(stepDir, hitCoord, dDepth), 1.0);
- return Result;
- }
- }
- }
- //discard;
- return vec4(0.0);
- //return vec4(projectedCoord.xy, depth, 0.0);
- }
- vec3 hash(vec3 a, float i)
- {
- //fTime * 600 +
- float scale = 0.01;
- float x = texture(blueNoise, gl_FragCoord.st*resolution*2 +(i*123123 * scale)).r;
- float y = texture(blueNoise, gl_FragCoord.st*resolution*2 + (i*123123+3.1412)*scale).r;
- float z = texture(blueNoise, gl_FragCoord.st*resolution*2 + (i*123123+1.4142)*scale+i).r;
- return vec3(x-0.5,y-0.5,z-0.5) * 2.0;
- /*
- a = fract(a * Scale);
- a += dot(a, a.yxz + K);
- return fract((a.xxy + a.yxx)*a.zyx);*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement