motionBlurPostProcess.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Nullable } from "../types";
  2. import { Logger } from "../Misc/logger";
  3. import { Vector2 } from "../Maths/math";
  4. import { Camera } from "../Cameras/camera";
  5. import { Effect } from "../Materials/effect";
  6. import { PostProcess, PostProcessOptions } from "./postProcess";
  7. import { Constants } from "../Engines/constants";
  8. import { GeometryBufferRenderer } from "../Rendering/geometryBufferRenderer";
  9. import { Scene } from "../scene";
  10. import "../Shaders/motionBlur.fragment";
  11. declare type Engine = import("../Engines/engine").Engine;
  12. /**
  13. * The Motion Blur Post Process which blurs an image based on the objects velocity in scene.
  14. * Velocity can be affected by each object's rotation, position and scale depending on the transformation speed.
  15. * As an example, all you have to do is to create the post-process:
  16. * var mb = new BABYLON.MotionBlurPostProcess(
  17. * 'mb', // The name of the effect.
  18. * scene, // The scene containing the objects to blur according to their velocity.
  19. * 1.0, // The required width/height ratio to downsize to before computing the render pass.
  20. * camera // The camera to apply the render pass to.
  21. * );
  22. * Then, all objects moving, rotating and/or scaling will be blurred depending on the transformation speed.
  23. */
  24. export class MotionBlurPostProcess extends PostProcess {
  25. /**
  26. * Defines how much the image is blurred by the movement. Default value is equal to 1
  27. */
  28. public motionStrength: number = 1;
  29. /**
  30. * Gets the number of iterations are used for motion blur quality. Default value is equal to 32
  31. */
  32. public get motionBlurSamples(): number {
  33. return this._motionBlurSamples;
  34. }
  35. /**
  36. * Sets the number of iterations to be used for motion blur quality
  37. */
  38. public set motionBlurSamples(samples: number) {
  39. this._motionBlurSamples = samples;
  40. if (this._geometryBufferRenderer) {
  41. this.updateEffect("#define GEOMETRY_SUPPORTED\n#define SAMPLES " + samples.toFixed(1));
  42. }
  43. }
  44. private _motionBlurSamples: number = 32;
  45. private _geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
  46. /**
  47. * Creates a new instance MotionBlurPostProcess
  48. * @param name The name of the effect.
  49. * @param scene The scene containing the objects to blur according to their velocity.
  50. * @param options The required width/height ratio to downsize to before computing the render pass.
  51. * @param camera The camera to apply the render pass to.
  52. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  53. * @param engine The engine which the post process will be applied. (default: current engine)
  54. * @param reusable If the post process can be reused on the same frame. (default: false)
  55. * @param textureType Type of textures used when performing the post process. (default: 0)
  56. * @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)
  57. */
  58. constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  59. super(name, "motionBlur", ["motionStrength", "motionScale", "screenSize"], ["velocitySampler"], options, camera, samplingMode, engine, reusable, "#define GEOMETRY_SUPPORTED\n#define SAMPLES 64.0", textureType, undefined, null, blockCompilation);
  60. this._geometryBufferRenderer = scene.enableGeometryBufferRenderer();
  61. if (!this._geometryBufferRenderer) {
  62. // Geometry buffer renderer is not supported. So, work as a passthrough.
  63. Logger.Warn("Multiple Render Target support needed to compute object based motion blur");
  64. this.updateEffect();
  65. } else {
  66. // Geometry buffer renderer is supported.
  67. this._geometryBufferRenderer.enableVelocity = true;
  68. this.onApply = (effect: Effect) => {
  69. effect.setVector2("screenSize", new Vector2(this.width, this.height));
  70. effect.setFloat("motionScale", scene.getAnimationRatio());
  71. effect.setFloat("motionStrength", this.motionStrength);
  72. if (this._geometryBufferRenderer) {
  73. const velocityIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.VELOCITY_TEXTURE_TYPE);
  74. effect.setTexture("velocitySampler", this._geometryBufferRenderer.getGBuffer().textures[velocityIndex]);
  75. }
  76. };
  77. }
  78. }
  79. /**
  80. * Disposes the post process.
  81. * @param camera The camera to dispose the post process on.
  82. */
  83. public dispose(camera?: Camera): void {
  84. if (this._geometryBufferRenderer) {
  85. // Clear previous transformation matrices dictionary used to compute objects velocities
  86. this._geometryBufferRenderer._previousTransformationMatrices = { };
  87. }
  88. super.dispose(camera);
  89. }
  90. }