grainPostProcess.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import { _TypeStore } from '../Misc/typeStore';
  9. import { serialize, SerializationHelper } from '../Misc/decorators';
  10. declare type Scene = import("../scene").Scene;
  11. /**
  12. * The GrainPostProcess adds noise to the image at mid luminance levels
  13. */
  14. export class GrainPostProcess extends PostProcess {
  15. /**
  16. * The intensity of the grain added (default: 30)
  17. */
  18. @serialize()
  19. public intensity: number = 30;
  20. /**
  21. * If the grain should be randomized on every frame
  22. */
  23. @serialize()
  24. public animated: boolean = false;
  25. /**
  26. * Gets a string identifying the name of the class
  27. * @returns "GrainPostProcess" string
  28. */
  29. public getClassName(): string {
  30. return "GrainPostProcess";
  31. }
  32. /**
  33. * Creates a new instance of @see GrainPostProcess
  34. * @param name The name of the effect.
  35. * @param options The required width/height ratio to downsize to before computing the render pass.
  36. * @param camera The camera to apply the render pass to.
  37. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  38. * @param engine The engine which the post process will be applied. (default: current engine)
  39. * @param reusable If the post process can be reused on the same frame. (default: false)
  40. * @param textureType Type of textures used when performing the post process. (default: 0)
  41. * @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)
  42. */
  43. constructor(name: string, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  44. super(name, "grain", ["intensity", "animatedSeed"], [], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, blockCompilation);
  45. this.onApplyObservable.add((effect: Effect) => {
  46. effect.setFloat('intensity', this.intensity);
  47. effect.setFloat('animatedSeed', this.animated ? Math.random() + 1 : 1);
  48. });
  49. }
  50. /** @hidden */
  51. public static _Parse(parsedPostProcess: any, targetCamera: Camera, scene: Scene, rootUrl: string) {
  52. return SerializationHelper.Parse(() => {
  53. return new GrainPostProcess(
  54. parsedPostProcess.name,
  55. parsedPostProcess.options, targetCamera,
  56. parsedPostProcess.renderTargetSamplingMode,
  57. scene.getEngine(), parsedPostProcess.reusable);
  58. }, parsedPostProcess, scene, rootUrl);
  59. }
  60. }
  61. _TypeStore.RegisteredTypes["BABYLON.GrainPostProcess"] = GrainPostProcess;