webgpu_compute.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute
  11. </div>
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import * as Nodes from 'three/nodes';
  25. import {
  26. ShaderNode, compute,
  27. uniform, element, storage, attribute, mul, sin, cos,
  28. temp, assign, add, sub, cond, abs, negate, max, min, length, float, vec2, vec3, color,
  29. greaterThanEqual, lessThanEqual, instanceIndex
  30. } from 'three/nodes';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  33. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  34. let camera, scene, renderer;
  35. let computeNode;
  36. const pointerVector = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
  37. const scaleVector = new THREE.Vector2( 1, 1 );
  38. init();
  39. function init() {
  40. if ( WebGPU.isAvailable() === false ) {
  41. document.body.appendChild( WebGPU.getErrorMessage() );
  42. throw new Error( 'No WebGPU support' );
  43. }
  44. camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
  45. camera.position.z = 1;
  46. scene = new THREE.Scene();
  47. // initialize particles
  48. const particleNum = 300000;
  49. const particleSize = 2; // vec2
  50. const particleArray = new Float32Array( particleNum * particleSize );
  51. const velocityArray = new Float32Array( particleNum * particleSize );
  52. // create buffers
  53. const particleBuffer = new THREE.InstancedBufferAttribute( particleArray, 2 );
  54. const velocityBuffer = new THREE.InstancedBufferAttribute( velocityArray, 2 );
  55. const particleBufferNode = storage( particleBuffer, 'vec2', particleNum );
  56. const velocityBufferNode = storage( velocityBuffer, 'vec2', particleNum );
  57. // create function
  58. const computeShaderNode = new ShaderNode( ( inputs, builder ) => {
  59. const particle = element( particleBufferNode, instanceIndex );
  60. const velocity = element( velocityBufferNode, instanceIndex );
  61. const pointer = uniform( pointerVector );
  62. const limit = uniform( scaleVector );
  63. const position = temp( add( particle, velocity ), 'tempPos' ); // @TODO: this should work without 'tempPos' property name
  64. position.build( builder );
  65. assign( velocity.x, cond( greaterThanEqual( abs( position.x ), limit.x ), negate( velocity.x ), velocity.x ) ).build( builder );
  66. assign( velocity.y, cond( greaterThanEqual( abs( position.y ), limit.y ), negate( velocity.y ), velocity.y ) ).build( builder );
  67. assign( position, max( negate( limit ), min( limit, position ) ) ).build( builder );
  68. const pointerSize = 0.1;
  69. const distanceFromPointer = length( sub( pointer, position ) );
  70. assign( particle, cond( lessThanEqual( distanceFromPointer, pointerSize ), vec3(), position ) ).build( builder );
  71. } );
  72. // compute
  73. computeNode = compute( computeShaderNode, particleNum );
  74. computeNode.onInit = ( { renderer } ) => {
  75. const precomputeShaderNode = new ShaderNode( ( inputs, builder ) => {
  76. const particleIndex = float( instanceIndex );
  77. const randomAngle = mul( mul( particleIndex, .005 ), Math.PI * 2 );
  78. const randomSpeed = add( mul( particleIndex, 0.00000001 ), 0.0000001 );
  79. const velX = mul( sin( randomAngle ), randomSpeed );
  80. const velY = mul( cos( randomAngle ), randomSpeed );
  81. const velocity = element( velocityBufferNode, instanceIndex );
  82. assign( velocity.xy, vec2( velX, velY ) ).build( builder );
  83. } );
  84. renderer.compute( compute( precomputeShaderNode, computeNode.count ) );
  85. };
  86. // use a compute shader to animate the point cloud's vertex data.
  87. const particleNode = attribute( 'particle', 'vec2' );
  88. const pointsGeometry = new THREE.BufferGeometry();
  89. pointsGeometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 3 ), 3 ) ); // single vertex ( not triangle )
  90. pointsGeometry.setAttribute( 'particle', particleBuffer ); // dummy the position points as instances
  91. pointsGeometry.drawRange.count = 1; // force render points as instances ( not triangle )
  92. const pointsMaterial = new Nodes.PointsNodeMaterial();
  93. pointsMaterial.colorNode = add( particleNode, color( 0xFFFFFF ) );
  94. pointsMaterial.positionNode = particleNode;
  95. const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
  96. mesh.isInstancedMesh = true;
  97. mesh.count = particleNum;
  98. scene.add( mesh );
  99. renderer = new WebGPURenderer();
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.setAnimationLoop( animate );
  103. document.body.appendChild( renderer.domElement );
  104. window.addEventListener( 'resize', onWindowResize );
  105. window.addEventListener( 'mousemove', onMouseMove );
  106. // gui
  107. const gui = new GUI();
  108. gui.add( scaleVector, 'x', 0, 1, 0.01 );
  109. gui.add( scaleVector, 'y', 0, 1, 0.01 );
  110. }
  111. function onWindowResize() {
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function onMouseMove( event ) {
  116. const x = event.clientX;
  117. const y = event.clientY;
  118. const width = window.innerWidth;
  119. const height = window.innerHeight;
  120. pointerVector.set(
  121. ( x / width - 0.5 ) * 2.0,
  122. ( - y / height + 0.5 ) * 2.0
  123. );
  124. }
  125. function animate() {
  126. renderer.compute( computeNode );
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>