123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <html lang="en">
- <head>
- <title>three.js - WebGPU - Compute</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">
- <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
- <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute
- </div>
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three-nodes/": "./jsm/nodes/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import * as Nodes from 'three-nodes/Nodes.js';
- import {
- ShaderNode, compute, context,
- uniform, element, storage, attribute,
- temp, assign, add, sub, cond, abs, negate, max, min, length, vec3, color,
- greaterThanEqual, lessThanEqual, instanceIndex
- } from 'three-nodes/Nodes.js';
- import { GUI } from './jsm/libs/lil-gui.module.min.js';
- import WebGPU from './jsm/capabilities/WebGPU.js';
- import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
- let camera, scene, renderer;
- let computeNode;
- const pointerVector = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
- const scaleVector = new THREE.Vector2( 1, 1 );
- init().then( animate ).catch( error );
- async function init() {
- if ( WebGPU.isAvailable() === false ) {
- document.body.appendChild( WebGPU.getErrorMessage() );
- throw new Error( 'No WebGPU support' );
- }
- camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
- camera.position.z = 1;
- scene = new THREE.Scene();
- // initialize particles
- const particleNum = 300000;
- const particleSize = 2; // vec2
- const particleArray = new Float32Array( particleNum * particleSize );
- const velocityArray = new Float32Array( particleNum * particleSize );
- for ( let i = 0; i < particleNum; i ++ ) {
- const r = Math.random() * 0.01 + 0.005;
- const degree = Math.random() * 360;
- velocityArray[ i * particleSize + 0 ] = r * Math.sin( degree * Math.PI / 180 ); // x
- velocityArray[ i * particleSize + 1 ] = r * Math.cos( degree * Math.PI / 180 ); // y
- }
- // create buffers
- const particleBuffer = new THREE.InstancedBufferAttribute( particleArray );
- const velocityBuffer = new THREE.InstancedBufferAttribute( velocityArray );
- const particleBufferNode = storage( particleBuffer, 'vec2', particleNum );
- const velocityBufferNode = storage( velocityBuffer, 'vec2', particleNum );
- // create function
- const FnNode = new ShaderNode( ( inputs, builder ) => {
- const particle = element( particleBufferNode, instanceIndex );
- const velocity = element( velocityBufferNode, instanceIndex );
- const pointer = uniform( pointerVector );
- const limit = uniform( scaleVector );
- const position = temp( context( add( particle, velocity ), { temp: false } ) );
- position.build( builder );
- assign( velocity.x, cond( greaterThanEqual( abs( position.x ), limit.x ), negate( velocity.x ), velocity.x ) ).build( builder );
- assign( velocity.y, cond( greaterThanEqual( abs( position.y ), limit.y ), negate( velocity.y ), velocity.y ) ).build( builder );
- assign( position, max( negate( limit ), min( limit, position ) ) ).build( builder );
- const pointerSize = 0.1;
- const distanceFromPointer = length( sub( pointer, position ) );
- assign( particle, cond( lessThanEqual( distanceFromPointer, pointerSize ), vec3(), position ) ).build( builder );
- } );
- // compute
- computeNode = compute( FnNode, particleNum );
- // use a compute shader to animate the point cloud's vertex data.
- const particleNode = attribute( 'particle', 'vec2' );
- const pointsGeometry = new THREE.BufferGeometry();
- pointsGeometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 3 ) ) ); // single vertex ( not triangle )
- pointsGeometry.setAttribute( 'particle', particleBuffer ); // dummy the position points as instances
- pointsGeometry.drawRange.count = 1; // force render points as instances ( not triangle )
- const pointsMaterial = new Nodes.PointsNodeMaterial();
- pointsMaterial.colorNode = add( particleNode, color( 0xFFFFFF ) );
- pointsMaterial.positionNode = particleNode;
- const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
- mesh.isInstancedMesh = true;
- mesh.count = particleNum;
- scene.add( mesh );
- renderer = new WebGPURenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize );
- window.addEventListener( 'mousemove', onMouseMove );
- // gui
- const gui = new GUI();
- gui.add( scaleVector, 'x', 0, 1, 0.01 );
- gui.add( scaleVector, 'y', 0, 1, 0.01 );
- return renderer.init();
- }
- function onWindowResize() {
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onMouseMove( event ) {
- const x = event.clientX;
- const y = event.clientY;
- const width = window.innerWidth;
- const height = window.innerHeight;
- pointerVector.set(
- ( x / width - 0.5 ) * 2.0,
- ( - y / height + 0.5 ) * 2.0
- );
- }
- function animate() {
- requestAnimationFrame( animate );
- renderer.compute( computeNode );
- renderer.render( scene, camera );
- }
- function error( error ) {
- console.error( error );
- }
- </script>
- </body>
- </html>
|