webgpu_compute.html 6.1 KB

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