motionBlurPostProcess.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Nullable } from "../types";
  2. import { Logger } from "../Misc/logger";
  3. import { Vector2 } from "../Maths/math.vector";
  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 { AbstractMesh } from "../Meshes/abstractMesh";
  11. import "../Animations/animatable";
  12. import '../Rendering/geometryBufferRendererSceneComponent';
  13. import "../Shaders/motionBlur.fragment";
  14. declare type Engine = import("../Engines/engine").Engine;
  15. /**
  16. * The Motion Blur Post Process which blurs an image based on the objects velocity in scene.
  17. * Velocity can be affected by each object's rotation, position and scale depending on the transformation speed.
  18. * As an example, all you have to do is to create the post-process:
  19. * var mb = new BABYLON.MotionBlurPostProcess(
  20. * 'mb', // The name of the effect.
  21. * scene, // The scene containing the objects to blur according to their velocity.
  22. * 1.0, // The required width/height ratio to downsize to before computing the render pass.
  23. * camera // The camera to apply the render pass to.
  24. * );
  25. * Then, all objects moving, rotating and/or scaling will be blurred depending on the transformation speed.
  26. */
  27. export class MotionBlurPostProcess extends PostProcess {
  28. /**
  29. * Defines how much the image is blurred by the movement. Default value is equal to 1
  30. */
  31. public motionStrength: number = 1;
  32. /**
  33. * Gets the number of iterations are used for motion blur quality. Default value is equal to 32
  34. */
  35. public get motionBlurSamples(): number {
  36. return this._motionBlurSamples;
  37. }
  38. /**
  39. * Sets the number of iterations to be used for motion blur quality
  40. */
  41. public set motionBlurSamples(samples: number) {
  42. this._motionBlurSamples = samples;
  43. if (this._geometryBufferRenderer) {
  44. this.updateEffect("#define GEOMETRY_SUPPORTED\n#define SAMPLES " + samples.toFixed(1));
  45. }
  46. }
  47. private _motionBlurSamples: number = 32;
  48. private _geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
  49. /**
  50. * Creates a new instance MotionBlurPostProcess
  51. * @param name The name of the effect.
  52. * @param scene The scene containing the objects to blur according to their velocity.
  53. * @param options The required width/height ratio to downsize to before computing the render pass.
  54. * @param camera The camera to apply the render pass to.
  55. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  56. * @param engine The engine which the post process will be applied. (default: current engine)
  57. * @param reusable If the post process can be reused on the same frame. (default: false)
  58. * @param textureType Type of textures used when performing the post process. (default: 0)
  59. * @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)
  60. */
  61. 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) {
  62. super(name, "motionBlur", ["motionStrength", "motionScale", "screenSize"], ["velocitySampler"], options, camera, samplingMode, engine, reusable, "#define GEOMETRY_SUPPORTED\n#define SAMPLES 64.0", textureType, undefined, null, blockCompilation);
  63. this._geometryBufferRenderer = scene.enableGeometryBufferRenderer();
  64. if (!this._geometryBufferRenderer) {
  65. // Geometry buffer renderer is not supported. So, work as a passthrough.
  66. Logger.Warn("Multiple Render Target support needed to compute object based motion blur");
  67. this.updateEffect();
  68. } else {
  69. // Geometry buffer renderer is supported.
  70. this._geometryBufferRenderer.enableVelocity = true;
  71. this.onApply = (effect: Effect) => {
  72. effect.setVector2("screenSize", new Vector2(this.width, this.height));
  73. effect.setFloat("motionScale", scene.getAnimationRatio());
  74. effect.setFloat("motionStrength", this.motionStrength);
  75. if (this._geometryBufferRenderer) {
  76. const velocityIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.VELOCITY_TEXTURE_TYPE);
  77. effect.setTexture("velocitySampler", this._geometryBufferRenderer.getGBuffer().textures[velocityIndex]);
  78. }
  79. };
  80. }
  81. }
  82. /**
  83. * Excludes the given skinned mesh from computing bones velocities.
  84. * Computing bones velocities can have a cost and that cost. The cost can be saved by calling this function and by passing the skinned mesh reference to ignore.
  85. * @param skinnedMesh The mesh containing the skeleton to ignore when computing the velocity map.
  86. */
  87. public excludeSkinnedMesh(skinnedMesh: AbstractMesh): void {
  88. if (this._geometryBufferRenderer && skinnedMesh.skeleton) {
  89. this._geometryBufferRenderer.excludedSkinnedMeshesFromVelocity.push(skinnedMesh);
  90. }
  91. }
  92. /**
  93. * Removes the given skinned mesh from the excluded meshes to integrate bones velocities while rendering the velocity map.
  94. * @param skinnedMesh The mesh containing the skeleton that has been ignored previously.
  95. * @see excludeSkinnedMesh to exclude a skinned mesh from bones velocity computation.
  96. */
  97. public removeExcludedSkinnedMesh(skinnedMesh: AbstractMesh): void {
  98. if (this._geometryBufferRenderer && skinnedMesh.skeleton) {
  99. const index = this._geometryBufferRenderer.excludedSkinnedMeshesFromVelocity.indexOf(skinnedMesh);
  100. if (index !== -1) {
  101. this._geometryBufferRenderer.excludedSkinnedMeshesFromVelocity.splice(index, 1);
  102. }
  103. }
  104. }
  105. /**
  106. * Disposes the post process.
  107. * @param camera The camera to dispose the post process on.
  108. */
  109. public dispose(camera?: Camera): void {
  110. if (this._geometryBufferRenderer) {
  111. // Clear previous transformation matrices dictionary used to compute objects velocities
  112. this._geometryBufferRenderer._previousTransformationMatrices = {};
  113. this._geometryBufferRenderer._previousBonesTransformationMatrices = {};
  114. this._geometryBufferRenderer.excludedSkinnedMeshesFromVelocity = [];
  115. }
  116. super.dispose(camera);
  117. }
  118. }