2
0

webgpu_compute.html 5.7 KB

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