webgpu_compute_points.html 5.0 KB

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