webgpu_compute_particles.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <html lang="en">
  2. <head>
  3. <title>three.js - WebGPU - Compute Particles</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 - 1M Particles
  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, texture, instanceIndex, float, vec3, storage, SpriteNodeMaterial, If } from 'three/nodes';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. const particleCount = 1000000;
  30. const gravity = uniform( - .0098 );
  31. const bounce = uniform( .8 );
  32. const friction = uniform( .99 );
  33. const size = uniform( .12 );
  34. const clickPosition = uniform( new THREE.Vector3() );
  35. let camera, scene, renderer;
  36. let controls, stats;
  37. let computeParticles;
  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. const { innerWidth, innerHeight } = window;
  45. camera = new THREE.PerspectiveCamera( 50, innerWidth / innerHeight, .1, 1000 );
  46. camera.position.set( 15, 30, 15 );
  47. scene = new THREE.Scene();
  48. // textures
  49. const textureLoader = new THREE.TextureLoader();
  50. const map = textureLoader.load( 'textures/sprite1.png' );
  51. //
  52. const createBuffer = () => storage( new THREE.InstancedBufferAttribute( new Float32Array( particleCount * 4 ), 4 ), 'vec3', particleCount );
  53. const positionBuffer = createBuffer();
  54. const velocityBuffer = createBuffer();
  55. const colorBuffer = createBuffer();
  56. // compute
  57. const computeInit = tslFn( () => {
  58. const position = positionBuffer.element( instanceIndex );
  59. const color = colorBuffer.element( instanceIndex );
  60. const randX = instanceIndex.hash();
  61. const randY = instanceIndex.add( 2 ).hash();
  62. const randZ = instanceIndex.add( 3 ).hash();
  63. position.x = randX.mul( 100 ).add( - 50 );
  64. position.y = 0; // randY.mul( 10 );
  65. position.z = randZ.mul( 100 ).add( - 50 );
  66. color.assign( vec3( randX, randY, randZ ) );
  67. } )().compute( particleCount );
  68. //
  69. const computeUpdate = tslFn( () => {
  70. const position = positionBuffer.element( instanceIndex );
  71. const velocity = velocityBuffer.element( instanceIndex );
  72. velocity.addAssign( vec3( 0.00, gravity, 0.00 ) );
  73. position.addAssign( velocity );
  74. velocity.mulAssign( friction );
  75. // floor
  76. If( position.y.lessThan( 0 ), () => {
  77. position.y = 0;
  78. velocity.y = velocity.y.negate().mul( bounce );
  79. // floor friction
  80. velocity.x = velocity.x.mul( .9 );
  81. velocity.z = velocity.z.mul( .9 );
  82. } );
  83. } );
  84. computeParticles = computeUpdate().compute( particleCount );
  85. // create nodes
  86. const textureNode = texture( map );
  87. // create particles
  88. const particleMaterial = new SpriteNodeMaterial();
  89. particleMaterial.colorNode = textureNode.mul( colorBuffer.element( instanceIndex ) );
  90. particleMaterial.positionNode = positionBuffer.toAttribute();
  91. particleMaterial.scaleNode = size;
  92. particleMaterial.depthWrite = false;
  93. particleMaterial.depthTest = true;
  94. particleMaterial.transparent = true;
  95. const particles = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), particleMaterial );
  96. particles.isInstancedMesh = true;
  97. particles.count = particleCount;
  98. particles.frustumCulled = false;
  99. scene.add( particles );
  100. //
  101. const helper = new THREE.GridHelper( 60, 40, 0x303030, 0x303030 );
  102. scene.add( helper );
  103. const geometry = new THREE.PlaneGeometry( 1000, 1000 );
  104. geometry.rotateX( - Math.PI / 2 );
  105. const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
  106. scene.add( plane );
  107. const raycaster = new THREE.Raycaster();
  108. const pointer = new THREE.Vector2();
  109. //
  110. renderer = new WebGPURenderer( { antialias: true } );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. renderer.setAnimationLoop( animate );
  114. document.body.appendChild( renderer.domElement );
  115. stats = new Stats();
  116. document.body.appendChild( stats.dom );
  117. //
  118. renderer.compute( computeInit );
  119. // click event
  120. const computeHit = tslFn( () => {
  121. const position = positionBuffer.element( instanceIndex );
  122. const velocity = velocityBuffer.element( instanceIndex );
  123. const dist = position.distance( clickPosition );
  124. const direction = position.sub( clickPosition ).normalize();
  125. const distArea = float( 6 ).sub( dist ).max( 0 );
  126. const power = distArea.mul( .01 );
  127. const relativePower = power.mul( instanceIndex.hash().mul( .5 ).add( .5 ) );
  128. velocity.assign( velocity.add( direction.mul( relativePower ) ) );
  129. } )().compute( particleCount );
  130. //
  131. function onMove( event ) {
  132. pointer.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
  133. raycaster.setFromCamera( pointer, camera );
  134. const intersects = raycaster.intersectObjects( [ plane ], false );
  135. if ( intersects.length > 0 ) {
  136. const { point } = intersects[ 0 ];
  137. // move to uniform
  138. clickPosition.value.copy( point );
  139. clickPosition.value.y = - 1;
  140. // compute
  141. renderer.compute( computeHit );
  142. }
  143. }
  144. // events
  145. renderer.domElement.addEventListener( 'pointermove', onMove );
  146. //
  147. controls = new OrbitControls( camera, renderer.domElement );
  148. controls.minDistance = 5;
  149. controls.maxDistance = 200;
  150. controls.target.set( 0, 0, 0 );
  151. controls.update();
  152. //
  153. window.addEventListener( 'resize', onWindowResize );
  154. // gui
  155. const gui = new GUI();
  156. gui.add( gravity, 'value', - .0098, 0, 0.0001 ).name( 'gravity' );
  157. gui.add( bounce, 'value', .1, 1, 0.01 ).name( 'bounce' );
  158. gui.add( friction, 'value', .96, .99, 0.01 ).name( 'friction' );
  159. gui.add( size, 'value', .12, .5, 0.01 ).name( 'size' );
  160. }
  161. function onWindowResize() {
  162. const { innerWidth, innerHeight } = window;
  163. camera.aspect = innerWidth / innerHeight;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( innerWidth, innerHeight );
  166. }
  167. function animate() {
  168. stats.update();
  169. renderer.compute( computeParticles );
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>