noise_effect.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import GLib from 'gi://GLib';
  2. import GObject from 'gi://GObject';
  3. import Clutter from 'gi://Clutter';
  4. import Shell from 'gi://Shell';
  5. const SHADER_PATH = GLib.filename_from_uri(GLib.uri_resolve_relative(import.meta.url, 'noise_effect.glsl', GLib.UriFlags.NONE))[0];
  6. const get_shader_source = _ => {
  7. try {
  8. return Shell.get_file_contents_utf8_sync(SHADER_PATH);
  9. } catch (e) {
  10. console.warn(`[Blur my Shell] error loading shader from ${SHADER_PATH}: ${e}`);
  11. return null;
  12. }
  13. };
  14. export const NoiseEffect = new GObject.registerClass({
  15. GTypeName: "NoiseEffect",
  16. Properties: {
  17. 'noise': GObject.ParamSpec.double(
  18. `noise`,
  19. `Noise`,
  20. `Amount of noise integrated with the image`,
  21. GObject.ParamFlags.READWRITE,
  22. 0.0, 1.0,
  23. 0.4,
  24. ),
  25. 'lightness': GObject.ParamSpec.double(
  26. `lightness`,
  27. `Lightness`,
  28. `Lightness of the grey used for the noise`,
  29. GObject.ParamFlags.READWRITE,
  30. 0.0, 2.0,
  31. 0.4,
  32. ),
  33. }
  34. }, class NoiseShader extends Clutter.ShaderEffect {
  35. constructor(params, settings) {
  36. super(params);
  37. this._noise = null;
  38. this._lightness = null;
  39. this._static = true;
  40. this._settings = settings;
  41. if (params.noise)
  42. this.noise = params.noise;
  43. if (params.lightness)
  44. this.lightness = params.lightness;
  45. // set shader source
  46. this._source = get_shader_source();
  47. if (this._source)
  48. this.set_shader_source(this._source);
  49. this.update_enabled();
  50. }
  51. get noise() {
  52. return this._noise;
  53. }
  54. set noise(value) {
  55. if (this._noise !== value) {
  56. this._noise = value;
  57. this.set_uniform_value('noise', parseFloat(this._noise - 1e-6));
  58. }
  59. this.update_enabled();
  60. }
  61. get lightness() {
  62. return this._lightness;
  63. }
  64. set lightness(value) {
  65. if (this._lightness !== value) {
  66. this._lightness = value;
  67. this.set_uniform_value('lightness', parseFloat(this._lightness - 1e-6));
  68. }
  69. }
  70. update_enabled() {
  71. // don't anything if this._settings is undefined (when calling super)
  72. if (this._settings === undefined)
  73. return;
  74. this.set_enabled(
  75. this.noise > 0 &&
  76. this._settings.COLOR_AND_NOISE &&
  77. this._static
  78. );
  79. }
  80. vfunc_paint_target(paint_node = null, paint_context = null) {
  81. this.set_uniform_value("tex", 0);
  82. if (paint_node && paint_context)
  83. super.vfunc_paint_target(paint_node, paint_context);
  84. else if (paint_node)
  85. super.vfunc_paint_target(paint_node);
  86. else
  87. super.vfunc_paint_target();
  88. }
  89. });