convolutionPostProcess.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { PostProcess, PostProcessOptions } from "./postProcess";
  2. import { Nullable } from "../types";
  3. import { Camera } from "../Cameras/camera";
  4. import { Engine } from "../Engines/engine";
  5. import { Effect } from "../Materials/effect";
  6. import { Constants } from "../Engines/constants";
  7. import "../Shaders/convolution.fragment";
  8. import { _TypeStore } from '../Misc/typeStore';
  9. import { serialize, SerializationHelper } from '../Misc/decorators';
  10. declare type Scene = import("../scene").Scene;
  11. /**
  12. * The ConvolutionPostProcess applies a 3x3 kernel to every pixel of the
  13. * input texture to perform effects such as edge detection or sharpening
  14. * See http://en.wikipedia.org/wiki/Kernel_(image_processing)
  15. */
  16. export class ConvolutionPostProcess extends PostProcess {
  17. /** Array of 9 values corresponding to the 3x3 kernel to be applied */
  18. @serialize()
  19. public kernel: number[];
  20. /**
  21. * Gets a string identifying the name of the class
  22. * @returns "ConvolutionPostProcess" string
  23. */
  24. public getClassName(): string {
  25. return "ConvolutionPostProcess";
  26. }
  27. /**
  28. * Creates a new instance ConvolutionPostProcess
  29. * @param name The name of the effect.
  30. * @param kernel Array of 9 values corresponding to the 3x3 kernel to be applied
  31. * @param options The required width/height ratio to downsize to before computing the render pass.
  32. * @param camera The camera to apply the render pass to.
  33. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  34. * @param engine The engine which the post process will be applied. (default: current engine)
  35. * @param reusable If the post process can be reused on the same frame. (default: false)
  36. * @param textureType Type of textures used when performing the post process. (default: 0)
  37. */
  38. constructor(name: string,
  39. kernel: number[],
  40. options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT) {
  41. super(name, "convolution", ["kernel", "screenSize"], null, options, camera, samplingMode, engine, reusable, null, textureType);
  42. this.kernel = kernel;
  43. this.onApply = (effect: Effect) => {
  44. effect.setFloat2("screenSize", this.width, this.height);
  45. effect.setArray("kernel", this.kernel);
  46. };
  47. }
  48. /** @hidden */
  49. public static _Parse(parsedPostProcess: any, targetCamera: Camera, scene: Scene, rootUrl: string): Nullable<ConvolutionPostProcess> {
  50. return SerializationHelper.Parse(() => {
  51. return new ConvolutionPostProcess(
  52. parsedPostProcess.name, parsedPostProcess.kernel,
  53. parsedPostProcess.options, targetCamera,
  54. parsedPostProcess.renderTargetSamplingMode,
  55. scene.getEngine(), parsedPostProcess.reusable, parsedPostProcess.textureType);
  56. }, parsedPostProcess, scene, rootUrl);
  57. }
  58. // Statics
  59. /**
  60. * Edge detection 0 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  61. */
  62. public static EdgeDetect0Kernel = [1, 0, -1, 0, 0, 0, -1, 0, 1];
  63. /**
  64. * Edge detection 1 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  65. */
  66. public static EdgeDetect1Kernel = [0, 1, 0, 1, -4, 1, 0, 1, 0];
  67. /**
  68. * Edge detection 2 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  69. */
  70. public static EdgeDetect2Kernel = [-1, -1, -1, -1, 8, -1, -1, -1, -1];
  71. /**
  72. * Kernel to sharpen an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  73. */
  74. public static SharpenKernel = [0, -1, 0, -1, 5, -1, 0, -1, 0];
  75. /**
  76. * Kernel to emboss an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  77. */
  78. public static EmbossKernel = [-2, -1, 0, -1, 1, 1, 0, 1, 2];
  79. /**
  80. * Kernel to blur an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  81. */
  82. public static GaussianKernel = [0, 1, 0, 1, 1, 1, 0, 1, 0];
  83. }
  84. _TypeStore.RegisteredTypes["BABYLON.ConvolutionPostProcess"] = ConvolutionPostProcess;