Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
"webgpu_postprocessing_dof",
"webgpu_postprocessing_fxaa",
"webgpu_postprocessing_sobel",
"webgpu_postprocessing_transition",
"webgpu_postprocessing",
"webgpu_procedural_texture",
"webgpu_reflection",
Expand Down
268 changes: 268 additions & 0 deletions examples/webgpu_postprocessing_transition.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - scenes transition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>

<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu scene transitions.<br/>
Original implementation by <a href="https://twitter.com/fernandojsg">fernandojsg</a> - <a href="https://github.com/kile/three.js-demos">github</a>
</div>

<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/tsl": "../build/three.webgpu.js",
"three/addons/": "./jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import TWEEN from 'three/addons/libs/tween.module.js';
import { uniform, transition, pass, texture } from 'three/tsl'

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused import texture.

let renderer, postProcessing, transitionController, transitionPass;

const textures = [];
const clock = new THREE.Clock();

const effectController = {
animateScene: true,
animateTransition: true,
transition: 0,
_transition: uniform( 0 ),
useTexture: true,
_useTexture: uniform( 1 ),
texture: 5,
cycle: true,
threshold: uniform(0.1),
};

function generateInstancedMesh( geometry, material, count ) {

const mesh = new THREE.InstancedMesh( geometry, material, count );

const dummy = new THREE.Object3D();
const color = new THREE.Color();

for ( let i = 0; i < count; i ++ ) {

dummy.position.x = Math.random() * 100 - 50;
dummy.position.y = Math.random() * 60 - 30;
dummy.position.z = Math.random() * 80 - 40;

dummy.rotation.x = Math.random() * 2 * Math.PI;
dummy.rotation.y = Math.random() * 2 * Math.PI;
dummy.rotation.z = Math.random() * 2 * Math.PI;

dummy.scale.x = Math.random() * 2 + 1;

if ( geometry.type === 'BoxGeometry' ) {

dummy.scale.y = Math.random() * 2 + 1;
dummy.scale.z = Math.random() * 2 + 1;

} else {

dummy.scale.y = dummy.scale.x;
dummy.scale.z = dummy.scale.x;

}

dummy.updateMatrix();

mesh.setMatrixAt( i, dummy.matrix );
mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );

}

return mesh;

}

function FXScene( geometry, rotationSpeed, backgroundColor ) {

const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 20;

// Setup scene
const scene = new THREE.Scene();
scene.background = new THREE.Color( backgroundColor );
scene.add( new THREE.AmbientLight( 0xaaaaaa, 3 ) );

const light = new THREE.DirectionalLight( 0xffffff, 3 );
light.position.set( 0, 1, 4 );
scene.add( light );

this.rotationSpeed = rotationSpeed;

const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
const material = new THREE.MeshPhongNodeMaterial( { color: color, flatShading: true } );
const mesh = generateInstancedMesh( geometry, material, 500 );
scene.add( mesh );

this.scene = scene;
this.camera = camera;
this.mesh = mesh;

this.update = function ( delta ) {

if ( effectController.animateScene ) {

mesh.rotation.x += this.rotationSpeed.x * delta;
mesh.rotation.y += this.rotationSpeed.y * delta;
mesh.rotation.z += this.rotationSpeed.z * delta;

}

};

this.resize = function () {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

};

}

const fxSceneA = new FXScene( new THREE.BoxGeometry( 2, 2, 2 ), new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
const fxSceneB = new FXScene( new THREE.IcosahedronGeometry( 1, 1 ), new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );

function init() {

// Initialize textures

const loader = new THREE.TextureLoader();

for ( let i = 0; i < 6; i ++ ) {

textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );;

}

renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );

postProcessing = new THREE.PostProcessing( renderer );

const scenePassA = pass( fxSceneA.scene, fxSceneA.camera );
const scenePassB = pass( fxSceneB.scene, fxSceneB.camera );

transitionPass = transition( scenePassA, scenePassB, new THREE.TextureNode(textures[ effectController.texture ]), effectController._transition, effectController.threshold, effectController._useTexture );

postProcessing.outputNode = transitionPass;

const gui = new GUI();

gui.add( effectController, 'animateScene' ).name( 'Animate Scene' );
gui.add( effectController, 'animateTransition' ).name( 'Animate Transition' );
transitionController = gui.add( effectController, 'transition', 0, 1, 0.01 ).name( 'transition' ).onChange(() => {

effectController._transition.value = effectController.transition;

});
gui.add( effectController, 'useTexture').onChange(() => {

const value = effectController.useTexture ? 1 : 0;

effectController._useTexture.value = value;

});
gui.add( effectController, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } );
gui.add( effectController, 'cycle' );
gui.add( effectController.threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );

}

window.addEventListener( 'resize', onWindowResize );

function onWindowResize() {3

Check warning

Code scanning / CodeQL

Expression has no effect

This expression has no effect.

fxSceneA.resize();
fxSceneB.resize();
renderer.setSize( window.innerWidth, window.innerHeight );

}

new TWEEN.Tween( effectController )
.to( { transition: 1 }, 1500 )
.onUpdate( function ( ) {


transitionController.setValue( effectController.transition )

// Change the current alpha texture after each transition
if ( effectController.cycle ) {

if ( effectController.transition == 0 || effectController.transition == 1 ) {

effectController.texture = ( effectController.texture + 1 ) % textures.length;
//erenderTransitionPass.setTexture( textures[ params.texture ] );

}

}

} )
.repeat( Infinity )
.delay( 2000 )
.yoyo( true )
.start();

function animate() {

if ( effectController.animateTransition ) TWEEN.update();

if ( textures[ effectController.texture ]) {

const mixTexture = textures[ effectController.texture ];
transitionPass.mixTextureNode.value = mixTexture;

}

const delta = clock.getDelta();
fxSceneA.update( delta );
fxSceneB.update( delta );

render();

}

function render() {

// Prevent render both scenes when it's not necessary
if ( effectController.transition === 0 ) {

renderer.render( fxSceneB.scene, fxSceneB.camera );

} else if ( effectController.transition === 1 ) {

renderer.render( fxSceneA.scene, fxSceneA.camera );

} else {

postProcessing.render();

}

}

init();

</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export { default as FilmNode, film } from './display/FilmNode.js';
export { default as Lut3DNode, lut3D } from './display/Lut3DNode.js';
export { default as GTAONode, ao } from './display/GTAONode.js';
export { default as FXAANode, fxaa } from './display/FXAANode.js';
export { default as TransitionNode, transition } from './display/TransitionNode.js';
export { default as RenderOutputNode, renderOutput } from './display/RenderOutputNode.js';

export { default as PassNode, pass, passTexture, depthPass } from './display/PassNode.js';
Expand Down
63 changes: 63 additions & 0 deletions src/nodes/display/TransitionNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import TempNode from '../core/TempNode.js';
import { uv } from '../accessors/UVNode.js';
import { addNodeElement, tslFn, nodeObject, float, If } from '../shadernode/ShaderNode.js';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused import If.
import { NodeUpdateType } from '../core/constants.js';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused import NodeUpdateType.
import { clamp, mix } from '../math/MathNode.js';

class TransitionNode extends TempNode {

constructor( textureNodeA, textureNodeB, mixTextureNode, mixRatioNode, thresholdNode, useTextureNode ) {

super();

// Input textures

this.textureNodeA = textureNodeA;
this.textureNodeB = textureNodeB;
this.mixTextureNode = mixTextureNode;

// Uniforms

this.mixRatioNode = mixRatioNode;
this.thresholdNode = thresholdNode;
this.useTextureNode = useTextureNode;

}

setup() {

const { textureNodeA, textureNodeB, mixTextureNode, mixRatioNode, thresholdNode, useTextureNode } = this;

const sampleTexture = ( textureNode ) => {

const uvNodeTexture = textureNode.uvNode || uv();
return textureNode.uv( uvNodeTexture );

};

const transition = tslFn( () => {

const texelOne = sampleTexture( textureNodeA );
const texelTwo = sampleTexture( textureNodeB );

const transitionTexel = sampleTexture( mixTextureNode );
const r = mixRatioNode.mul( thresholdNode.mul( 2.0 ).add( 1.0 ) ).sub( thresholdNode );
const mixf = clamp( transitionTexel.r.sub( r ).mul( float( 1.0 ).div( thresholdNode ) ), 0.0, 1.0 );

return mix( texelOne, texelTwo, useTextureNode.equal(1).cond( mixf, mixRatioNode ) );

} );

const outputNode = transition();

return outputNode;

}

}

export const transition = ( nodeA, nodeB, mixTexture, mixRatio, threshold, useTexture ) => nodeObject( new TransitionNode( nodeObject( nodeA ).toTexture(), nodeObject( nodeB ).toTexture(), nodeObject( mixTexture ).toTexture(), nodeObject( mixRatio ), nodeObject( threshold ), nodeObject( useTexture ) ) );

addNodeElement( 'transition', transition );

export default TransitionNode;