2018-08-27 05:31:48 +00:00
|
|
|
/* clang-format off */
|
2016-11-29 22:55:12 +00:00
|
|
|
[vertex]
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
layout(location = 0) in highp vec4 vertex_attrib;
|
2018-08-27 05:31:48 +00:00
|
|
|
/* clang-format on */
|
2018-08-24 11:42:18 +00:00
|
|
|
layout(location = 4) in vec2 uv_in;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
out vec2 uv_interp;
|
|
|
|
out vec2 pos_interp;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
uv_interp = uv_in;
|
|
|
|
gl_Position = vertex_attrib;
|
2018-08-24 11:42:18 +00:00
|
|
|
pos_interp.xy = gl_Position.xy;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 05:31:48 +00:00
|
|
|
/* clang-format off */
|
2016-11-29 22:55:12 +00:00
|
|
|
[fragment]
|
|
|
|
|
|
|
|
in vec2 uv_interp;
|
2018-08-27 05:31:48 +00:00
|
|
|
/* clang-format on */
|
2016-11-29 22:55:12 +00:00
|
|
|
in vec2 pos_interp;
|
|
|
|
|
|
|
|
uniform sampler2D source_diffuse; //texunit:0
|
|
|
|
uniform sampler2D source_normal_roughness; //texunit:1
|
|
|
|
uniform sampler2D source_depth; //texunit:2
|
|
|
|
|
|
|
|
uniform float camera_z_near;
|
|
|
|
uniform float camera_z_far;
|
|
|
|
|
|
|
|
uniform vec2 viewport_size;
|
|
|
|
uniform vec2 pixel_size;
|
|
|
|
|
|
|
|
uniform float filter_mipmap_levels;
|
|
|
|
|
|
|
|
uniform mat4 inverse_projection;
|
|
|
|
uniform mat4 projection;
|
|
|
|
|
|
|
|
uniform int num_steps;
|
|
|
|
uniform float depth_tolerance;
|
|
|
|
uniform float distance_fade;
|
2017-06-16 00:28:05 +00:00
|
|
|
uniform float curve_fade_in;
|
|
|
|
|
2016-11-29 22:55:12 +00:00
|
|
|
layout(location = 0) out vec4 frag_color;
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 view_to_screen(vec3 view_pos, out float w) {
|
|
|
|
vec4 projected = projection * vec4(view_pos, 1.0);
|
|
|
|
projected.xyz /= projected.w;
|
|
|
|
projected.xy = projected.xy * 0.5 + 0.5;
|
|
|
|
w = projected.w;
|
|
|
|
return projected.xy;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define M_PI 3.14159265359
|
|
|
|
|
|
|
|
void main() {
|
2018-08-24 11:42:18 +00:00
|
|
|
vec4 diffuse = texture(source_diffuse, uv_interp);
|
|
|
|
vec4 normal_roughness = texture(source_normal_roughness, uv_interp);
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
vec3 normal;
|
2018-08-24 11:42:18 +00:00
|
|
|
normal = normal_roughness.xyz * 2.0 - 1.0;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
float roughness = normal_roughness.w;
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
float depth_tex = texture(source_depth, uv_interp).r;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
vec4 world_pos = inverse_projection * vec4(uv_interp * 2.0 - 1.0, depth_tex * 2.0 - 1.0, 1.0);
|
|
|
|
vec3 vertex = world_pos.xyz / world_pos.w;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
vec3 view_dir = normalize(vertex);
|
|
|
|
vec3 ray_dir = normalize(reflect(view_dir, normal));
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
if (dot(ray_dir, normal) < 0.001) {
|
|
|
|
frag_color = vec4(0.0);
|
2016-11-29 22:55:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//ray_dir = normalize(view_dir - normal * dot(normal,view_dir) * 2.0);
|
2019-02-24 22:55:37 +00:00
|
|
|
//ray_dir = normalize(vec3(1.0, 1.0, -1.0));
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
////////////////
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// make ray length and clip it against the near plane (don't want to trace beyond visible)
|
2016-11-29 22:55:12 +00:00
|
|
|
float ray_len = (vertex.z + ray_dir.z * camera_z_far) > -camera_z_near ? (-camera_z_near - vertex.z) / ray_dir.z : camera_z_far;
|
2018-08-24 11:42:18 +00:00
|
|
|
vec3 ray_end = vertex + ray_dir * ray_len;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
float w_begin;
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 vp_line_begin = view_to_screen(vertex, w_begin);
|
2016-11-29 22:55:12 +00:00
|
|
|
float w_end;
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 vp_line_end = view_to_screen(ray_end, w_end);
|
|
|
|
vec2 vp_line_dir = vp_line_end - vp_line_begin;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// we need to interpolate w along the ray, to generate perspective correct reflections
|
|
|
|
w_begin = 1.0 / w_begin;
|
|
|
|
w_end = 1.0 / w_end;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
float z_begin = vertex.z * w_begin;
|
|
|
|
float z_end = ray_end.z * w_end;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 line_begin = vp_line_begin / pixel_size;
|
|
|
|
vec2 line_dir = vp_line_dir / pixel_size;
|
2016-11-29 22:55:12 +00:00
|
|
|
float z_dir = z_end - z_begin;
|
|
|
|
float w_dir = w_end - w_begin;
|
|
|
|
|
|
|
|
// clip the line to the viewport edges
|
|
|
|
|
2017-04-02 14:16:25 +00:00
|
|
|
float scale_max_x = min(1.0, 0.99 * (1.0 - vp_line_begin.x) / max(1e-5, vp_line_dir.x));
|
|
|
|
float scale_max_y = min(1.0, 0.99 * (1.0 - vp_line_begin.y) / max(1e-5, vp_line_dir.y));
|
|
|
|
float scale_min_x = min(1.0, 0.99 * vp_line_begin.x / max(1e-5, -vp_line_dir.x));
|
|
|
|
float scale_min_y = min(1.0, 0.99 * vp_line_begin.y / max(1e-5, -vp_line_dir.y));
|
2016-11-29 22:55:12 +00:00
|
|
|
float line_clip = min(scale_max_x, scale_max_y) * min(scale_min_x, scale_min_y);
|
|
|
|
line_dir *= line_clip;
|
|
|
|
z_dir *= line_clip;
|
2018-08-24 11:42:18 +00:00
|
|
|
w_dir *= line_clip;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// clip z and w advance to line advance
|
|
|
|
vec2 line_advance = normalize(line_dir); // down to pixel
|
|
|
|
float step_size = length(line_advance) / length(line_dir);
|
|
|
|
float z_advance = z_dir * step_size; // adapt z advance to line advance
|
|
|
|
float w_advance = w_dir * step_size; // adapt w advance to line advance
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// make line advance faster if direction is closer to pixel edges (this avoids sampling the same pixel twice)
|
|
|
|
float advance_angle_adj = 1.0 / max(abs(line_advance.x), abs(line_advance.y));
|
|
|
|
line_advance *= advance_angle_adj; // adapt z advance to line advance
|
|
|
|
z_advance *= advance_angle_adj;
|
|
|
|
w_advance *= advance_angle_adj;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
vec2 pos = line_begin;
|
|
|
|
float z = z_begin;
|
|
|
|
float w = w_begin;
|
2018-08-24 11:42:18 +00:00
|
|
|
float z_from = z / w;
|
|
|
|
float z_to = z_from;
|
2016-11-29 22:55:12 +00:00
|
|
|
float depth;
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 prev_pos = pos;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
bool found = false;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
float steps_taken = 0.0;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
for (int i = 0; i < num_steps; i++) {
|
|
|
|
pos += line_advance;
|
|
|
|
z += z_advance;
|
|
|
|
w += w_advance;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// convert to linear depth
|
2017-09-07 23:16:33 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
depth = texture(source_depth, pos * pixel_size).r * 2.0 - 1.0;
|
2017-09-07 23:16:33 +00:00
|
|
|
#ifdef USE_ORTHOGONAL_PROJECTION
|
2018-08-24 11:42:18 +00:00
|
|
|
depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0;
|
2017-09-07 23:16:33 +00:00
|
|
|
#else
|
2016-11-29 22:55:12 +00:00
|
|
|
depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near));
|
2017-09-07 23:16:33 +00:00
|
|
|
#endif
|
2018-08-24 11:42:18 +00:00
|
|
|
depth = -depth;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
z_from = z_to;
|
2018-08-24 11:42:18 +00:00
|
|
|
z_to = z / w;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
if (depth > z_to) {
|
|
|
|
// if depth was surpassed
|
2020-05-22 08:06:23 +00:00
|
|
|
if ((depth <= max(z_to, z_from) + depth_tolerance) && (-depth < camera_z_far)) {
|
|
|
|
// check the depth tolerance and far clip
|
2018-08-24 11:42:18 +00:00
|
|
|
found = true;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
steps_taken += 1.0;
|
|
|
|
prev_pos = pos;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2018-08-24 11:42:18 +00:00
|
|
|
float margin_blend = 1.0;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
vec2 margin = vec2((viewport_size.x + viewport_size.y) * 0.5 * 0.05); // make a uniform margin
|
2020-09-08 22:29:36 +00:00
|
|
|
if (any(bvec4(lessThan(pos, vec2(0.0, 0.0)), greaterThan(pos, viewport_size * 0.5)))) {
|
|
|
|
// clip at the screen edges
|
2018-08-24 11:42:18 +00:00
|
|
|
frag_color = vec4(0.0);
|
2016-11-29 22:55:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-09-08 22:29:36 +00:00
|
|
|
//blend fading out towards inner margin
|
|
|
|
// 0.25 = midpoint of half-resolution reflection
|
|
|
|
vec2 margin_grad = mix(viewport_size * 0.5 - pos, pos, lessThan(pos, viewport_size * 0.25));
|
|
|
|
margin_blend = smoothstep(0.0, margin.x * margin.y, margin_grad.x * margin_grad.y);
|
2018-08-24 11:42:18 +00:00
|
|
|
//margin_blend = 1.0;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec2 final_pos;
|
2022-01-16 17:00:13 +00:00
|
|
|
float grad = (steps_taken + 1.0) / float(num_steps);
|
2018-08-24 11:42:18 +00:00
|
|
|
float initial_fade = curve_fade_in == 0.0 ? 1.0 : pow(clamp(grad, 0.0, 1.0), curve_fade_in);
|
|
|
|
float fade = pow(clamp(1.0 - grad, 0.0, 1.0), distance_fade) * initial_fade;
|
|
|
|
final_pos = pos;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
#ifdef REFLECT_ROUGHNESS
|
|
|
|
|
|
|
|
vec4 final_color;
|
2018-08-24 11:42:18 +00:00
|
|
|
// if roughness is enabled, do screen space cone tracing
|
2016-11-29 22:55:12 +00:00
|
|
|
if (roughness > 0.001) {
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
2018-08-24 11:42:18 +00:00
|
|
|
// use a blurred version (in consecutive mipmaps) of the screen to simulate roughness
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
float gloss = 1.0 - roughness;
|
2016-11-29 22:55:12 +00:00
|
|
|
float cone_angle = roughness * M_PI * 0.5;
|
|
|
|
vec2 cone_dir = final_pos - line_begin;
|
|
|
|
float cone_len = length(cone_dir);
|
2018-08-24 11:42:18 +00:00
|
|
|
cone_dir = normalize(cone_dir); // will be used normalized from now on
|
2017-04-02 14:16:25 +00:00
|
|
|
float max_mipmap = filter_mipmap_levels - 1.0;
|
2018-08-24 11:42:18 +00:00
|
|
|
float gloss_mult = gloss;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
float rem_alpha = 1.0;
|
2016-11-29 22:55:12 +00:00
|
|
|
final_color = vec4(0.0);
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
float op_len = 2.0 * tan(cone_angle) * cone_len; // opposite side of iso triangle
|
2016-11-29 22:55:12 +00:00
|
|
|
float radius;
|
|
|
|
{
|
2018-08-24 11:42:18 +00:00
|
|
|
// fit to sphere inside cone (sphere ends at end of cone), something like this:
|
2016-11-29 22:55:12 +00:00
|
|
|
// ___
|
|
|
|
// \O/
|
|
|
|
// V
|
|
|
|
//
|
|
|
|
// as it avoids bleeding from beyond the reflection as much as possible. As a plus
|
|
|
|
// it also makes the rough reflection more elongated.
|
|
|
|
float a = op_len;
|
|
|
|
float h = cone_len;
|
|
|
|
float a2 = a * a;
|
|
|
|
float fh2 = 4.0f * h * h;
|
|
|
|
radius = (a * (sqrt(a2 + fh2) - a)) / (4.0f * h);
|
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// find the place where screen must be sampled
|
|
|
|
vec2 sample_pos = (line_begin + cone_dir * (cone_len - radius)) * pixel_size;
|
|
|
|
// radius is in pixels, so it's natural that log2(radius) maps to the right mipmap for the amount of pixels
|
|
|
|
float mipmap = clamp(log2(radius), 0.0, max_mipmap);
|
|
|
|
//mipmap = max(mipmap - 1.0, 0.0);
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// do sampling
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
vec4 sample_color;
|
|
|
|
{
|
2018-08-24 11:42:18 +00:00
|
|
|
sample_color = textureLod(source_diffuse, sample_pos, mipmap);
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
// multiply by gloss
|
|
|
|
sample_color.rgb *= gloss_mult;
|
|
|
|
sample_color.a = gloss_mult;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
rem_alpha -= sample_color.a;
|
2018-08-24 11:42:18 +00:00
|
|
|
if (rem_alpha < 0.0) {
|
2016-11-29 22:55:12 +00:00
|
|
|
sample_color.rgb *= (1.0 - abs(rem_alpha));
|
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
final_color += sample_color;
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
if (final_color.a >= 0.95) {
|
2016-11-29 22:55:12 +00:00
|
|
|
// This code of accumulating gloss and aborting on near one
|
|
|
|
// makes sense when you think of cone tracing.
|
|
|
|
// Think of it as if roughness was 0, then we could abort on the first
|
|
|
|
// iteration. For lesser roughness values, we need more iterations, but
|
|
|
|
// each needs to have less influence given the sphere is smaller
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
cone_len -= radius * 2.0; // go to next (smaller) circle.
|
2016-11-29 22:55:12 +00:00
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
gloss_mult *= gloss;
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-24 11:42:18 +00:00
|
|
|
final_color = textureLod(source_diffuse, final_pos * pixel_size, 0.0);
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 11:42:18 +00:00
|
|
|
frag_color = vec4(final_color.rgb, fade * margin_blend);
|
2016-11-29 22:55:12 +00:00
|
|
|
|
|
|
|
#else
|
2018-08-24 11:42:18 +00:00
|
|
|
frag_color = vec4(textureLod(source_diffuse, final_pos * pixel_size, 0.0).rgb, fade * margin_blend);
|
2016-11-29 22:55:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} else {
|
2018-08-24 11:42:18 +00:00
|
|
|
frag_color = vec4(0.0, 0.0, 0.0, 0.0);
|
2016-11-29 22:55:12 +00:00
|
|
|
}
|
|
|
|
}
|