babylon.motionBlurPostProcess.ts 4.9 KB

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