noise.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import GObject from 'gi://GObject';
  2. import * as utils from '../conveniences/utils.js';
  3. const Shell = await utils.import_in_shell_only('gi://Shell');
  4. const Clutter = await utils.import_in_shell_only('gi://Clutter');
  5. const SHADER_FILENAME = 'noise.glsl';
  6. const DEFAULT_PARAMS = {
  7. noise: 0.4, lightness: 0.4
  8. };
  9. export const NoiseEffect = utils.IS_IN_PREFERENCES ?
  10. { default_params: DEFAULT_PARAMS } :
  11. new GObject.registerClass({
  12. GTypeName: "NoiseEffect",
  13. Properties: {
  14. 'noise': GObject.ParamSpec.double(
  15. `noise`,
  16. `Noise`,
  17. `Amount of noise integrated with the image`,
  18. GObject.ParamFlags.READWRITE,
  19. 0.0, 1.0,
  20. 0.4,
  21. ),
  22. 'lightness': GObject.ParamSpec.double(
  23. `lightness`,
  24. `Lightness`,
  25. `Lightness of the grey used for the noise`,
  26. GObject.ParamFlags.READWRITE,
  27. 0.0, 2.0,
  28. 0.4,
  29. ),
  30. }
  31. }, class NoiseEffect extends Clutter.ShaderEffect {
  32. constructor(params) {
  33. super(params);
  34. utils.setup_params(this, params);
  35. // set shader source
  36. this._source = utils.get_shader_source(Shell, SHADER_FILENAME, import.meta.url);
  37. if (this._source)
  38. this.set_shader_source(this._source);
  39. }
  40. static get default_params() {
  41. return DEFAULT_PARAMS;
  42. }
  43. get noise() {
  44. return this._noise;
  45. }
  46. set noise(value) {
  47. if (this._noise !== value) {
  48. this._noise = value;
  49. this.set_uniform_value('noise', parseFloat(this._noise - 1e-6));
  50. this.set_enabled(this.noise > 0. && this.lightness != 1);
  51. }
  52. }
  53. get lightness() {
  54. return this._lightness;
  55. }
  56. set lightness(value) {
  57. if (this._lightness !== value) {
  58. this._lightness = value;
  59. this.set_uniform_value('lightness', parseFloat(this._lightness - 1e-6));
  60. this.set_enabled(this.noise > 0. && this.lightness != 1);
  61. }
  62. }
  63. });