motionBlurPostProcess.ts 7.8 KB

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