virtualx-engine/servers/rendering/renderer_rd/shaders/effects/motion_vectors.glsl
Dario e7d3a7c2aa Improve visual feedback when using the motion vectors debug view option.
Replaces the current method of showing the raw values of the motion vectors buffer to display a grid of lines instead with a new shader.
2023-08-28 10:14:21 -03:00

80 lines
2.1 KiB
GLSL

#[vertex]
#version 450
#VERSION_DEFINES
layout(location = 0) out vec2 uv_interp;
void main() {
vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
}
#[fragment]
#version 450
#VERSION_DEFINES
layout(location = 0) in vec2 uv_interp;
layout(set = 0, binding = 0) uniform sampler2D source_velocity;
layout(location = 0) out vec4 frag_color;
layout(push_constant, std430) uniform Params {
vec2 resolution;
}
params;
// Based on distance to line segment from https://www.shadertoy.com/view/3tdSDj
float line_segment(in vec2 p, in vec2 a, in vec2 b) {
vec2 aspect = vec2(params.resolution.x / params.resolution.y, 1.0f);
vec2 ba = (b - a) * aspect;
vec2 pa = (p - a) * aspect;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0f, 1.0f);
return length(pa - h * ba) * (params.resolution.y / 2.0f);
}
void main() {
// Retrieve motion vector data.
float cell_size = 32.0f;
float circle_radius = 2.0f;
vec3 nan_color = vec3(1.0f, 0.0f, 0.0f);
vec3 active_color = vec3(1.0f, 0.8f, 0.1f);
vec3 inactive_color = vec3(0.5f, 0.5f, 0.5f);
vec2 pos_pixel = uv_interp * params.resolution;
vec2 cell_pos_pixel = floor(pos_pixel / cell_size) * cell_size + (cell_size * 0.5f);
vec2 cell_pos_uv = cell_pos_pixel / params.resolution;
vec2 cell_pos_previous_uv = cell_pos_uv + textureLod(source_velocity, cell_pos_uv, 0.0f).xy;
// Draw the shapes.
float epsilon = 1e-6f;
vec2 cell_pos_delta_uv = cell_pos_uv - cell_pos_previous_uv;
bool motion_active = length(cell_pos_delta_uv) > epsilon;
vec3 color;
if (any(isnan(cell_pos_delta_uv))) {
color = nan_color;
} else if (motion_active) {
color = active_color;
} else {
color = inactive_color;
}
float alpha;
if (length(cell_pos_pixel - pos_pixel) <= circle_radius) {
// Circle center.
alpha = 1.0f;
} else if (motion_active) {
// Motion vector line.
alpha = 1.0f - line_segment(uv_interp, cell_pos_uv, cell_pos_previous_uv);
} else {
// Ignore pixel.
alpha = 0.0f;
}
frag_color = vec4(color, alpha);
}