123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <html lang="en">
- <head>
- <title>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">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute<br/>(Chrome Canary with flag: --enable-unsafe-webgpu)
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
- import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
- import WebGPUStorageBuffer from './jsm/renderers/webgpu/WebGPUStorageBuffer.js';
- import WebGPUUniformsGroup from './jsm/renderers/webgpu/WebGPUUniformsGroup.js';
- import { Vector2Uniform } from './jsm/renderers/webgpu/WebGPUUniform.js';
- import PositionNode from './jsm/renderers/nodes/accessors/PositionNode.js';
- import ColorNode from './jsm/renderers/nodes/inputs/ColorNode.js';
- import OperatorNode from './jsm/renderers/nodes/math/OperatorNode.js';
- let camera, scene, renderer;
- let pointer;
- const computeParams = [];
- init().then( animate ).catch( error );
- async function init() {
- if ( WebGPU.isAvailable() === false ) {
- document.body.appendChild( WebGPU.getErrorMessage() );
- throw '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();
- scene.background = new THREE.Color( 0x000000 );
- const particleNum = 100000;
- const particleSize = 3;
- const particleArray = new Float32Array( particleNum * particleSize );
- const velocityArray = new Float32Array( particleNum * particleSize );
- for ( let i = 0; i < particleArray.length; i += 3 ) {
- const r = Math.random() * 0.01 + 0.0005;
- const degree = Math.random() * 360;
- velocityArray[ i + 0 ] = r * Math.sin( degree * Math.PI / 180 );
- velocityArray[ i + 1 ] = r * Math.cos( degree * Math.PI / 180 );
- }
- const particleBuffer = new WebGPUStorageBuffer( 'particle', new THREE.BufferAttribute( particleArray, 3 ) );
- const velocityBuffer = new WebGPUStorageBuffer( 'velocity', new THREE.BufferAttribute( velocityArray, 3 ) );
- pointer = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
- const pointerGroup = new WebGPUUniformsGroup( 'mouseUniforms' ).addUniform(
- new Vector2Uniform( 'pointer', pointer )
- );
- const computeBindings = [
- particleBuffer,
- velocityBuffer,
- pointerGroup
- ];
- const computeShader = /* glsl */`#version 450
- #define PARTICLE_NUM ${particleNum}
- #define PARTICLE_SIZE ${particleSize}
- #define ROOM_SIZE 1.0
- #define POINTER_SIZE 0.1
- // Limitation for now: the order should be the same as bindings order
- layout(set = 0, binding = 0) buffer Particle {
- float particle[ PARTICLE_NUM * PARTICLE_SIZE ];
- } particle;
- layout(set = 0, binding = 1) buffer Velocity {
- float velocity[ PARTICLE_NUM * PARTICLE_SIZE ];
- } velocity;
- layout(set = 0, binding = 2) uniform MouseUniforms {
- vec2 pointer;
- } mouseUniforms;
- void main() {
- uint index = gl_GlobalInvocationID.x;
- if ( index >= PARTICLE_NUM ) { return; }
- vec3 position = vec3(
- particle.particle[ index * 3 + 0 ] + velocity.velocity[ index * 3 + 0 ],
- particle.particle[ index * 3 + 1 ] + velocity.velocity[ index * 3 + 1 ],
- particle.particle[ index * 3 + 2 ] + velocity.velocity[ index * 3 + 2 ]
- );
- if ( abs( position.x ) >= ROOM_SIZE ) {
- velocity.velocity[ index * 3 + 0 ] = - velocity.velocity[ index * 3 + 0 ];
- }
- if ( abs( position.y ) >= ROOM_SIZE ) {
- velocity.velocity[ index * 3 + 1 ] = - velocity.velocity[ index * 3 + 1 ];
- }
- if ( abs( position.z ) >= ROOM_SIZE ) {
- velocity.velocity[ index * 3 + 2 ] = - velocity.velocity[ index * 3 + 2 ];
- }
- float dx = mouseUniforms.pointer.x - position.x;
- float dy = mouseUniforms.pointer.y - position.y;
- float distanceFromPointer = sqrt( dx * dx + dy * dy );
- if ( distanceFromPointer <= POINTER_SIZE ) {
- position.x = 0.0;
- position.y = 0.0;
- position.z = 0.0;
- }
- particle.particle[ index * 3 + 0 ] = position.x;
- particle.particle[ index * 3 + 1 ] = position.y;
- particle.particle[ index * 3 + 2 ] = position.z;
- }
- `;
- computeParams.push( {
- num: particleNum,
- shader: computeShader,
- bindings: computeBindings
- } );
- // Use a compute shader to animate the point cloud's vertex data.
- const pointsGeometry = new THREE.BufferGeometry().setAttribute(
- 'position', particleBuffer.attribute
- );
- const pointsMaterial = new THREE.PointsMaterial();
- pointsMaterial.colorNode = new OperatorNode( '+', new PositionNode(), new ColorNode( new THREE.Color( 0x0000FF ) ) );
- const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
- 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 );
- 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;
- pointer.set(
- ( x / width - 0.5 ) * 2.0,
- ( - y / height + 0.5 ) * 2.0
- );
- }
- function animate() {
- requestAnimationFrame( animate );
- renderer.compute( computeParams );
- renderer.render( scene, camera );
- }
- function error( error ) {
- console.error( error );
- }
- </script>
- </body>
- </html>
|