webgpu_compute.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <html lang="en">
  2. <head>
  3. <title>three.js - 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. <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
  8. <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
  9. </head>
  10. <body>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute
  13. </div>
  14. <script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three-nodes/": "./jsm/nodes/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as Nodes from 'three-nodes/Nodes.js';
  26. import {
  27. compute,
  28. color, add, uniform, element, storage, func,
  29. assign, float, mul,
  30. positionLocal, instanceIndex
  31. } from 'three-nodes/Nodes.js';
  32. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  33. import WebGPU from './jsm/capabilities/WebGPU.js';
  34. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  35. let camera, scene, renderer;
  36. let computeNode;
  37. const pointer = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
  38. const scaleVector = new THREE.Vector2( 1, 1 );
  39. init().then( animate ).catch( error );
  40. async function init() {
  41. if ( WebGPU.isAvailable() === false ) {
  42. document.body.appendChild( WebGPU.getErrorMessage() );
  43. throw new Error( 'No WebGPU support' );
  44. }
  45. camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
  46. camera.position.z = 1;
  47. scene = new THREE.Scene();
  48. // initialize particles
  49. const particleNum = 65000; // 16-bit limit
  50. const particleSize = 3; // vec3
  51. const particleArray = new Float32Array( particleNum * particleSize );
  52. const velocityArray = new Float32Array( particleNum * particleSize );
  53. for ( let i = 0; i < particleArray.length; i += particleSize ) {
  54. const r = Math.random() * 0.01 + 0.0005;
  55. const degree = Math.random() * 360;
  56. velocityArray[ i + 0 ] = r * Math.sin( degree * Math.PI / 180 );
  57. velocityArray[ i + 1 ] = r * Math.cos( degree * Math.PI / 180 );
  58. }
  59. // create buffers
  60. const particleBuffer = new THREE.BufferAttribute( particleArray, particleSize );
  61. const velocityBuffer = new THREE.BufferAttribute( velocityArray, particleSize );
  62. const particleBufferNode = storage( particleBuffer, 'vec3' );
  63. const velocityBufferNode = storage( velocityBuffer, 'vec3' );
  64. // create wgsl function
  65. const WGSLFnNode = func( `( pointer:vec2<f32>, limit:vec2<f32> ) {
  66. var position = particle + velocity;
  67. if ( abs( position.x ) >= limit.x ) {
  68. if ( position.x > 0.0 ) {
  69. position.x = limit.x;
  70. } else {
  71. position.x = -limit.x;
  72. }
  73. velocity.x = - velocity.x;
  74. }
  75. if ( abs( position.y ) >= limit.y ) {
  76. if ( position.y > 0.0 ) {
  77. position.y = limit.y;
  78. } else {
  79. position.y = -limit.y;
  80. }
  81. velocity.y = - velocity.y ;
  82. }
  83. let POINTER_SIZE = .1;
  84. let dx = pointer.x - position.x;
  85. let dy = pointer.y - position.y;
  86. let distanceFromPointer = sqrt( dx * dx + dy * dy );
  87. if ( distanceFromPointer <= POINTER_SIZE ) {
  88. position.x = 0.0;
  89. position.y = 0.0;
  90. position.z = 0.0;
  91. }
  92. particle = position;
  93. }
  94. ` );
  95. // define particle and velocity keywords in wgsl function
  96. // it's used in case of needed change a global variable like this storageBuffer
  97. const particleNode = element( particleBufferNode, instanceIndex );
  98. const velocityNode = element( velocityBufferNode, instanceIndex );
  99. WGSLFnNode.keywords[ 'particle' ] = particleNode;
  100. WGSLFnNode.keywords[ 'velocity' ] = velocityNode;
  101. // compute
  102. computeNode = compute( particleNum );
  103. // Example 1: Calling a WGSL function
  104. computeNode.computeNode = WGSLFnNode.call( {
  105. pointer: uniform( pointer ),
  106. limit: uniform( scaleVector )
  107. } );
  108. // Example 2: Creating single storage assign
  109. //computeNode.computeNode = assign( particleNode, add( particleNode, velocityNode ) );
  110. // Example 3: Creating multiples storage assign
  111. /*computeNode.computeNode = new Nodes.ShaderNode( ( {}, builder ) => {
  112. assign( particleNode, add( particleNode, velocityNode ) ).build( builder );
  113. assign( velocityNode, mul( velocityNode, float( 0.99 ) ) ).build( builder );
  114. } );/**/
  115. // use a compute shader to animate the point cloud's vertex data.
  116. const pointsGeometry = new THREE.BufferGeometry();
  117. pointsGeometry.setAttribute( 'position', particleBuffer );
  118. const pointsMaterial = new Nodes.PointsNodeMaterial();
  119. pointsMaterial.colorNode = add( positionLocal, color( 0xFFFFFF ) );
  120. const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
  121. scene.add( mesh );
  122. renderer = new WebGPURenderer();
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. document.body.appendChild( renderer.domElement );
  126. window.addEventListener( 'resize', onWindowResize );
  127. window.addEventListener( 'mousemove', onMouseMove );
  128. // gui
  129. const gui = new GUI();
  130. gui.add( scaleVector, 'x', 0, 1, 0.01 );
  131. gui.add( scaleVector, 'y', 0, 1, 0.01 );
  132. return renderer.init();
  133. }
  134. function onWindowResize() {
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function onMouseMove( event ) {
  139. const x = event.clientX;
  140. const y = event.clientY;
  141. const width = window.innerWidth;
  142. const height = window.innerHeight;
  143. pointer.set(
  144. ( x / width - 0.5 ) * 2.0,
  145. ( - y / height + 0.5 ) * 2.0
  146. );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. renderer.compute( computeNode );
  151. renderer.render( scene, camera );
  152. }
  153. function error( error ) {
  154. console.error( error );
  155. }
  156. </script>
  157. </body>
  158. </html>
粤ICP备19079148号