🔧 Use factory to create shaders

This commit is contained in:
Simon Schneegans
2022-05-14 13:58:28 +02:00
parent cb4b87811e
commit e1017b7707
17 changed files with 767 additions and 817 deletions
+31 -53
View File
@@ -13,25 +13,50 @@
'use strict';
const GObject = imports.gi.GObject;
const _ = imports.gettext.domain('burn-my-windows').gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const utils = Me.imports.src.utils;
const Effect = Me.imports.src.Effect.Effect;
const ShaderFactory = Me.imports.src.ShaderFactory.ShaderFactory;
//////////////////////////////////////////////////////////////////////////////////////////
// This effect hides the actor by violently sucking it into the void of magic. //
// towards the middle and then hiding the resulting line from left and right towards //
// the center. //
//////////////////////////////////////////////////////////////////////////////////////////
// The effect class can be used to get some metadata (like the effect's name or supported
// GNOME Shell versions), to initialize the respective page of the settings dialog, as
// well as to create the actual shader for the effect.
var Apparition = class Apparition extends Effect {
var Apparition = class Apparition {
// The constructor creates a ShaderFactory which will be used by extension.js to create
// shader instances for this effect. The shaders will be automagically created using the
// GLSL file in resources/shaders/<nick>.glsl. The callback will be called for each
// newly created shader instance.
constructor() {
this.shaderFactory = new ShaderFactory(this.getNick(), (shader) => {
// Store uniform locations of newly created shaders.
shader._uSeed = shader.get_uniform_location('uSeed');
shader._uShake = shader.get_uniform_location('uShake');
shader._uTwirl = shader.get_uniform_location('uTwirl');
shader._uSuction = shader.get_uniform_location('uSuction');
shader._uRandomness = shader.get_uniform_location('uRandomness');
// Write all uniform values at the start of each animation.
shader.connect('begin-animation', (shader, settings) => {
// If we are performing an integration tests, we use a fixed seed.
const testMode = settings.get_boolean('test-mode');
// clang-format off
shader.set_uniform_float(shader._uSeed, 2, [testMode ? 0 : Math.random(), testMode ? 0 : Math.random()]);
shader.set_uniform_float(shader._uShake, 1, [settings.get_double('apparition-shake-intensity')]);
shader.set_uniform_float(shader._uTwirl, 1, [settings.get_double('apparition-twirl-intensity')]);
shader.set_uniform_float(shader._uSuction, 1, [settings.get_double('apparition-suction-intensity')]);
shader.set_uniform_float(shader._uRandomness, 1, [settings.get_double('apparition-randomness')]);
// clang-format on
});
});
}
// ---------------------------------------------------------------------------- metadata
@@ -83,51 +108,4 @@ var Apparition = class Apparition extends Effect {
getActorScale(settings) {
return {x: 2.0, y: 2.0};
}
// This is called by the effect's base class whenever a new shader is required. Since
// this shader depends on classes by GNOME Shell, we register it locally in this method
// as this file is also included from the preferences dialog where those classes would
// not be available.
createShader() {
// Only register the shader class when this method is called for the first time.
if (!this._ShaderClass) {
const Shader = Me.imports.src.Shader.Shader;
this._ShaderClass = GObject.registerClass({}, class ShaderClass extends Shader {
// We use the constructor of the shader to store all required uniform locations.
_init(effect) {
super._init(effect);
this._uSeed = this.get_uniform_location('uSeed');
this._uShake = this.get_uniform_location('uShake');
this._uTwirl = this.get_uniform_location('uTwirl');
this._uSuction = this.get_uniform_location('uSuction');
this._uRandomness = this.get_uniform_location('uRandomness');
}
// This is called once each time the shader is used. This can be used to retrieve
// the configuration from the settings and update all uniforms accordingly.
beginAnimation(actor, settings, forOpening) {
super.beginAnimation(actor, settings, forOpening);
// If we are currently performing integration test, the animation uses a fixed
// seed.
const testMode = settings.get_boolean('test-mode');
// clang-format off
this.set_uniform_float(this._uSeed, 2, [testMode ? 0 : Math.random(), testMode ? 0 : Math.random()]);
this.set_uniform_float(this._uShake, 1, [settings.get_double('apparition-shake-intensity')]);
this.set_uniform_float(this._uTwirl, 1, [settings.get_double('apparition-twirl-intensity')]);
this.set_uniform_float(this._uSuction, 1, [settings.get_double('apparition-suction-intensity')]);
this.set_uniform_float(this._uRandomness, 1, [settings.get_double('apparition-randomness')]);
// clang-format on
}
});
}
// Finally, return a new instance of the shader class.
return new this._ShaderClass(this);
}
}