grainPostProcess.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Nullable } from "../types";
  2. import { Camera } from "../Cameras/camera";
  3. import { Effect } from "../Materials/effect";
  4. import { PostProcess, PostProcessOptions } from "./postProcess";
  5. import { Engine } from "../Engines/engine";
  6. import { Constants } from "../Engines/constants";
  7. import "../Shaders/grain.fragment";
  8. /**
  9. * The GrainPostProcess adds noise to the image at mid luminance levels
  10. */
  11. export class GrainPostProcess extends PostProcess {
  12. /**
  13. * The intensity of the grain added (default: 30)
  14. */
  15. public intensity: number = 30;
  16. /**
  17. * If the grain should be randomized on every frame
  18. */
  19. public animated: boolean = false;
  20. /**
  21. * Creates a new instance of @see GrainPostProcess
  22. * @param name The name of the effect.
  23. * @param options The required width/height ratio to downsize to before computing the render pass.
  24. * @param camera The camera to apply the render pass to.
  25. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  26. * @param engine The engine which the post process will be applied. (default: current engine)
  27. * @param reusable If the post process can be reused on the same frame. (default: false)
  28. * @param textureType Type of textures used when performing the post process. (default: 0)
  29. * @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)
  30. */
  31. constructor(name: string, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  32. super(name, "grain", ["intensity", "animatedSeed"], [], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, blockCompilation);
  33. this.onApplyObservable.add((effect: Effect) => {
  34. effect.setFloat('intensity', this.intensity);
  35. effect.setFloat('animatedSeed', this.animated ? Math.random() + 1 : 1);
  36. });
  37. }
  38. }