Motion Blur Post Process
Introduction
You can find an example of the motion blur post-process in our playground: Motion Blur Post Process Example
Creating the motion blur post-process
You only need to create an instance of BABYLON.MotionBlurPostProcess.
import { MotionBlurPostProcess } from "@babylonjs/core/PostProcesses/motionBlurPostProcess";var motionblur = new MotionBlurPostProcess("mb", // The name of the effect.scene, // The scene containing the objects to blur according to their velocity.1.0, // The required width/height ratio to downsize to before computing the render pass.camera // The camera to apply the render pass to.);The blur is based on object velocities. The faster an object's transformation changes, the stronger the blur on that object. Velocity is affected by each object's position, rotation, and scale:
Customizing
By default, the post-process will blur the scene using a coefficient named motionStrength. Its default value is equal to 1 and can be customized:
motionblur.motionStrength = 2; // double the blur effectExample: Customizing Motion Blur
For performance/quality reasons, you can also customize the blur quality. To blur an image, the effect takes some samples around the current pixel. The more samples you take, the higher the blur quality. You can customize the number of samples using the motionBlurSamples property. Its default value is 32:
motionblur.motionBlurSamples = 16; // divide quality by 2Optimizing your application
By default, the post-process will blur all objects that generate a velocity (position, rotation, and scale). This also includes skinned meshes animated by their bones.
Sometimes, complex skinned meshes can have too many bones and can cause a drop in frame rate. You can choose to exclude a skinned mesh from bone computation while rendering the velocity map and apply blur only to its position/rotation/scale variation. The MotionBlurPostProcess provides a helper to add and remove skinned meshes:
// Now, the mesh "mySkinnedMesh" will not compute bones velocities and will save performances.motionblur.excludeSkinnedMesh(mySkinnedMesh);// Previously excluded, the mesh "mySkinnedMesh" will now compute bones velocities for a better render.motionblur.removeExcludedSkinnedMesh(mySkinnedMesh);Limitations
To improve performance, the motion blur velocity map is rendered at the same time as the depth and normal buffers using the geometry render buffer. The clear color of the render buffer collides with the needed clear color of the velocity map and can generate glitches like this: Limitations In The Motion Blur Post Process. As a limitation, your scene must occur in a closed environment OR have at least a skybox to hide the empty space that causes these glitches.
The Motion Blur post-process requires at least WebGL 2 support, or WebGL 1 support with multiple render targets. If neither is available, the post-process will work as a passthrough.
Disabling Object-Based Motion Blur
By default, the motion blur post-process uses object-based velocity to calculate blur. In large scenes, calculating the velocity of every visible object can be costly. Object-based velocity can be disabled to fall back to screen-based mode. Screen-based mode avoids calculating object velocities by basing the velocity only on the camera's movement.
To enable screen-based mode, simply disable object-based mode on the post-process:
// Disable object-based mode in order to enable screen-based mode.motionblur.isObjectBased = false;