chromaticAberrationPostProcess.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import { _TypeStore } from '../Misc/typeStore';
  10. import { serialize, SerializationHelper } from '../Misc/decorators';
  11. declare type Scene = import("../scene").Scene;
  12. /**
  13. * The ChromaticAberrationPostProcess separates the rgb channels in an image to produce chromatic distortion around the edges of the screen
  14. */
  15. export class ChromaticAberrationPostProcess extends PostProcess {
  16. /**
  17. * The amount of seperation of rgb channels (default: 30)
  18. */
  19. @serialize()
  20. aberrationAmount = 30;
  21. /**
  22. * The amount the effect will increase for pixels closer to the edge of the screen. (default: 0)
  23. */
  24. @serialize()
  25. radialIntensity = 0;
  26. /**
  27. * 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))
  28. */
  29. @serialize()
  30. direction = new Vector2(0.707, 0.707);
  31. /**
  32. * 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))
  33. */
  34. @serialize()
  35. centerPosition = new Vector2(0.5, 0.5);
  36. /** The width of the screen to apply the effect on */
  37. @serialize()
  38. screenWidth: number;
  39. /** The height of the screen to apply the effect on */
  40. @serialize()
  41. screenHeight: number;
  42. /**
  43. * Gets a string identifying the name of the class
  44. * @returns "ChromaticAberrationPostProcess" string
  45. */
  46. public getClassName(): string {
  47. return "ChromaticAberrationPostProcess";
  48. }
  49. /**
  50. * Creates a new instance ChromaticAberrationPostProcess
  51. * @param name The name of the effect.
  52. * @param screenWidth The width of the screen to apply the effect on.
  53. * @param screenHeight The height of the screen to apply the effect on.
  54. * @param options The required width/height ratio to downsize to before computing the render pass.
  55. * @param camera The camera to apply the render pass to.
  56. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  57. * @param engine The engine which the post process will be applied. (default: current engine)
  58. * @param reusable If the post process can be reused on the same frame. (default: false)
  59. * @param textureType Type of textures used when performing the post process. (default: 0)
  60. * @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)
  61. */
  62. 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) {
  63. super(name, "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity", "centerPosition"], [], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, blockCompilation);
  64. this.screenWidth = screenWidth;
  65. this.screenHeight = screenHeight;
  66. this.onApplyObservable.add((effect: Effect) => {
  67. effect.setFloat('chromatic_aberration', this.aberrationAmount);
  68. effect.setFloat('screen_width', screenWidth);
  69. effect.setFloat('screen_height', screenHeight);
  70. effect.setFloat('radialIntensity', this.radialIntensity);
  71. effect.setFloat2('direction', this.direction.x, this.direction.y);
  72. effect.setFloat2('centerPosition', this.centerPosition.x, this.centerPosition.y);
  73. });
  74. }
  75. /** @hidden */
  76. public static _Parse(parsedPostProcess: any, targetCamera: Camera, scene: Scene, rootUrl: string): Nullable<ChromaticAberrationPostProcess> {
  77. return SerializationHelper.Parse(() => {
  78. return new ChromaticAberrationPostProcess(
  79. parsedPostProcess.name,
  80. parsedPostProcess.screenWidth, parsedPostProcess.screenHeight,
  81. parsedPostProcess.options, targetCamera,
  82. parsedPostProcess.renderTargetSamplingMode,
  83. scene.getEngine(), parsedPostProcess.reusable,
  84. parsedPostProcess.textureType, false);
  85. }, parsedPostProcess, scene, rootUrl);
  86. }
  87. }
  88. _TypeStore.RegisteredTypes["BABYLON.ChromaticAberrationPostProcess"] = ChromaticAberrationPostProcess;