chromaticAberrationPostProcess.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Vector2 } from "Math/math";
  2. import { Nullable } from "types";
  3. import { PostProcess, PostProcessOptions } from "./postProcess";
  4. import { Effect } from "Materials/effect";
  5. import { Camera } from "Cameras/camera";
  6. import { Engine } from "Engine/engine";
  7. import { Constants } from "Engine/constants";
  8. /**
  9. * The ChromaticAberrationPostProcess separates the rgb channels in an image to produce chromatic distortion around the edges of the screen
  10. */
  11. export class ChromaticAberrationPostProcess extends PostProcess {
  12. /**
  13. * The amount of seperation of rgb channels (default: 30)
  14. */
  15. aberrationAmount = 30;
  16. /**
  17. * The amount the effect will increase for pixels closer to the edge of the screen. (default: 0)
  18. */
  19. radialIntensity = 0;
  20. /**
  21. * The normilized direction in which the rgb channels should be seperated. If set to 0,0 radial direction will be used. (default: Vector2(0.707,0.707))
  22. */
  23. direction = new Vector2(0.707, 0.707);
  24. /**
  25. * The center position where the radialIntensity should be around. [0.5,0.5 is center of screen, 1,1 is top right corder] (default: Vector2(0.5 ,0.5))
  26. */
  27. centerPosition = new Vector2(0.5, 0.5);
  28. /**
  29. * Creates a new instance ChromaticAberrationPostProcess
  30. * @param name The name of the effect.
  31. * @param screenWidth The width of the screen to apply the effect on.
  32. * @param screenHeight The height of the screen to apply the effect on.
  33. * @param options The required width/height ratio to downsize to before computing the render pass.
  34. * @param camera The camera to apply the render pass to.
  35. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  36. * @param engine The engine which the post process will be applied. (default: current engine)
  37. * @param reusable If the post process can be reused on the same frame. (default: false)
  38. * @param textureType Type of textures used when performing the post process. (default: 0)
  39. * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
  40. */
  41. constructor(name: string, screenWidth: number, screenHeight: number, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  42. super(name, "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity", "centerPosition"], [], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, blockCompilation);
  43. this.onApplyObservable.add((effect: Effect) => {
  44. effect.setFloat('chromatic_aberration', this.aberrationAmount);
  45. effect.setFloat('screen_width', screenWidth);
  46. effect.setFloat('screen_height', screenHeight);
  47. effect.setFloat('radialIntensity', this.radialIntensity);
  48. effect.setFloat2('direction', this.direction.x, this.direction.y);
  49. effect.setFloat2('centerPosition', this.centerPosition.x, this.centerPosition.y);
  50. });
  51. }
  52. }