chromaticAberrationPostProcess.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Vector2 } from "../Maths/math.vector";
  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 "../Engines/engine";
  7. import { Constants } from "../Engines/constants";
  8. import "../Shaders/chromaticAberration.fragment";
  9. /**
  10. * The ChromaticAberrationPostProcess separates the rgb channels in an image to produce chromatic distortion around the edges of the screen
  11. */
  12. export class ChromaticAberrationPostProcess extends PostProcess {
  13. /**
  14. * The amount of seperation of rgb channels (default: 30)
  15. */
  16. aberrationAmount = 30;
  17. /**
  18. * The amount the effect will increase for pixels closer to the edge of the screen. (default: 0)
  19. */
  20. radialIntensity = 0;
  21. /**
  22. * 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))
  23. */
  24. direction = new Vector2(0.707, 0.707);
  25. /**
  26. * 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))
  27. */
  28. centerPosition = new Vector2(0.5, 0.5);
  29. /**
  30. * Creates a new instance ChromaticAberrationPostProcess
  31. * @param name The name of the effect.
  32. * @param screenWidth The width of the screen to apply the effect on.
  33. * @param screenHeight The height of the screen to apply the effect on.
  34. * @param options The required width/height ratio to downsize to before computing the render pass.
  35. * @param camera The camera to apply the render pass to.
  36. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  37. * @param engine The engine which the post process will be applied. (default: current engine)
  38. * @param reusable If the post process can be reused on the same frame. (default: false)
  39. * @param textureType Type of textures used when performing the post process. (default: 0)
  40. * @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)
  41. */
  42. 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) {
  43. super(name, "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity", "centerPosition"], [], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, blockCompilation);
  44. this.onApplyObservable.add((effect: Effect) => {
  45. effect.setFloat('chromatic_aberration', this.aberrationAmount);
  46. effect.setFloat('screen_width', screenWidth);
  47. effect.setFloat('screen_height', screenHeight);
  48. effect.setFloat('radialIntensity', this.radialIntensity);
  49. effect.setFloat2('direction', this.direction.x, this.direction.y);
  50. effect.setFloat2('centerPosition', this.centerPosition.x, this.centerPosition.y);
  51. });
  52. }
  53. }