motionBlurPostProcess.ts 9.3 KB

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