Skip to content

Commit

Permalink
Merge pull request #13200 from Popov72/fix-motionblur
Browse files Browse the repository at this point in the history
Motion blur: Fix motion blur when not in the "object based" mode
  • Loading branch information
sebavan committed Nov 4, 2022
2 parents 0eb224c + 01e2365 commit 8b3fe48
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 9 additions & 6 deletions packages/dev/core/src/PostProcesses/motionBlurPostProcess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Nullable } from "../types";
import { Logger } from "../Misc/logger";
import { Matrix, Vector2 } from "../Maths/math.vector";
import { Matrix, TmpVectors, Vector2 } from "../Maths/math.vector";
import type { Camera } from "../Cameras/camera";
import type { Effect } from "../Materials/effect";
import type { PostProcessOptions } from "./postProcess";
Expand Down Expand Up @@ -135,8 +135,8 @@ export class MotionBlurPostProcess extends PostProcess {
super(
name,
"motionBlur",
["motionStrength", "motionScale", "screenSize", "inverseViewProjection", "prevViewProjection"],
["velocitySampler", "textureSampler", "depthSampler"],
["motionStrength", "motionScale", "screenSize", "inverseViewProjection", "prevViewProjection", "projection"],
["velocitySampler", "depthSampler"],
options,
camera,
samplingMode,
Expand Down Expand Up @@ -250,7 +250,7 @@ export class MotionBlurPostProcess extends PostProcess {
this.onApply = (effect: Effect) => this._onApplyObjectBased(effect);
} else {
this._invViewProjection = Matrix.Identity();
this._previousViewProjection = Matrix.Identity();
this._previousViewProjection = this._scene.getTransformMatrix().clone();

if (this._prePassRenderer && this._prePassEffectConfiguration) {
this._prePassEffectConfiguration.texturesRequired[0] = Constants.PREPASS_DEPTH_TEXTURE_TYPE;
Expand Down Expand Up @@ -284,13 +284,16 @@ export class MotionBlurPostProcess extends PostProcess {
* @param effect
*/
private _onApplyScreenBased(effect: Effect): void {
const viewProjection = this._scene.getProjectionMatrix().multiply(this._scene.getViewMatrix());
const viewProjection = TmpVectors.Matrix[0];
viewProjection.copyFrom(this._scene.getTransformMatrix());

viewProjection.invertToRef(this._invViewProjection!);
effect.setMatrix("inverseViewProjection", this._invViewProjection!);

effect.setMatrix("prevViewProjection", this._previousViewProjection!);
this._previousViewProjection = viewProjection;
this._previousViewProjection!.copyFrom(viewProjection);

effect.setMatrix("projection", this._scene.getProjectionMatrix());

effect.setVector2("screenSize", new Vector2(this.width, this.height));

Expand Down
9 changes: 6 additions & 3 deletions packages/dev/core/src/Shaders/motionBlur.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform sampler2D depthSampler;

uniform mat4 inverseViewProjection;
uniform mat4 prevViewProjection;
uniform mat4 projection;
#endif


Expand Down Expand Up @@ -49,12 +50,14 @@ void main(void)
#else
vec2 texelSize = 1.0 / screenSize;
float depth = texture2D(depthSampler, vUV).r;
depth = projection[2].z + projection[3].z / depth; // convert from view linear z to NDC z

vec4 cpos = vec4(vUV * 2.0 - 1.0, depth, 1.0);
cpos = cpos * inverseViewProjection;
cpos = inverseViewProjection * cpos;
cpos /= cpos.w;

vec4 ppos = cpos * prevViewProjection;
ppos.xyz /= ppos.w;
vec4 ppos = prevViewProjection * cpos;
ppos /= ppos.w;
ppos.xy = ppos.xy * 0.5 + 0.5;

vec2 velocity = (ppos.xy - vUV) * motionScale * motionStrength;
Expand Down

0 comments on commit 8b3fe48

Please sign in to comment.