webgpu_compute_points.html 5.5 KB

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