kale/shaders/blit.frag

116 lines
2.7 KiB
GLSL

#version 450
layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 out_color;
layout(set = 2, binding = 0) uniform sampler2D screen;
layout(set = 2, binding = 1) uniform sampler2D depth_tex;
layout(std140, set = 3, binding = 0) uniform UniformBlock {
mat4 model;
mat4 view;
mat4 projection;
mat4 inv_view;
mat4 inv_projection;
vec4 time_padded;
vec4 cam_pos;
};
float road(float x) { return cos(x); }
vec3 backup(vec2 uv) {
float time = time_padded.x;
float x_scale = time * 12.0;
float y_scale = uv.x;
uv.x *= x_scale;
uv.y *= 2.0;
uv.y -= 1.0;
uv.y /= 0.75;
uv.y /= y_scale;
float y = road(uv.x);
float dist = length(uv.y - y);
vec3 col = vec3(cos(time / 10.0)) - vec3(exp(-dist), cos(dist), sin(dist));
return (0.005 * (1.2 + abs(cos(time)))) / col;
}
const int MAX_MARCH_STEPS = 64;
const float EPS_DIST = 1e-6;
float sdf(vec3 point) {
float time = time_padded.x;
float size = 8.0 + (3.0* sin(time));
vec3 s1_local = mod(point, vec3(size)) - vec3(size/2.0);
float s1 = length(s1_local) - 0.3;
return s1;
}
vec3 getnormal(vec3 p) {
float e = EPS_DIST;
return normalize(vec3(
sdf(p + vec3(e, 0.0, 0.0)) - sdf(p - vec3(e, 0.0, 0.0)),
sdf(p + vec3(0.0, e, 0.0)) - sdf(p - vec3(0.0, e, 0.0)),
sdf(p + vec3(0.0, 0.0, e)) - sdf(p - vec3(0.0, 0.0, e))
));
}
vec4 march(vec3 point, vec3 ray, float max_depth) {
float totaldist = 0.0;
int i;
for (i = 0; i < MAX_MARCH_STEPS && totaldist < max_depth; i += 1) {
float h = sdf(point);
point += h * ray;
if (h + totaldist > max_depth)
break;
if (h < EPS_DIST) {
return vec4(point, totaldist + h);
}
totaldist += h;
}
return vec4(-1.0);
}
vec3 camray() {
vec2 ndc = uv * 2.0 - 1.0;
ndc.y = -ndc.y;
vec4 view_pos = inv_projection * vec4(ndc, 1.0, 1.0);
view_pos /= view_pos.w;
return normalize((inv_view * vec4(normalize(view_pos.xyz), 0.0)).xyz);
}
vec4 kernel(float max_depth) {
max_depth = (max_depth > 0.0)? max_depth : 1000.0;
vec3 cam_ray = camray();
return march(cam_pos.xyz, cam_ray, max_depth);
}
float t(float x, float y) { return length(texture(screen, vec2(x, y)).xyz); }
float linearize_depth(vec2 uv) {
float d = texture(depth_tex, uv).r;
if (d >= 1.0) return 1000.0; // nothing written
vec4 ndc = vec4((uv * 2.0 - 1.0), d * 2.0 - 1.0, 1.0);
vec4 view_pos = inv_projection * ndc;
view_pos /= view_pos.w;
return -view_pos.z; // view space, positive distance
}
void main() {
vec2 bg = (projection * view * vec4(uv, 1.0, 1.0)).xy;
vec3 b = backup(bg);
float max_depth = linearize_depth(uv);
vec4 hit = kernel(max_depth);
if (hit.w > 0.0) {
out_color = vec4(getnormal(hit.xyz), 1.0);
} else {
out_color = texture(screen, uv);
}
}