Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function applyMotionBlur(actor, n, dir, wFn=function() { return 1; }) {
function blurShaderSource() {
function SAMPLE(offx, offy, w) {
return `pixel += ${w} * texture2D (tex, cogl_tex_coord_in[0].st + vec2(width_inv, height_inv) * vec2 (${offx}, ${offy}));`
}
function motionBlur(n, sign=1) {
let vs = []
let weights = []
let sum = 0
for (let i = 0; i < n; i++) {
// let w = n-i+1
// let w = Math.pow((i+1), 1.5)
let w = wFn(i, n)
weights.push(w)
sum += w
}
for (let i = 0; i < n; i++) {
vs.push(SAMPLE(i*sign, 0, weights[i]/sum))
}
// vs.push(`pixel /= ${n}.0;`)
return vs.join("\n")
}
// Note: `tex` doesn't need to be set (it's not a special name either..)
// Setting it to 0 from the outside have no effect, setting it to 1 messes things up.
// Setting it to 0 inside the shader source also messes things up...
// Texture coordinates go from 0 to 1. The texture pixel width does not seem to be available.
// Unsure about how edge conditions are resolved.
return `
uniform sampler2D tex;
uniform float width_inv;
uniform float height_inv;
void main() {
vec4 pixel = vec4(0.0, 0.0, 0.0, 0.0);
${motionBlur(n, dir)}
cogl_color_out = pixel;
}
`;
}
actor.clear_effects()
let effect = new Clutter.ShaderEffect();
effect.set_shader_source(blurShaderSource(n, dir))
effect.set_uniform_value("width_inv", 1/actor.width)
effect.set_uniform_value("height_inv", 1/actor.height)
actor.add_effect_with_name("Foo", effect)
}
function repl() {
function linear(i, n) {
return n - i + 1
}
function power(i, n) {
return Math.pow((n-i+1), 2)
}
function constant(i, n) {
return 1
}
actor = space.cloneContainer
actor = metaWindow.clone
actor.clear_effects()
applyMotionBlur(actor, 20, -1, linear)
applyMotionBlur(actor, 4, 20, power)
applyMotionBlur(actor, 30, -1, power)
imports.gi.St.set_slow_down_factor(3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment