Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes: Add VelocityNode and MotionBlurNode. #29058

Merged
merged 25 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/screenshots/webgpu_mrt.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 12 additions & 9 deletions examples/webgpu_mrt.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - mrt<br />
Final / Beauty / Normal / Emissive / Diffuse
Final / Beauty / Normal / Velocity / Diffuse
</div>

<script type="importmap">
Expand All @@ -26,14 +26,14 @@
<script type="module">

import * as THREE from 'three';
import { output, transformedNormalWorld, pass, step, diffuseColor, emissive, viewportTopLeft, mix, mrt, tslFn } from 'three/tsl';
import { output, transformedNormalWorld, pass, step, diffuseColor, velocity, viewportTopLeft, mix, mrt, tslFn } from 'three/tsl';

import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

let camera, scene, renderer;
let camera, scene, renderer, controls;
let postProcessing;

init();
Expand Down Expand Up @@ -86,16 +86,16 @@
output: output,
normal: transformedNormalWorld.directionToColor(),
diffuse: diffuseColor,
emissive: emissive
velocity: velocity
} ) );

// optimize textures

const normalTexture = scenePass.getTexture( 'normal' );
const diffuseTexture = scenePass.getTexture( 'diffuse' );
const emissiveTexture = scenePass.getTexture( 'emissive' );
const velocityTexture = scenePass.getTexture( 'velocity' );

normalTexture.type = diffuseTexture.type = emissiveTexture.type = THREE.UnsignedByteType;
normalTexture.type = diffuseTexture.type = velocityTexture.type = THREE.UnsignedByteType;

// post processing - mrt

Expand All @@ -106,11 +106,11 @@
const output = scenePass.getTextureNode( 'output' ); // output name is optional here
const normal = scenePass.getTextureNode( 'normal' );
const diffuse = scenePass.getTextureNode( 'diffuse' );
const emissive = scenePass.getTextureNode( 'emissive' );
const velocity = scenePass.getTextureNode( 'velocity' );

const out = mix( output.renderOutput(), output, step( 0.2, viewportTopLeft.x ) );
const nor = mix( out, normal, step( 0.4, viewportTopLeft.x ) );
const emi = mix( nor, emissive, step( 0.6, viewportTopLeft.x ) );
const emi = mix( nor, velocity, step( 0.6, viewportTopLeft.x ) );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved
const dif = mix( emi, diffuse, step( 0.8, viewportTopLeft.x ) );

return dif;
Expand All @@ -119,10 +119,11 @@

// controls

const controls = new OrbitControls( camera, renderer.domElement );
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
controls.maxDistance = 10;
controls.target.set( 0, 0, - 0.2 );
controls.enableDamping = true;
controls.update();

window.addEventListener( 'resize', onWindowResize );
Expand All @@ -142,6 +143,8 @@

function render() {

controls.update();

postProcessing.render();

}
Expand Down
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export { default as StorageTextureNode, storageTexture, textureStore } from './a
export { default as Texture3DNode, texture3D } from './accessors/Texture3DNode.js';
export * from './accessors/UVNode.js';
export { default as UserDataNode, userData } from './accessors/UserDataNode.js';
export { default as VelocityNode, velocity } from './accessors/VelocityNode.js';

// display
export { default as BlendModeNode, burn, dodge, overlay, screen } from './display/BlendModeNode.js';
Expand Down
104 changes: 104 additions & 0 deletions src/nodes/accessors/VelocityNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { addNodeClass } from '../core/Node.js';
import TempNode from '../core/TempNode.js';
import { modelWorldMatrix } from './ModelNode.js';
import { positionLocal } from './PositionNode.js';
import { nodeImmutable } from '../shadernode/ShaderNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { Matrix4 } from '../../math/Matrix4.js';
import { uniform } from '../core/UniformNode.js';
import { sub } from '../math/OperatorNode.js';

const _worldMatrixCache = new WeakMap();

class VelocityNode extends TempNode {

constructor() {

super( 'vec2' );

this.updateType = NodeUpdateType.OBJECT;
this.updateAfterType = NodeUpdateType.OBJECT;

this.previousProjectionViewMatrix = uniform( new Matrix4() );
this.currentProjectionViewMatrix = uniform( new Matrix4() );
this.previousModelMatrix = uniform( new Matrix4() );

this.firstFrame = true;

}

update( frame ) {

const camera = frame.camera;
const object = frame.object;

if ( this.firstFrame ) {

this.firstFrame = false;

this.currentProjectionViewMatrix.value.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
this.previousProjectionViewMatrix.value.copy( this.currentProjectionViewMatrix.value );

this.previousModelMatrix.value.copy( object.matrixWorld );

} else {

this.previousProjectionViewMatrix.value.copy( this.currentProjectionViewMatrix.value );
this.currentProjectionViewMatrix.value.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );

const previousModelMatrix = getPreviousModelMatrix( object );
this.previousModelMatrix.value.copy( previousModelMatrix );

}

}

updateAfter( frame ) {

const object = frame.object;

const previousModelMatrix = getPreviousModelMatrix( object );

previousModelMatrix.copy( object.matrixWorld );

}

setup( /*builder*/ ) {

this.firstFrame = true;

const clipPositionCurrent = this.currentProjectionViewMatrix.mul( modelWorldMatrix ).mul( positionLocal ).toVar();
const clipPositionPrevious = this.previousProjectionViewMatrix.mul( this.previousModelMatrix ).mul( positionLocal ).toVar();

const ndcPositionCurrent = clipPositionCurrent.xy.div( clipPositionCurrent.w );
const ndcPositionPrevious = clipPositionPrevious.xy.div( clipPositionPrevious.w );

let velocity = sub( ndcPositionCurrent, ndcPositionPrevious ).mul( 0.5 );
velocity = velocity.mul( 0.5 ).add( 0.5 );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

return velocity;

}

}

function getPreviousModelMatrix( object ) {

let previousModelMatrix = _worldMatrixCache.get( object );

if ( previousModelMatrix === undefined ) {

previousModelMatrix = new Matrix4();
_worldMatrixCache.set( object, previousModelMatrix );

}

return previousModelMatrix;

}

export default VelocityNode;

export const velocity = nodeImmutable( VelocityNode );

addNodeClass( 'VelocityNode', VelocityNode );