webgpu_compute.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/[email protected]/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. ShaderNode, compute, context,
  28. uniform, element, storage, attribute,
  29. temp, assign, add, sub, cond, abs, negate, max, min, length, vec3, color,
  30. greaterThanEqual, lessThanEqual, 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 pointerVector = 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 = 300000;
  50. const particleSize = 2; // vec2
  51. const particleArray = new Float32Array( particleNum * particleSize );
  52. const velocityArray = new Float32Array( particleNum * particleSize );
  53. for ( let i = 0; i < particleNum; i ++ ) {
  54. const r = Math.random() * 0.01 + 0.005;
  55. const degree = Math.random() * 360;
  56. velocityArray[ i * particleSize + 0 ] = r * Math.sin( degree * Math.PI / 180 ); // x
  57. velocityArray[ i * particleSize + 1 ] = r * Math.cos( degree * Math.PI / 180 ); // y
  58. }
  59. // create buffers
  60. const particleBuffer = new THREE.InstancedBufferAttribute( particleArray );
  61. const velocityBuffer = new THREE.InstancedBufferAttribute( velocityArray );
  62. const particleBufferNode = storage( particleBuffer, 'vec2', particleNum );
  63. const velocityBufferNode = storage( velocityBuffer, 'vec2', particleNum );
  64. // create function
  65. const FnNode = new ShaderNode( ( inputs, builder ) => {
  66. const particle = element( particleBufferNode, instanceIndex );
  67. const velocity = element( velocityBufferNode, instanceIndex );
  68. const pointer = uniform( pointerVector );
  69. const limit = uniform( scaleVector );
  70. const position = temp( context( add( particle, velocity ), { temp: false } ) );
  71. position.build( builder );
  72. assign( velocity.x, cond( greaterThanEqual( abs( position.x ), limit.x ), negate( velocity.x ), velocity.x ) ).build( builder );
  73. assign( velocity.y, cond( greaterThanEqual( abs( position.y ), limit.y ), negate( velocity.y ), velocity.y ) ).build( builder );
  74. assign( position, max( negate( limit ), min( limit, position ) ) ).build( builder );
  75. const pointerSize = 0.1;
  76. const distanceFromPointer = length( sub( pointer, position ) );
  77. assign( particle, cond( lessThanEqual( distanceFromPointer, pointerSize ), vec3(), position ) ).build( builder );
  78. } );
  79. // compute
  80. computeNode = compute( FnNode, particleNum );
  81. // use a compute shader to animate the point cloud's vertex data.
  82. const particleNode = attribute( 'particle', 'vec2' );
  83. const pointsGeometry = new THREE.BufferGeometry();
  84. pointsGeometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 3 ) ) ); // single vertex ( not triangle )
  85. pointsGeometry.setAttribute( 'particle', particleBuffer ); // dummy the position points as instances
  86. pointsGeometry.drawRange.count = 1; // force render points as instances ( not triangle )
  87. const pointsMaterial = new Nodes.PointsNodeMaterial();
  88. pointsMaterial.colorNode = add( particleNode, color( 0xFFFFFF ) );
  89. pointsMaterial.positionNode = particleNode;
  90. const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
  91. mesh.isInstancedMesh = true;
  92. mesh.count = particleNum;
  93. scene.add( mesh );
  94. renderer = new WebGPURenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. document.body.appendChild( renderer.domElement );
  98. window.addEventListener( 'resize', onWindowResize );
  99. window.addEventListener( 'mousemove', onMouseMove );
  100. // gui
  101. const gui = new GUI();
  102. gui.add( scaleVector, 'x', 0, 1, 0.01 );
  103. gui.add( scaleVector, 'y', 0, 1, 0.01 );
  104. return renderer.init();
  105. }
  106. function onWindowResize() {
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function onMouseMove( event ) {
  111. const x = event.clientX;
  112. const y = event.clientY;
  113. const width = window.innerWidth;
  114. const height = window.innerHeight;
  115. pointerVector.set(
  116. ( x / width - 0.5 ) * 2.0,
  117. ( - y / height + 0.5 ) * 2.0
  118. );
  119. }
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. renderer.compute( computeNode );
  123. renderer.render( scene, camera );
  124. }
  125. function error( error ) {
  126. console.error( error );
  127. }
  128. </script>
  129. </body>
  130. </html>