webgpu_compute.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <html lang="en">
  2. <head>
  3. <title>WebGPU Compute</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute<br/>(Chrome Canary with flag: --enable-unsafe-webgpu)
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  22. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  23. import WebGPUStorageBuffer from './jsm/renderers/webgpu/WebGPUStorageBuffer.js';
  24. import WebGPUUniformsGroup from './jsm/renderers/webgpu/WebGPUUniformsGroup.js';
  25. import { Vector2Uniform } from './jsm/renderers/webgpu/WebGPUUniform.js';
  26. import PositionNode from './jsm/renderers/nodes/accessors/PositionNode.js';
  27. import ColorNode from './jsm/renderers/nodes/inputs/ColorNode.js';
  28. import OperatorNode from './jsm/renderers/nodes/math/OperatorNode.js';
  29. let camera, scene, renderer;
  30. let pointer;
  31. const computeParams = [];
  32. init().then( animate ).catch( error );
  33. async function init() {
  34. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw 'No WebGPU support';
  37. }
  38. camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
  39. camera.position.z = 1;
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x000000 );
  42. const particleNum = 100000;
  43. const particleSize = 3;
  44. const particleArray = new Float32Array( particleNum * particleSize );
  45. const velocityArray = new Float32Array( particleNum * particleSize );
  46. for ( let i = 0; i < particleArray.length; i += 3 ) {
  47. const r = Math.random() * 0.01 + 0.0005;
  48. const degree = Math.random() * 360;
  49. velocityArray[ i + 0 ] = r * Math.sin( degree * Math.PI / 180 );
  50. velocityArray[ i + 1 ] = r * Math.cos( degree * Math.PI / 180 );
  51. }
  52. const particleBuffer = new WebGPUStorageBuffer( 'particle', new THREE.BufferAttribute( particleArray, 3 ) );
  53. const velocityBuffer = new WebGPUStorageBuffer( 'velocity', new THREE.BufferAttribute( velocityArray, 3 ) );
  54. pointer = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
  55. const pointerGroup = new WebGPUUniformsGroup( 'mouseUniforms' ).addUniform(
  56. new Vector2Uniform( 'pointer', pointer )
  57. );
  58. const computeBindings = [
  59. particleBuffer,
  60. velocityBuffer,
  61. pointerGroup
  62. ];
  63. const computeShader = /* glsl */`#version 450
  64. #define PARTICLE_NUM ${particleNum}
  65. #define PARTICLE_SIZE ${particleSize}
  66. #define ROOM_SIZE 1.0
  67. #define POINTER_SIZE 0.1
  68. // Limitation for now: the order should be the same as bindings order
  69. layout(set = 0, binding = 0) buffer Particle {
  70. float particle[ PARTICLE_NUM * PARTICLE_SIZE ];
  71. } particle;
  72. layout(set = 0, binding = 1) buffer Velocity {
  73. float velocity[ PARTICLE_NUM * PARTICLE_SIZE ];
  74. } velocity;
  75. layout(set = 0, binding = 2) uniform MouseUniforms {
  76. vec2 pointer;
  77. } mouseUniforms;
  78. void main() {
  79. uint index = gl_GlobalInvocationID.x;
  80. if ( index >= PARTICLE_NUM ) { return; }
  81. vec3 position = vec3(
  82. particle.particle[ index * 3 + 0 ] + velocity.velocity[ index * 3 + 0 ],
  83. particle.particle[ index * 3 + 1 ] + velocity.velocity[ index * 3 + 1 ],
  84. particle.particle[ index * 3 + 2 ] + velocity.velocity[ index * 3 + 2 ]
  85. );
  86. if ( abs( position.x ) >= ROOM_SIZE ) {
  87. velocity.velocity[ index * 3 + 0 ] = - velocity.velocity[ index * 3 + 0 ];
  88. }
  89. if ( abs( position.y ) >= ROOM_SIZE ) {
  90. velocity.velocity[ index * 3 + 1 ] = - velocity.velocity[ index * 3 + 1 ];
  91. }
  92. if ( abs( position.z ) >= ROOM_SIZE ) {
  93. velocity.velocity[ index * 3 + 2 ] = - velocity.velocity[ index * 3 + 2 ];
  94. }
  95. float dx = mouseUniforms.pointer.x - position.x;
  96. float dy = mouseUniforms.pointer.y - position.y;
  97. float distanceFromPointer = sqrt( dx * dx + dy * dy );
  98. if ( distanceFromPointer <= POINTER_SIZE ) {
  99. position.x = 0.0;
  100. position.y = 0.0;
  101. position.z = 0.0;
  102. }
  103. particle.particle[ index * 3 + 0 ] = position.x;
  104. particle.particle[ index * 3 + 1 ] = position.y;
  105. particle.particle[ index * 3 + 2 ] = position.z;
  106. }
  107. `;
  108. computeParams.push( {
  109. num: particleNum,
  110. shader: computeShader,
  111. bindings: computeBindings
  112. } );
  113. // Use a compute shader to animate the point cloud's vertex data.
  114. const pointsGeometry = new THREE.BufferGeometry().setAttribute(
  115. 'position', particleBuffer.attribute
  116. );
  117. const pointsMaterial = new THREE.PointsMaterial();
  118. pointsMaterial.colorNode = new OperatorNode( '+', new PositionNode(), new ColorNode( new THREE.Color( 0x0000FF ) ) );
  119. const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
  120. scene.add( mesh );
  121. renderer = new WebGPURenderer();
  122. renderer.setPixelRatio( window.devicePixelRatio );
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. document.body.appendChild( renderer.domElement );
  125. window.addEventListener( 'resize', onWindowResize );
  126. window.addEventListener( 'mousemove', onMouseMove );
  127. return renderer.init();
  128. }
  129. function onWindowResize() {
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function onMouseMove( event ) {
  134. const x = event.clientX;
  135. const y = event.clientY;
  136. const width = window.innerWidth;
  137. const height = window.innerHeight;
  138. pointer.set(
  139. ( x / width - 0.5 ) * 2.0,
  140. ( - y / height + 0.5 ) * 2.0
  141. );
  142. }
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. renderer.compute( computeParams );
  146. renderer.render( scene, camera );
  147. }
  148. function error( error ) {
  149. console.error( error );
  150. }
  151. </script>
  152. </body>
  153. </html>