123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <html lang="en">
- <head>
- <title>three.js - WebGPU - Compute Particles</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 - Compute - 1M Particles
- <div id="timestamps" style="
- position: absolute;
- top: 60px;
- left: 0;
- padding: 10px;
- background: rgba( 0, 0, 0, 0.5 );
- color: #fff;
- font-family: monospace;
- font-size: 12px;
- line-height: 1.5;
- pointer-events: none;
- text-align: left;
- "></div>
- </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 { Fn, uniform, texture, instanceIndex, float, hash, vec3, storage, If } from 'three/tsl';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import Stats from 'three/addons/libs/stats.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- const particleCount = 1000000;
- const gravity = uniform( - .0098 );
- const bounce = uniform( .8 );
- const friction = uniform( .99 );
- const size = uniform( .12 );
- const clickPosition = uniform( new THREE.Vector3() );
- let camera, scene, renderer;
- let controls, stats;
- let computeParticles;
- const timestamps = document.getElementById( 'timestamps' );
- init();
- function init() {
- const { innerWidth, innerHeight } = window;
- camera = new THREE.PerspectiveCamera( 50, innerWidth / innerHeight, .1, 1000 );
- camera.position.set( 15, 30, 15 );
- scene = new THREE.Scene();
- // textures
- const textureLoader = new THREE.TextureLoader();
- const map = textureLoader.load( 'textures/sprite1.png' );
- //
- const createBuffer = () => storage( new THREE.StorageInstancedBufferAttribute( particleCount, 3 ), 'vec3', particleCount );
- const positionBuffer = createBuffer();
- const velocityBuffer = createBuffer();
- const colorBuffer = createBuffer();
- // compute
- const computeInit = Fn( () => {
- const position = positionBuffer.element( instanceIndex );
- const color = colorBuffer.element( instanceIndex );
- const randX = hash( instanceIndex );
- const randY = hash( instanceIndex.add( 2 ) );
- const randZ = hash( instanceIndex.add( 3 ) );
- position.x = randX.mul( 100 ).add( - 50 );
- position.y = 0; // randY.mul( 10 );
- position.z = randZ.mul( 100 ).add( - 50 );
- color.assign( vec3( randX, randY, randZ ) );
- } )().compute( particleCount );
- //
- const computeUpdate = Fn( () => {
- const position = positionBuffer.element( instanceIndex );
- const velocity = velocityBuffer.element( instanceIndex );
- velocity.addAssign( vec3( 0.00, gravity, 0.00 ) );
- position.addAssign( velocity );
- velocity.mulAssign( friction );
- // floor
- If( position.y.lessThan( 0 ), () => {
- position.y = 0;
- velocity.y = velocity.y.negate().mul( bounce );
- // floor friction
- velocity.x = velocity.x.mul( .9 );
- velocity.z = velocity.z.mul( .9 );
- } );
- } );
- computeParticles = computeUpdate().compute( particleCount );
- // create nodes
- const textureNode = texture( map );
- // create particles
- const particleMaterial = new THREE.SpriteNodeMaterial();
- particleMaterial.colorNode = textureNode.mul( colorBuffer.element( instanceIndex ) );
- particleMaterial.positionNode = positionBuffer.toAttribute();
- particleMaterial.scaleNode = size;
- particleMaterial.depthWrite = false;
- particleMaterial.depthTest = true;
- particleMaterial.transparent = true;
- const particles = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), particleMaterial );
- particles.count = particleCount;
- particles.frustumCulled = false;
- scene.add( particles );
- //
- const helper = new THREE.GridHelper( 60, 40, 0x303030, 0x303030 );
- scene.add( helper );
- const geometry = new THREE.PlaneGeometry( 1000, 1000 );
- geometry.rotateX( - Math.PI / 2 );
- const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
- scene.add( plane );
- const raycaster = new THREE.Raycaster();
- const pointer = new THREE.Vector2();
- //
- renderer = new THREE.WebGPURenderer( { antialias: true, trackTimestamp: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- renderer.compute( computeInit );
- // click event
- const computeHit = Fn( () => {
- const position = positionBuffer.element( instanceIndex );
- const velocity = velocityBuffer.element( instanceIndex );
- const dist = position.distance( clickPosition );
- const direction = position.sub( clickPosition ).normalize();
- const distArea = float( 6 ).sub( dist ).max( 0 );
- const power = distArea.mul( .01 );
- const relativePower = power.mul( hash( instanceIndex ).mul( .5 ).add( .5 ) );
- velocity.assign( velocity.add( direction.mul( relativePower ) ) );
- } )().compute( particleCount );
- //
- function onMove( event ) {
- pointer.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
- raycaster.setFromCamera( pointer, camera );
- const intersects = raycaster.intersectObjects( [ plane ], false );
- if ( intersects.length > 0 ) {
- const { point } = intersects[ 0 ];
- // move to uniform
- clickPosition.value.copy( point );
- clickPosition.value.y = - 1;
- // compute
- renderer.compute( computeHit );
- }
- }
- // events
- renderer.domElement.addEventListener( 'pointermove', onMove );
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 5;
- controls.maxDistance = 200;
- controls.target.set( 0, 0, 0 );
- controls.update();
- //
- window.addEventListener( 'resize', onWindowResize );
- // gui
- const gui = new GUI();
- gui.add( gravity, 'value', - .0098, 0, 0.0001 ).name( 'gravity' );
- gui.add( bounce, 'value', .1, 1, 0.01 ).name( 'bounce' );
- gui.add( friction, 'value', .96, .99, 0.01 ).name( 'friction' );
- gui.add( size, 'value', .12, .5, 0.01 ).name( 'size' );
- }
- function onWindowResize() {
- const { innerWidth, innerHeight } = window;
- camera.aspect = innerWidth / innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( innerWidth, innerHeight );
- }
- async function animate() {
- stats.update();
- await renderer.computeAsync( computeParticles );
- await renderer.renderAsync( scene, camera );
- // throttle the logging
- if ( renderer.hasFeature( 'timestamp-query' ) ) {
- if ( renderer.info.render.calls % 5 === 0 ) {
- timestamps.innerHTML = `
- Compute ${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>
- Draw ${renderer.info.render.drawCalls} pass in ${renderer.info.render.timestamp.toFixed( 6 )}ms`;
- }
- } else {
- timestamps.innerHTML = 'Timestamp queries not supported';
- }
- }
- </script>
- </body>
- </html>
|