|
|
@@ -0,0 +1,201 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js webgpu - radial blur</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="example.css">
|
|
|
+ </head>
|
|
|
+
|
|
|
+ <body>
|
|
|
+
|
|
|
+ <div id="info">
|
|
|
+ <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
|
|
|
+
|
|
|
+ <div class="title-wrapper">
|
|
|
+ <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Radial Blur</span>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <small>Radial Blur for faked light shafts.</small>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <script type="importmap">
|
|
|
+ {
|
|
|
+ "imports": {
|
|
|
+ "three": "../build/three.webgpu.js",
|
|
|
+ "three/webgpu": "../build/three.webgpu.js",
|
|
|
+ "three/tsl": "../build/three.tsl.js",
|
|
|
+ "three/addons/": "./jsm/"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+
|
|
|
+ <script type="module">
|
|
|
+
|
|
|
+ import * as THREE from 'three/webgpu';
|
|
|
+ import { float, int, pass, uniform } from 'three/tsl';
|
|
|
+ import { radialBlur } from 'three/addons/tsl/display/radialBlur.js';
|
|
|
+
|
|
|
+ import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ enabled: true,
|
|
|
+ animated: true
|
|
|
+ };
|
|
|
+
|
|
|
+ let camera, scene, renderer, timer, group;
|
|
|
+ let postProcessing;
|
|
|
+
|
|
|
+ init();
|
|
|
+
|
|
|
+ async function init() {
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
|
|
|
+ camera.position.z = 50;
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+ scene.background = new THREE.Color( 0x000000 );
|
|
|
+
|
|
|
+ timer = new THREE.Timer();
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
|
|
|
+ hemiLight.position.set( 0, 1000, 0 );
|
|
|
+ scene.add( hemiLight );
|
|
|
+
|
|
|
+ const pointLight = new THREE.PointLight( 0xffffff, 1000 );
|
|
|
+ pointLight.position.set( 0, 0, 0 );
|
|
|
+ scene.add( pointLight );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ group = new THREE.Group();
|
|
|
+
|
|
|
+ const geometry = new THREE.TetrahedronGeometry();
|
|
|
+ const material = new THREE.MeshStandardMaterial( { flatShading: true } );
|
|
|
+
|
|
|
+ const mesh = new THREE.InstancedMesh( geometry, material, 100 );
|
|
|
+ const dummy = new THREE.Object3D();
|
|
|
+ const col = new THREE.Color();
|
|
|
+ const center = new THREE.Vector2();
|
|
|
+
|
|
|
+ for ( let i = 0; i < mesh.count; i ++ ) {
|
|
|
+
|
|
|
+ dummy.position.x = Math.random() * 50 - 25;
|
|
|
+ dummy.position.y = Math.random() * 50 - 25;
|
|
|
+ dummy.position.z = Math.random() * 50 - 25;
|
|
|
+
|
|
|
+ center.set( dummy.position.x, dummy.position.y );
|
|
|
+
|
|
|
+ // make sure tetrahedrons are not positioned at the origin
|
|
|
+
|
|
|
+ if ( center.length() < 6 ) {
|
|
|
+
|
|
|
+ center.normalize().multiplyScalar( 6 );
|
|
|
+ dummy.position.x = center.x;
|
|
|
+ dummy.position.y = center.y;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ dummy.scale.setScalar( Math.random() * 2 + 1 );
|
|
|
+
|
|
|
+ dummy.rotation.x = Math.random() * Math.PI;
|
|
|
+ dummy.rotation.y = Math.random() * Math.PI;
|
|
|
+ dummy.rotation.z = Math.random() * Math.PI;
|
|
|
+
|
|
|
+ dummy.updateMatrix();
|
|
|
+ mesh.setMatrixAt( i, dummy.matrix );
|
|
|
+
|
|
|
+ col.setHSL( 0.55 + ( i / mesh.count ) * 0.15, 1, 0.2 );
|
|
|
+
|
|
|
+ mesh.setColorAt( i, col );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ group.add( mesh );
|
|
|
+ scene.add( group );
|
|
|
+
|
|
|
+ renderer = new THREE.WebGPURenderer();
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ renderer.setAnimationLoop( animate );
|
|
|
+ renderer.inspector = new Inspector();
|
|
|
+ renderer.toneMapping = THREE.NeutralToneMapping;
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ // post processing
|
|
|
+
|
|
|
+ postProcessing = new THREE.PostProcessing( renderer );
|
|
|
+
|
|
|
+ const scenePass = pass( scene, camera );
|
|
|
+
|
|
|
+ const weightUniform = uniform( float( 0.9 ) );
|
|
|
+ const decayUniform = uniform( float( 0.95 ) );
|
|
|
+ const exposureUniform = uniform( int( 5 ) );
|
|
|
+ const countUniform = uniform( int( 32 ) );
|
|
|
+
|
|
|
+ const blurPass = radialBlur( scenePass, { weight: weightUniform, decay: decayUniform, count: countUniform, exposure: exposureUniform } );
|
|
|
+
|
|
|
+ postProcessing.outputNode = blurPass;
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ const gui = renderer.inspector.createParameters( 'Settings' );
|
|
|
+ gui.add( weightUniform, 'value', 0, 1 ).name( 'weight' );
|
|
|
+ gui.add( decayUniform, 'value', 0, 1 ).name( 'decay' );
|
|
|
+ gui.add( countUniform, 'value', 16, 64, 1 ).name( 'sample count' );
|
|
|
+ gui.add( exposureUniform, 'value', 1, 10 ).name( 'exposure' );
|
|
|
+ gui.add( params, 'enabled' ).onChange( ( value ) => {
|
|
|
+
|
|
|
+ if ( value === true ) {
|
|
|
+
|
|
|
+ postProcessing.outputNode = blurPass;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ postProcessing.outputNode = scenePass;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ postProcessing.needsUpdate = true;
|
|
|
+
|
|
|
+ } );
|
|
|
+ gui.add( params, 'animated' );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ timer.update();
|
|
|
+ const delta = timer.getDelta();
|
|
|
+
|
|
|
+ if ( params.animated === true ) {
|
|
|
+
|
|
|
+ group.rotation.y += delta * 0.1;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ postProcessing.render();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+
|
|
|
+ </body>
|
|
|
+</html>
|