convolutionPostProcess.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /**
  9. * The ConvolutionPostProcess applies a 3x3 kernel to every pixel of the
  10. * input texture to perform effects such as edge detection or sharpening
  11. * See http://en.wikipedia.org/wiki/Kernel_(image_processing)
  12. */
  13. export class ConvolutionPostProcess extends PostProcess{
  14. /**
  15. * Creates a new instance ConvolutionPostProcess
  16. * @param name The name of the effect.
  17. * @param kernel Array of 9 values corrisponding to the 3x3 kernel to be applied
  18. * @param options The required width/height ratio to downsize to before computing the render pass.
  19. * @param camera The camera to apply the render pass to.
  20. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  21. * @param engine The engine which the post process will be applied. (default: current engine)
  22. * @param reusable If the post process can be reused on the same frame. (default: false)
  23. * @param textureType Type of textures used when performing the post process. (default: 0)
  24. */
  25. constructor(name: string,
  26. /** Array of 9 values corrisponding to the 3x3 kernel to be applied */
  27. public kernel: number[],
  28. options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT) {
  29. super(name, "convolution", ["kernel", "screenSize"], null, options, camera, samplingMode, engine, reusable, null, textureType);
  30. this.onApply = (effect: Effect) => {
  31. effect.setFloat2("screenSize", this.width, this.height);
  32. effect.setArray("kernel", this.kernel);
  33. };
  34. }
  35. // Statics
  36. /**
  37. * Edge detection 0 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  38. */
  39. public static EdgeDetect0Kernel = [1, 0, -1, 0, 0, 0, -1, 0, 1];
  40. /**
  41. * Edge detection 1 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  42. */
  43. public static EdgeDetect1Kernel = [0, 1, 0, 1, -4, 1, 0, 1, 0];
  44. /**
  45. * Edge detection 2 see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  46. */
  47. public static EdgeDetect2Kernel = [-1, -1, -1, -1, 8, -1, -1, -1, -1];
  48. /**
  49. * Kernel to sharpen an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  50. */
  51. public static SharpenKernel = [0, -1, 0, -1, 5, -1, 0, -1, 0];
  52. /**
  53. * Kernel to emboss an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  54. */
  55. public static EmbossKernel = [-2, -1, 0, -1, 1, 1, 0, 1, 2];
  56. /**
  57. * Kernel to blur an image see https://en.wikipedia.org/wiki/Kernel_(image_processing)
  58. */
  59. public static GaussianKernel = [0, 1, 0, 1, 1, 1, 0, 1, 0];
  60. }