🔧 Fix memory leaks of more effects

This commit is contained in:
Simon Schneegans
2022-05-08 13:09:02 +02:00
parent 61bea04018
commit 317eaa2969
7 changed files with 444 additions and 256 deletions
+11 -10
View File
@@ -33,16 +33,17 @@ const utils = Me.imports.src.utils;
// New effects must be registered here and in prefs.js. // New effects must be registered here and in prefs.js.
const ALL_EFFECTS = [ const ALL_EFFECTS = [
// Me.imports.src.Apparition.Apparition, Me.imports.src.Apparition.Apparition,
// Me.imports.src.BrokenGlass.BrokenGlass, Me.imports.src.BrokenGlass.BrokenGlass,
// Me.imports.src.EnergizeA.EnergizeA, Me.imports.src.EnergizeA.EnergizeA,
// Me.imports.src.EnergizeB.EnergizeB, Me.imports.src.EnergizeB.EnergizeB,
Me.imports.src.Fire.Fire, Me.imports.src.Hexagon.Hexagon, Me.imports.src.Fire.Fire,
// Me.imports.src.Matrix.Matrix, Me.imports.src.Hexagon.Hexagon,
// Me.imports.src.SnapOfDisintegration.SnapOfDisintegration, Me.imports.src.Matrix.Matrix,
// Me.imports.src.TRexAttack.TRexAttack, Me.imports.src.SnapOfDisintegration.SnapOfDisintegration,
// Me.imports.src.TVEffect.TVEffect, Me.imports.src.TRexAttack.TRexAttack,
// Me.imports.src.Wisps.Wisps, Me.imports.src.TVEffect.TVEffect,
Me.imports.src.Wisps.Wisps,
]; ];
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
+11 -10
View File
@@ -32,16 +32,17 @@ const utils = Me.imports.src.utils;
// New effects must be registered here and in extension.js. // New effects must be registered here and in extension.js.
const ALL_EFFECTS = [ const ALL_EFFECTS = [
// Me.imports.src.Apparition.Apparition, Me.imports.src.Apparition.Apparition,
// Me.imports.src.BrokenGlass.BrokenGlass, Me.imports.src.BrokenGlass.BrokenGlass,
// Me.imports.src.EnergizeA.EnergizeA, Me.imports.src.EnergizeA.EnergizeA,
// Me.imports.src.EnergizeB.EnergizeB, Me.imports.src.EnergizeB.EnergizeB,
Me.imports.src.Fire.Fire, Me.imports.src.Hexagon.Hexagon, Me.imports.src.Fire.Fire,
// Me.imports.src.Matrix.Matrix, Me.imports.src.Hexagon.Hexagon,
// Me.imports.src.SnapOfDisintegration.SnapOfDisintegration, Me.imports.src.Matrix.Matrix,
// Me.imports.src.TRexAttack.TRexAttack, Me.imports.src.SnapOfDisintegration.SnapOfDisintegration,
// Me.imports.src.TVEffect.TVEffect, Me.imports.src.TRexAttack.TRexAttack,
// Me.imports.src.Wisps.Wisps, Me.imports.src.TVEffect.TVEffect,
Me.imports.src.Wisps.Wisps,
]; ];
// This template widget class is defined at the bottom of this file. // This template widget class is defined at the bottom of this file.
+107 -62
View File
@@ -25,8 +25,13 @@ const utils = Me.imports.src.utils;
// This effect looks a bit like the transporter effect from TOS. // // This effect looks a bit like the transporter effect from TOS. //
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect is registered further down in this file. // The shader class for this effect is registered further down in this file. When this
let Shader = null; // effect is used for the first time, an instance of this shader class is created. Once
// the effect is finished, the shader will be stored in the freeShaders array and will
// then be reused if a new shader is requested. ShaderClass which will be used whenever
// this effect is used.
let ShaderClass = null;
let freeShaders = [];
// The effect class is completely static. It can be used to get some metadata (like the // The effect class is completely static. It can be used to get some metadata (like the
// effect's name or supported GNOME Shell versions), to initialize the respective page of // effect's name or supported GNOME Shell versions), to initialize the respective page of
@@ -75,9 +80,21 @@ var EnergizeA = class EnergizeA {
// ---------------------------------------------------------------- API for extension.js // ---------------------------------------------------------------- API for extension.js
// This is called from extension.js whenever a window is closed with this effect. // This is called from extension.js whenever a window is opened or closed with this
static createShader(actor, settings, forOpening) { // effect. It returns an instance of the shader class, trying to reuse previously
return new Shader(settings, forOpening); // created shaders.
static getShader(actor, settings, forOpening) {
let shader;
if (freeShaders.length == 0) {
shader = new ShaderClass();
} else {
shader = freeShaders.pop();
}
shader.setUniforms(actor, settings, forOpening);
return shader;
} }
// The tweakTransition() is called from extension.js to tweak a window's open / close // The tweakTransition() is called from extension.js to tweak a window's open / close
@@ -97,6 +114,12 @@ var EnergizeA = class EnergizeA {
'scale-y': {from: 1.0, to: 1.0, mode: 3} 'scale-y': {from: 1.0, to: 1.0, mode: 3}
}; };
} }
// This is called from extension.js if the extension is disabled. This should free all
// static resources.
static cleanUp() {
freeShaders = [];
}
} }
@@ -108,79 +131,100 @@ var EnergizeA = class EnergizeA {
if (utils.isInShellProcess()) { if (utils.isInShellProcess()) {
const Clutter = imports.gi.Clutter; const {Clutter, Shell} = imports.gi;
const shaderSnippets = Me.imports.src.shaderSnippets; const shaderSnippets = Me.imports.src.shaderSnippets;
Shader = GObject.registerClass({}, class Shader extends Clutter.ShaderEffect { ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
_init(settings, forOpening) { // This is called when the effect is used for the first time. This can be used to
super._init({shader_type: Clutter.ShaderType.FRAGMENT_SHADER}); // store all required uniform locations.
_init() {
super._init();
const color = Clutter.Color.from_string(settings.get_string('energize-a-color'))[1]; this._uForOpening = this.get_uniform_location('uForOpening');
this._uColor = this.get_uniform_location('uColor');
this._uScale = this.get_uniform_location('uScale');
}
// If we are currently performing integration test, the animation uses a fixed seed. // This is called each time the effect is used. This can be used to retrieve the
const testMode = settings.get_boolean('test-mode'); // configuration from the settings and update all uniforms accordingly.
setUniforms(actor, settings, forOpening) {
const c = Clutter.Color.from_string(settings.get_string('energize-a-color'))[1];
this.set_shader_source(` // clang-format off
this.set_uniform_float(this._uForOpening, 1, [forOpening]);
this.set_uniform_float(this._uColor, 3, [c.red / 255, c.green / 255, c.blue / 255]);
this.set_uniform_float(this._uScale, 1, [settings.get_double('energize-a-scale')]);
// clang-format on
}
// Inject some common shader snippets. // This is called by extension.js when the shader is not used anymore. We will store
${shaderSnippets.standardUniforms()} // this instance of the shader so that it can be re-used in th future.
${shaderSnippets.noise()} free() {
${shaderSnippets.edgeMask()} freeShaders.push(this);
}
const vec2 SEED = vec2(${testMode ? 0 : Math.random()}, // This is called by the constructor. This is means it's only called when the effect
${testMode ? 0 : Math.random()}); // is used for the first time.
const float FADE_IN_TIME = 0.3; vfunc_build_pipeline() {
const float FADE_OUT_TIME = 0.6; const declarations = `
const float HEART_FADE_TIME = 0.3; // Inject some common shader snippets.
const float EDGE_FADE_WIDTH = 50; ${shaderSnippets.standardUniforms()}
${shaderSnippets.noise()}
${shaderSnippets.edgeMask()}
// This method returns two values: uniform bool uForOpening;
// result.x: A mask for the particles. uniform vec3 uColor;
// result.y: The opacity of the fading window. uniform float uScale;
vec2 getMasks() {
float fadeInProgress = clamp(uProgress/FADE_IN_TIME, 0, 1);
float fadeOutProgress = clamp((uProgress-FADE_IN_TIME)/FADE_OUT_TIME, 0, 1);
float heartProgress = clamp((uProgress-(1.0-HEART_FADE_TIME))/HEART_FADE_TIME, 0, 1);
// Compute mask for the "atom" particles. const float FADE_IN_TIME = 0.3;
float dist = length(cogl_tex_coord_in[0].st - 0.5) * 4.0; const float FADE_OUT_TIME = 0.6;
float atomMask = smoothstep(0.0, 1.0, (fadeInProgress * 2.0 - dist + 1.0)); const float HEART_FADE_TIME = 0.3;
atomMask *= fadeInProgress; const float EDGE_FADE_WIDTH = 50;
atomMask *= smoothstep(1.0, 0.0, fadeOutProgress);
// Fade-out the masks at the window edges. // This method returns two values:
float edgeFade = getAbsoluteEdgeMask(EDGE_FADE_WIDTH); // result.x: A mask for the particles.
atomMask *= edgeFade; // result.y: The opacity of the fading window.
vec2 getMasks() {
float fadeInProgress = clamp(uProgress/FADE_IN_TIME, 0, 1);
float fadeOutProgress = clamp((uProgress-FADE_IN_TIME)/FADE_OUT_TIME, 0, 1);
float heartProgress = clamp((uProgress-(1.0-HEART_FADE_TIME))/HEART_FADE_TIME, 0, 1);
float heartMask = getRelativeEdgeMask(0.5); // Compute mask for the "atom" particles.
heartMask = 3.0 * pow(heartMask, 5); float dist = length(cogl_tex_coord_in[0].st - 0.5) * 4.0;
heartMask *= fadeOutProgress; float atomMask = smoothstep(0.0, 1.0, (fadeInProgress * 2.0 - dist + 1.0));
heartMask *= 1.0 - heartProgress; atomMask *= fadeInProgress;
atomMask = clamp(heartMask+atomMask, 0, 1); atomMask *= smoothstep(1.0, 0.0, fadeOutProgress);
// Compute fading window opacity. // Fade-out the masks at the window edges.
float windowMask = pow(1.0 - fadeOutProgress, 2.0); float edgeFade = getAbsoluteEdgeMask(EDGE_FADE_WIDTH);
atomMask *= edgeFade;
#if ${forOpening ? '1' : '0'} float heartMask = getRelativeEdgeMask(0.5);
windowMask = 1.0 - windowMask; heartMask = 3.0 * pow(heartMask, 5);
#endif heartMask *= fadeOutProgress;
heartMask *= 1.0 - heartProgress;
atomMask = clamp(heartMask+atomMask, 0, 1);
return vec2(atomMask, windowMask); // Compute fading window opacity.
} float windowMask = pow(1.0 - fadeOutProgress, 2.0);
void main() { if (uForOpening) {
windowMask = 1.0 - windowMask;
}
return vec2(atomMask, windowMask);
}
`;
const code = `
vec2 masks = getMasks(); vec2 masks = getMasks();
vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st); vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st);
vec3 effectColor = vec3(${color.red / 255},
${color.green / 255},
${color.blue / 255});
// Dissolve window to effect color / transparency. // Dissolve window to effect color / transparency.
cogl_color_out = mix(vec4(effectColor, 1.0) * windowColor.a, windowColor, 0.2 * masks.y + 0.8) * masks.y; cogl_color_out = mix(vec4(uColor, 1.0) * windowColor.a, windowColor, 0.2 * masks.y + 0.8) * masks.y;
vec2 scaledUV = (cogl_tex_coord_in[0].st-0.5) * (1.0 + 0.1*uProgress); vec2 scaledUV = (cogl_tex_coord_in[0].st-0.5) * (1.0 + 0.1*uProgress);
scaledUV /= ${settings.get_double('energize-a-scale')}; scaledUV /= uScale;
// Add molecule particles. // Add molecule particles.
vec2 uv = scaledUV + vec2(0, 0.1*uTime); vec2 uv = scaledUV + vec2(0, 0.1*uTime);
@@ -194,15 +238,16 @@ if (utils.isInShellProcess()) {
particles += 0.5 * pow(0.2 * (1.0 / (1.0 - atoms)-1.0), 2); particles += 0.5 * pow(0.2 * (1.0 / (1.0 - atoms)-1.0), 2);
} }
cogl_color_out.rgb += effectColor * particles * masks.x; cogl_color_out.rgb += uColor * particles * masks.x;
// These are pretty useful for understanding how this works. // These are pretty useful for understanding how this works.
// cogl_color_out = vec4(masks, 0.0, 1.0); // cogl_color_out = vec4(masks, 0.0, 1.0);
// cogl_color_out = vec4(vec3(masks.x), 1.0); // cogl_color_out = vec4(vec3(masks.x), 1.0);
// cogl_color_out = vec4(vec3(masks.y), 1.0); // cogl_color_out = vec4(vec3(masks.y), 1.0);
// cogl_color_out = vec4(vec3(particles), 1.0); // cogl_color_out = vec4(vec3(particles), 1.0);
} `;
`);
}; this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
}
}); });
} }
+124 -80
View File
@@ -25,8 +25,13 @@ const utils = Me.imports.src.utils;
// This effect looks a bit like the transporter effect from TNG. // // This effect looks a bit like the transporter effect from TNG. //
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect is registered further down in this file. // The shader class for this effect is registered further down in this file. When this
let Shader = null; // effect is used for the first time, an instance of this shader class is created. Once
// the effect is finished, the shader will be stored in the freeShaders array and will
// then be reused if a new shader is requested. ShaderClass which will be used whenever
// this effect is used.
let ShaderClass = null;
let freeShaders = [];
// The effect class is completely static. It can be used to get some metadata (like the // The effect class is completely static. It can be used to get some metadata (like the
// effect's name or supported GNOME Shell versions), to initialize the respective page of // effect's name or supported GNOME Shell versions), to initialize the respective page of
@@ -75,9 +80,21 @@ var EnergizeB = class EnergizeB {
// ---------------------------------------------------------------- API for extension.js // ---------------------------------------------------------------- API for extension.js
// This is called from extension.js whenever a window is closed with this effect. // This is called from extension.js whenever a window is opened or closed with this
static createShader(actor, settings, forOpening) { // effect. It returns an instance of the shader class, trying to reuse previously
return new Shader(settings, forOpening); // created shaders.
static getShader(actor, settings, forOpening) {
let shader;
if (freeShaders.length == 0) {
shader = new ShaderClass();
} else {
shader = freeShaders.pop();
}
shader.setUniforms(actor, settings, forOpening);
return shader;
} }
// The tweakTransition() is called from extension.js to tweak a window's open / close // The tweakTransition() is called from extension.js to tweak a window's open / close
@@ -97,6 +114,12 @@ var EnergizeB = class EnergizeB {
'scale-y': {from: 1.0, to: 1.0, mode: 3} 'scale-y': {from: 1.0, to: 1.0, mode: 3}
}; };
} }
// This is called from extension.js if the extension is disabled. This should free all
// static resources.
static cleanUp() {
freeShaders = [];
}
} }
@@ -108,112 +131,132 @@ var EnergizeB = class EnergizeB {
if (utils.isInShellProcess()) { if (utils.isInShellProcess()) {
const Clutter = imports.gi.Clutter; const {Clutter, Shell} = imports.gi;
const shaderSnippets = Me.imports.src.shaderSnippets; const shaderSnippets = Me.imports.src.shaderSnippets;
Shader = GObject.registerClass({}, class Shader extends Clutter.ShaderEffect { ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
_init(settings, forOpening) { // This is called when the effect is used for the first time. This can be used to
super._init({shader_type: Clutter.ShaderType.FRAGMENT_SHADER}); // store all required uniform locations.
_init() {
super._init();
const color = Clutter.Color.from_string(settings.get_string('energize-b-color'))[1]; this._uForOpening = this.get_uniform_location('uForOpening');
this._uColor = this.get_uniform_location('uColor');
this._uScale = this.get_uniform_location('uScale');
}
// If we are currently performing integration test, the animation uses a fixed seed. // This is called each time the effect is used. This can be used to retrieve the
const testMode = settings.get_boolean('test-mode'); // configuration from the settings and update all uniforms accordingly.
setUniforms(actor, settings, forOpening) {
const c = Clutter.Color.from_string(settings.get_string('energize-b-color'))[1];
this.set_shader_source(` // clang-format off
this.set_uniform_float(this._uForOpening, 1, [forOpening]);
this.set_uniform_float(this._uColor, 3, [c.red / 255, c.green / 255, c.blue / 255]);
this.set_uniform_float(this._uScale, 1, [settings.get_double('energize-b-scale')]);
// clang-format on
}
// Inject some common shader snippets. // This is called by extension.js when the shader is not used anymore. We will store
${shaderSnippets.standardUniforms()} // this instance of the shader so that it can be re-used in th future.
${shaderSnippets.noise()} free() {
${shaderSnippets.edgeMask()} freeShaders.push(this);
}
const vec2 SEED = vec2(${testMode ? 0 : Math.random()}, // This is called by the constructor. This is means it's only called when the effect
${testMode ? 0 : Math.random()}); // is used for the first time.
const float SHOWER_TIME = 0.3; vfunc_build_pipeline() {
const float SHOWER_WIDTH = 0.3; const declarations = `
const float STREAK_TIME = 0.6; // Inject some common shader snippets.
const float EDGE_FADE = 50; ${shaderSnippets.standardUniforms()}
const float SCALE = ${settings.get_double('energize-b-scale')}; ${shaderSnippets.noise()}
${shaderSnippets.edgeMask()}
// This method returns four values: uniform bool uForOpening;
// result.x: A mask for the particles which lead the shower. uniform vec3 uColor;
// result.y: A mask for the streaks which follow the shower particles. uniform float uScale;
// result.z: A mask for the final "atom" particles.
// result.w: The opacity of the fading window.
vec4 getMasks() {
float showerProgress = uProgress/SHOWER_TIME;
float streakProgress = clamp((uProgress-SHOWER_TIME)/STREAK_TIME, 0, 1);
float fadeProgress = clamp((uProgress-SHOWER_TIME)/(1.0 - SHOWER_TIME), 0, 1);
// Gradient from top to bottom. const float SHOWER_TIME = 0.3;
float t = cogl_tex_coord_in[0].t; const float SHOWER_WIDTH = 0.3;
const float STREAK_TIME = 0.6;
const float EDGE_FADE = 50;
// A smooth gradient which moves to the bottom within the showerProgress. // This method returns four values:
float showerMask = smoothstep(1, 0, abs(showerProgress - t - SHOWER_WIDTH) / SHOWER_WIDTH); // result.x: A mask for the particles which lead the shower.
// result.y: A mask for the streaks which follow the shower particles.
// result.z: A mask for the final "atom" particles.
// result.w: The opacity of the fading window.
vec4 getMasks() {
float showerProgress = uProgress/SHOWER_TIME;
float streakProgress = clamp((uProgress-SHOWER_TIME)/STREAK_TIME, 0, 1);
float fadeProgress = clamp((uProgress-SHOWER_TIME)/(1.0 - SHOWER_TIME), 0, 1);
// This is 1 above the streak mask. // Gradient from top to bottom.
float streakMask = (showerProgress - t - SHOWER_WIDTH) > 0 ? 1 : 0; float t = cogl_tex_coord_in[0].t;
// Compute mask for the "atom" particles. // A smooth gradient which moves to the bottom within the showerProgress.
float atomMask = getRelativeEdgeMask(0.2); float showerMask = smoothstep(1, 0, abs(showerProgress - t - SHOWER_WIDTH) / SHOWER_WIDTH);
atomMask = max(0, atomMask - showerMask);
atomMask *= streakMask;
atomMask *= sqrt(1-fadeProgress*fadeProgress);
// Make some particles visible in the streaks. // This is 1 above the streak mask.
showerMask += 0.05 * streakMask; float streakMask = (showerProgress - t - SHOWER_WIDTH) > 0 ? 1 : 0;
// Add shower mask to streak mask. // Compute mask for the "atom" particles.
streakMask = max(streakMask, showerMask); float atomMask = getRelativeEdgeMask(0.2);
atomMask = max(0, atomMask - showerMask);
atomMask *= streakMask;
atomMask *= sqrt(1-fadeProgress*fadeProgress);
// Fade-out the masks at the window edges. // Make some particles visible in the streaks.
float edgeFade = getAbsoluteEdgeMask(EDGE_FADE); showerMask += 0.05 * streakMask;
streakMask *= edgeFade;
showerMask *= edgeFade;
// Fade-out the masks from top to bottom.
float fade = smoothstep(0.0, 1.0, 1.0 + t - 2.0 * streakProgress);
streakMask *= fade;
showerMask *= fade;
// Compute fading window opacity. // Add shower mask to streak mask.
float windowMask = pow(1.0 - fadeProgress, 2.0); streakMask = max(streakMask, showerMask);
#if ${forOpening ? '1' : '0'} // Fade-out the masks at the window edges.
windowMask = 1.0 - windowMask; float edgeFade = getAbsoluteEdgeMask(EDGE_FADE);
#endif streakMask *= edgeFade;
showerMask *= edgeFade;
// Fade-out the masks from top to bottom.
float fade = smoothstep(0.0, 1.0, 1.0 + t - 2.0 * streakProgress);
streakMask *= fade;
showerMask *= fade;
return vec4(showerMask, streakMask, atomMask, windowMask); // Compute fading window opacity.
} float windowMask = pow(1.0 - fadeProgress, 2.0);
void main() { if (uForOpening) {
windowMask = 1.0 - windowMask;
}
return vec4(showerMask, streakMask, atomMask, windowMask);
}
`;
const code = `
vec4 masks = getMasks(); vec4 masks = getMasks();
vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st); vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st);
vec3 effectColor = vec3(${color.red / 255},
${color.green / 255},
${color.blue / 255});
// Dissolve window to effect color / transparency. // Dissolve window to effect color / transparency.
cogl_color_out = mix(vec4(effectColor, 1.0) * windowColor.a, windowColor, 0.5 * masks.w + 0.5) * masks.w; cogl_color_out = mix(vec4(uColor, 1.0) * windowColor.a, windowColor, 0.5 * masks.w + 0.5) * masks.w;
// Add leading shower particles. // Add leading shower particles.
vec2 showerUV = cogl_tex_coord_in[0].st + vec2(0, -0.7*uProgress/SHOWER_TIME); vec2 showerUV = cogl_tex_coord_in[0].st + vec2(0, -0.7*uProgress/SHOWER_TIME);
showerUV *= 0.02 * vec2(uSizeX, uSizeY) / SCALE; showerUV *= 0.02 * vec2(uSizeX, uSizeY) / uScale;
float shower = pow(simplex2D(showerUV), 10.0); float shower = pow(simplex2D(showerUV), 10.0);
cogl_color_out.rgb += effectColor * shower * masks.x; cogl_color_out.rgb += uColor * shower * masks.x;
// Add trailing streak lines. // Add trailing streak lines.
vec2 streakUV = cogl_tex_coord_in[0].st + vec2(0, -uProgress/SHOWER_TIME); vec2 streakUV = cogl_tex_coord_in[0].st + vec2(0, -uProgress/SHOWER_TIME);
streakUV *= vec2(0.05 * uSizeX, 0.001 * uSizeY) / SCALE; streakUV *= vec2(0.05 * uSizeX, 0.001 * uSizeY) / uScale;
float streaks = simplex2DFractal(streakUV) * 0.5; float streaks = simplex2DFractal(streakUV) * 0.5;
cogl_color_out.rgb += effectColor * streaks * masks.y; cogl_color_out.rgb += uColor * streaks * masks.y;
// Add glimmering atoms. // Add glimmering atoms.
vec2 atomUV = cogl_tex_coord_in[0].st + vec2(0, -0.025*uProgress/SHOWER_TIME); vec2 atomUV = cogl_tex_coord_in[0].st + vec2(0, -0.025*uProgress/SHOWER_TIME);
atomUV *= 0.2 * vec2(uSizeX, uSizeY) / SCALE; atomUV *= 0.2 * vec2(uSizeX, uSizeY) / uScale;
float atoms = pow((simplex3D(vec3(atomUV, uTime))), 5.0); float atoms = pow((simplex3D(vec3(atomUV, uTime))), 5.0);
cogl_color_out.rgb += effectColor * atoms * masks.z; cogl_color_out.rgb += uColor * atoms * masks.z;
// These are pretty useful for understanding how this works. // These are pretty useful for understanding how this works.
// cogl_color_out = vec4(masks.rgb, 1.0); // cogl_color_out = vec4(masks.rgb, 1.0);
@@ -224,8 +267,9 @@ if (utils.isInShellProcess()) {
// cogl_color_out = vec4(vec3(shower), 1.0); // cogl_color_out = vec4(vec3(shower), 1.0);
// cogl_color_out = vec4(vec3(streaks), 1.0); // cogl_color_out = vec4(vec3(streaks), 1.0);
// cogl_color_out = vec4(vec3(atoms), 1.0); // cogl_color_out = vec4(vec3(atoms), 1.0);
} `;
`);
}; this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
}
}); });
} }
+1 -1
View File
@@ -255,7 +255,7 @@ if (utils.isInShellProcess()) {
coords = (coords + grow + shrink) / scale; coords = (coords + grow + shrink) / scale;
// Add some wind. // Add some wind.
coords.x = WIND_INTENSITY * coords.x * progress * (uForOpening ? 1.0 : -1.0); coords.x += WIND_INTENSITY * progress * (uForOpening ? 1.0 : -1.0);
// Now check wether there is actually something in the current dust layer at // Now check wether there is actually something in the current dust layer at
// the coords position. // the coords position.
+75 -29
View File
@@ -27,8 +27,13 @@ const utils = Me.imports.src.utils;
// the center. // // the center. //
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect is registered further down in this file. // The shader class for this effect is registered further down in this file. When this
let Shader = null; // effect is used for the first time, an instance of this shader class is created. Once
// the effect is finished, the shader will be stored in the freeShaders array and will
// then be reused if a new shader is requested. ShaderClass which will be used whenever
// this effect is used.
let ShaderClass = null;
let freeShaders = [];
// The effect class is completely static. It can be used to get some metadata (like the // The effect class is completely static. It can be used to get some metadata (like the
// effect's name or supported GNOME Shell versions), to initialize the respective page of // effect's name or supported GNOME Shell versions), to initialize the respective page of
@@ -76,9 +81,21 @@ var TVEffect = class TVEffect {
// ---------------------------------------------------------------- API for extension.js // ---------------------------------------------------------------- API for extension.js
// This is called from extension.js whenever a window is closed with this effect. // This is called from extension.js whenever a window is opened or closed with this
static createShader(actor, settings, forOpening) { // effect. It returns an instance of the shader class, trying to reuse previously
return new Shader(settings, forOpening); // created shaders.
static getShader(actor, settings, forOpening) {
let shader;
if (freeShaders.length == 0) {
shader = new ShaderClass();
} else {
shader = freeShaders.pop();
}
shader.setUniforms(actor, settings, forOpening);
return shader;
} }
// The tweakTransition() is called from extension.js to tweak a window's open / close // The tweakTransition() is called from extension.js to tweak a window's open / close
@@ -98,6 +115,12 @@ var TVEffect = class TVEffect {
'scale-y': {from: forOpening ? 0.5 : 1.0, to: forOpening ? 1.0 : 0.5, mode: 3} 'scale-y': {from: forOpening ? 0.5 : 1.0, to: forOpening ? 1.0 : 0.5, mode: 3}
}; };
} }
// This is called from extension.js if the extension is disabled. This should free all
// static resources.
static cleanUp() {
freeShaders = [];
}
} }
@@ -109,29 +132,55 @@ var TVEffect = class TVEffect {
if (utils.isInShellProcess()) { if (utils.isInShellProcess()) {
const Clutter = imports.gi.Clutter; const {Clutter, Shell} = imports.gi;
const shaderSnippets = Me.imports.src.shaderSnippets; const shaderSnippets = Me.imports.src.shaderSnippets;
Shader = GObject.registerClass({}, class Shader extends Clutter.ShaderEffect { ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
_init(settings, forOpening) { // This is called when the effect is used for the first time. This can be used to
super._init({shader_type: Clutter.ShaderType.FRAGMENT_SHADER}); // store all required uniform locations.
_init() {
super._init();
const color = Clutter.Color.from_string(settings.get_string('tv-effect-color'))[1]; this._uForOpening = this.get_uniform_location('uForOpening');
this._uColor = this.get_uniform_location('uColor');
}
this.set_shader_source(` // This is called each time the effect is used. This can be used to retrieve the
// configuration from the settings and update all uniforms accordingly.
setUniforms(actor, settings, forOpening) {
const c = Clutter.Color.from_string(settings.get_string('tv-effect-color'))[1];
// Inject some common shader snippets. // clang-format off
${shaderSnippets.standardUniforms()} this.set_uniform_float(this._uForOpening, 1, [forOpening]);
this.set_uniform_float(this._uColor, 3, [c.red / 255, c.green / 255, c.blue / 255]);
// clang-format on
}
const float BLUR_WIDTH = 0.01; // Width of the gradients. // This is called by extension.js when the shader is not used anymore. We will store
const float TB_TIME = 0.7; // Relative time for the top/bottom animation. // this instance of the shader so that it can be re-used in th future.
const float LR_TIME = 0.4; // Relative time for the left/right animation. free() {
const float LR_DELAY = 0.6; // Delay after which the left/right animation starts. freeShaders.push(this);
const float FF_TIME = 0.1; // Relative time for the final fade to transparency. }
void main() { // This is called by the constructor. This is means it's only called when the effect
// is used for the first time.
vfunc_build_pipeline() {
const declarations = `
// Inject some common shader snippets.
${shaderSnippets.standardUniforms()}
float progress = ${forOpening ? '1.0-uProgress' : 'uProgress'}; uniform bool uForOpening;
uniform vec3 uColor;
const float BLUR_WIDTH = 0.01; // Width of the gradients.
const float TB_TIME = 0.7; // Relative time for the top/bottom animation.
const float LR_TIME = 0.4; // Relative time for the left/right animation.
const float LR_DELAY = 0.6; // Delay after which the left/right animation starts.
const float FF_TIME = 0.1; // Relative time for the final fade to transparency.
`;
const code = `
float progress = uForOpening ? 1.0-uProgress : uProgress;
// All of these are in [0..1] during the different stages of the animation. // All of these are in [0..1] during the different stages of the animation.
// tb refers to the top-bottom animation. // tb refers to the top-bottom animation.
@@ -158,11 +207,7 @@ if (utils.isInShellProcess()) {
float mask = tbMask * lrMask * ffMask; float mask = tbMask * lrMask * ffMask;
vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st); vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st);
vec4 effectColor = vec4(${color.red / 255}, windowColor.rgb = mix(windowColor.rgb, uColor * windowColor.a, smoothstep(0, 1, progress));
${color.green / 255},
${color.blue / 255}, 1.0) * windowColor.a;
windowColor.rgb = mix(windowColor.rgb, effectColor.rgb, smoothstep(0, 1, progress));
cogl_color_out = windowColor * mask; cogl_color_out = windowColor * mask;
@@ -171,8 +216,9 @@ if (utils.isInShellProcess()) {
// cogl_color_out = vec4(vec3(lrMask), 1); // cogl_color_out = vec4(vec3(lrMask), 1);
// cogl_color_out = vec4(vec3(ffMask), 1); // cogl_color_out = vec4(vec3(ffMask), 1);
// cogl_color_out = vec4(vec3(mask), 1); // cogl_color_out = vec4(vec3(mask), 1);
} `;
`);
}; this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
}
}); });
} }
+115 -64
View File
@@ -26,8 +26,13 @@ const utils = Me.imports.src.utils;
// fairies. It's implemented with several overlaid grids of randomly moving points. // // fairies. It's implemented with several overlaid grids of randomly moving points. //
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect is registered further down in this file. // The shader class for this effect is registered further down in this file. When this
let Shader = null; // effect is used for the first time, an instance of this shader class is created. Once
// the effect is finished, the shader will be stored in the freeShaders array and will
// then be reused if a new shader is requested. ShaderClass which will be used whenever
// this effect is used.
let ShaderClass = null;
let freeShaders = [];
// This will be called in various places where a unique identifier for this effect is // This will be called in various places where a unique identifier for this effect is
// required. It should match the prefix of the settings keys which store whether the // required. It should match the prefix of the settings keys which store whether the
@@ -76,9 +81,21 @@ var Wisps = class Wisps {
// ---------------------------------------------------------------- API for extension.js // ---------------------------------------------------------------- API for extension.js
// This is called from extension.js whenever a window is closed with this effect. // This is called from extension.js whenever a window is opened or closed with this
static createShader(actor, settings, forOpening) { // effect. It returns an instance of the shader class, trying to reuse previously
return new Shader(settings, forOpening); // created shaders.
static getShader(actor, settings, forOpening) {
let shader;
if (freeShaders.length == 0) {
shader = new ShaderClass();
} else {
shader = freeShaders.pop();
}
shader.setUniforms(actor, settings, forOpening);
return shader;
} }
// The tweakTransition() is called from extension.js to tweak a window's open / close // The tweakTransition() is called from extension.js to tweak a window's open / close
@@ -98,6 +115,12 @@ var Wisps = class Wisps {
'scale-y': {from: forOpening ? 0.9 : 1.0, to: forOpening ? 1.0 : 0.9, mode: 3} 'scale-y': {from: forOpening ? 0.9 : 1.0, to: forOpening ? 1.0 : 0.9, mode: 3}
}; };
} }
// This is called from extension.js if the extension is disabled. This should free all
// static resources.
static cleanUp() {
freeShaders = [];
}
} }
@@ -109,88 +132,115 @@ var Wisps = class Wisps {
if (utils.isInShellProcess()) { if (utils.isInShellProcess()) {
const Clutter = imports.gi.Clutter; const {Clutter, Shell} = imports.gi;
const shaderSnippets = Me.imports.src.shaderSnippets; const shaderSnippets = Me.imports.src.shaderSnippets;
Shader = GObject.registerClass({}, class Shader extends Clutter.ShaderEffect { ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
_init(settings, forOpening) { // This is called when the effect is used for the first time. This can be used to
super._init({shader_type: Clutter.ShaderType.FRAGMENT_SHADER}); // store all required uniform locations.
_init() {
super._init();
const color = Clutter.Color.from_string(settings.get_string('wisps-color'))[1]; this._uForOpening = this.get_uniform_location('uForOpening');
this._uSeed = this.get_uniform_location('uSeed');
this._uColor = this.get_uniform_location('uColor');
this._uScale = this.get_uniform_location('uScale');
}
// This is called each time the effect is used. This can be used to retrieve the
// configuration from the settings and update all uniforms accordingly.
setUniforms(actor, settings, forOpening) {
const c = Clutter.Color.from_string(settings.get_string('wisps-color'))[1];
// If we are currently performing integration test, the animation uses a fixed seed. // If we are currently performing integration test, the animation uses a fixed seed.
const testMode = settings.get_boolean('test-mode'); const testMode = settings.get_boolean('test-mode');
this.set_shader_source(` // clang-format off
this.set_uniform_float(this._uForOpening, 1, [forOpening]);
this.set_uniform_float(this._uSeed, 2, [testMode ? 0 : Math.random(), testMode ? 0 : Math.random()]);
this.set_uniform_float(this._uColor, 3, [c.red / 255, c.green / 255, c.blue / 255]);
this.set_uniform_float(this._uScale, 1, [settings.get_double('wisps-scale')]);
// clang-format on
}
// Inject some common shader snippets. // This is called by extension.js when the shader is not used anymore. We will store
${shaderSnippets.standardUniforms()} // this instance of the shader so that it can be re-used in th future.
${shaderSnippets.noise()} free() {
${shaderSnippets.edgeMask()} freeShaders.push(this);
}
const vec2 SEED = vec2(${testMode ? 0 : Math.random()}, // This is called by the constructor. This is means it's only called when the effect
${testMode ? 0 : Math.random()}); // is used for the first time.
const float WISPS_RADIUS = 20.0; vfunc_build_pipeline() {
const float WISPS_SPEED = 10.0; const declarations = `
const float WISPS_SPACING = 40 + WISPS_RADIUS; // Inject some common shader snippets.
const int WISPS_LAYERS = 8; ${shaderSnippets.standardUniforms()}
const float WISPS_IN_TIME = 0.5; ${shaderSnippets.noise()}
const float WINDOW_OUT_TIME = 1.0; ${shaderSnippets.edgeMask()}
// Returns a grid of randomly moving points. Each grid cell contains one point which uniform bool uForOpening;
// moves on an ellipse. uniform vec2 uSeed;
float getWisps(vec2 texCoords, float gridSize, vec2 seed) { uniform vec3 uColor;
uniform float uScale;
// Shift coordinates by a random offset and make sure the have a 1:1 aspect ratio. const float WISPS_RADIUS = 20.0;
vec2 coords = (texCoords + hash22(seed)) * vec2(uSizeX, uSizeY); const float WISPS_SPEED = 10.0;
const float WISPS_SPACING = 40 + WISPS_RADIUS;
const int WISPS_LAYERS = 8;
const float WISPS_IN_TIME = 0.5;
const float WINDOW_OUT_TIME = 1.0;
// Apply global scale. // Returns a grid of randomly moving points. Each grid cell contains one point which
coords /= gridSize; // moves on an ellipse.
float getWisps(vec2 texCoords, float gridSize, vec2 seed) {
// Get grid cell coordinates in [0..1]. // Shift coordinates by a random offset and make sure the have a 1:1 aspect ratio.
vec2 cellUV = mod(coords, vec2(1)); vec2 coords = (texCoords + hash22(seed)) * vec2(uSizeX, uSizeY);
// This is unique for each cell. // Apply global scale.
vec2 cellID = coords-cellUV + vec2(362.456); coords /= gridSize;
// Add random rotation, scale and offset to each grid cell. // Get grid cell coordinates in [0..1].
float speed = mix(10.0, 15.0, hash12(cellID*seed*134.451)) / gridSize * WISPS_SPEED; vec2 cellUV = mod(coords, vec2(1));
float rotation = mix( 0.0, 6.283, hash12(cellID*seed*54.4129));
float radius = mix( 0.5, 1.0, hash12(cellID*seed*19.1249)) * WISPS_RADIUS;
float roundness = mix(-1.0, 1.0, hash12(cellID*seed*7.51949));
vec2 offset = vec2(sin(speed * (uTime+1)) * roundness, cos(speed * (uTime+1))); // This is unique for each cell.
offset *= 0.5 - 0.5 * radius / gridSize; vec2 cellID = coords-cellUV + vec2(362.456);
offset = vec2(offset.x * cos(rotation) - offset.y * sin(rotation),
offset.x * sin(rotation) + offset.y * cos(rotation));
cellUV += offset; // Add random rotation, scale and offset to each grid cell.
float speed = mix(10.0, 15.0, hash12(cellID*seed*134.451)) / gridSize * WISPS_SPEED;
float rotation = mix( 0.0, 6.283, hash12(cellID*seed*54.4129));
float radius = mix( 0.5, 1.0, hash12(cellID*seed*19.1249)) * WISPS_RADIUS;
float roundness = mix(-1.0, 1.0, hash12(cellID*seed*7.51949));
// Use distance to center of shifted / rotated UV coordinates to draw a glaring point. vec2 offset = vec2(sin(speed * (uTime+1)) * roundness, cos(speed * (uTime+1)));
float dist = length(cellUV - 0.5) * gridSize / radius; offset *= 0.5 - 0.5 * radius / gridSize;
if (dist < 1.0) { offset = vec2(offset.x * cos(rotation) - offset.y * sin(rotation),
return min(10, 0.01 / pow(dist, 2.0)); offset.x * sin(rotation) + offset.y * cos(rotation));
cellUV += offset;
// Use distance to center of shifted / rotated UV coordinates to draw a glaring point.
float dist = length(cellUV - 0.5) * gridSize / radius;
if (dist < 1.0) {
return min(10, 0.01 / pow(dist, 2.0));
}
return 0.0;
} }
`;
return 0.0; const code = `
} float progress = uForOpening ? 1.0-uProgress : uProgress;
void main() {
float progress = ${forOpening ? '1.0-uProgress' : 'uProgress'};
// Get the color of the window. // Get the color of the window.
vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st); vec4 windowColor = texture2D(uTexture, cogl_tex_coord_in[0].st);
vec4 effectColor = vec4(${color.red / 255},
${color.green / 255},
${color.blue / 255}, 1.0) * windowColor.a;
// Compute several layers of moving wisps. // Compute several layers of moving wisps.
vec2 uv = (cogl_tex_coord_in[0].st-0.5) / mix(1.0, 0.5, progress) + 0.5; vec2 uv = (cogl_tex_coord_in[0].st-0.5) / mix(1.0, 0.5, progress) + 0.5;
uv /= ${settings.get_double('wisps-scale')}; uv /= uScale;
float wisps = 0; float wisps = 0;
for (int i=0; i<WISPS_LAYERS; ++i) { for (int i=0; i<WISPS_LAYERS; ++i) {
wisps += getWisps(uv*0.3, WISPS_SPACING, SEED * (i+1)); wisps += getWisps(uv*0.3, WISPS_SPACING, uSeed * (i+1));
} }
// Compute shrinking edge mask. // Compute shrinking edge mask.
@@ -207,15 +257,16 @@ if (utils.isInShellProcess()) {
cogl_color_out = windowColor * windowMask * mask; cogl_color_out = windowColor * windowMask * mask;
// Add the wisps. // Add the wisps.
cogl_color_out += min(wispsIn, 1.0 - wispsOut) * wisps * effectColor * mask; cogl_color_out += min(wispsIn, 1.0 - wispsOut) * wisps * vec4(uColor, 1.0) * mask * windowColor.a;
// These are pretty useful for understanding how this works. // These are pretty useful for understanding how this works.
// cogl_color_out = vec4(vec3(windowMask), 1.0); // cogl_color_out = vec4(vec3(windowMask), 1.0);
// cogl_color_out = vec4(vec3(wisps), 1.0); // cogl_color_out = vec4(vec3(wisps), 1.0);
// cogl_color_out = vec4(vec3(noise), 1.0); // cogl_color_out = vec4(vec3(noise), 1.0);
// cogl_color_out = vec4(vec3(mask*min(wispsIn, 1.0 - wispsOut)), 1.0); // cogl_color_out = vec4(vec3(mask*min(wispsIn, 1.0 - wispsOut)), 1.0);
} `;
`);
}; this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, code, true);
}
}); });
} }