webgpu_compute_particles.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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="example.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  11. <div class="title-wrapper">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Compute Particles</span>
  13. </div>
  14. <small>500k Particles.</small>
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three/webgpu';
  28. import { Fn, If, uniform, float, uv, vec3, hash, shapeCircle,
  29. instancedArray, instanceIndex } from 'three/tsl';
  30. import { Inspector } from 'three/addons/inspector/Inspector.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. const particleCount = 200000;
  33. const gravity = uniform( - .00098 );
  34. const bounce = uniform( .8 );
  35. const friction = uniform( .99 );
  36. const size = uniform( .12 );
  37. const clickPosition = uniform( new THREE.Vector3() );
  38. let camera, scene, renderer;
  39. let controls;
  40. let computeParticles;
  41. let isOrbitControlsActive;
  42. init();
  43. async function init() {
  44. const { innerWidth, innerHeight } = window;
  45. camera = new THREE.PerspectiveCamera( 50, innerWidth / innerHeight, .1, 1000 );
  46. camera.position.set( 0, 5, 20 );
  47. scene = new THREE.Scene();
  48. //
  49. const positions = instancedArray( particleCount, 'vec3' );
  50. const velocities = instancedArray( particleCount, 'vec3' );
  51. const colors = instancedArray( particleCount, 'vec3' );
  52. // compute
  53. const separation = 0.2;
  54. const amount = Math.sqrt( particleCount );
  55. const offset = float( amount / 2 );
  56. const computeInit = Fn( () => {
  57. const position = positions.element( instanceIndex );
  58. const color = colors.element( instanceIndex );
  59. const x = instanceIndex.mod( amount );
  60. const z = instanceIndex.div( amount );
  61. position.x = offset.sub( x ).mul( separation );
  62. position.z = offset.sub( z ).mul( separation );
  63. color.x = hash( instanceIndex );
  64. color.y = hash( instanceIndex.add( 2 ) );
  65. } )().compute( particleCount ).setName( 'Init Particles' );
  66. //
  67. const computeUpdate = Fn( () => {
  68. const position = positions.element( instanceIndex );
  69. const velocity = velocities.element( instanceIndex );
  70. velocity.addAssign( vec3( 0.00, gravity, 0.00 ) );
  71. position.addAssign( velocity );
  72. velocity.mulAssign( friction );
  73. // floor
  74. If( position.y.lessThan( 0 ), () => {
  75. position.y = 0;
  76. velocity.y = velocity.y.negate().mul( bounce );
  77. // floor friction
  78. velocity.x = velocity.x.mul( .9 );
  79. velocity.z = velocity.z.mul( .9 );
  80. } );
  81. } );
  82. computeParticles = computeUpdate().compute( particleCount ).setName( 'Update Particles' );
  83. // create particles
  84. const material = new THREE.SpriteNodeMaterial();
  85. material.colorNode = uv().mul( colors.element( instanceIndex ) );
  86. material.positionNode = positions.toAttribute();
  87. material.scaleNode = size;
  88. material.opacityNode = shapeCircle();
  89. material.alphaToCoverage = true;
  90. material.transparent = true;
  91. const particles = new THREE.Sprite( material );
  92. particles.count = particleCount;
  93. particles.frustumCulled = false;
  94. scene.add( particles );
  95. //
  96. const helper = new THREE.GridHelper( 90, 45, 0x303030, 0x303030 );
  97. scene.add( helper );
  98. const geometry = new THREE.PlaneGeometry( 200, 200 );
  99. geometry.rotateX( - Math.PI / 2 );
  100. const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
  101. scene.add( plane );
  102. const raycaster = new THREE.Raycaster();
  103. const pointer = new THREE.Vector2();
  104. //
  105. renderer = new THREE.WebGPURenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. renderer.setAnimationLoop( animate );
  109. renderer.inspector = new Inspector();
  110. document.body.appendChild( renderer.domElement );
  111. await renderer.init();
  112. //
  113. renderer.compute( computeInit );
  114. // Hit
  115. const computeHit = Fn( () => {
  116. const position = positions.element( instanceIndex );
  117. const velocity = velocities.element( instanceIndex );
  118. const dist = position.distance( clickPosition );
  119. const direction = position.sub( clickPosition ).normalize();
  120. const distArea = float( 3 ).sub( dist ).max( 0 );
  121. const power = distArea.mul( .01 );
  122. const relativePower = power.mul( hash( instanceIndex ).mul( 1.5 ).add( .5 ) );
  123. velocity.assign( velocity.add( direction.mul( relativePower ) ) );
  124. } )().compute( particleCount ).setName( 'Hit Particles' );
  125. //
  126. function onMove( event ) {
  127. if ( isOrbitControlsActive ) return;
  128. pointer.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
  129. raycaster.setFromCamera( pointer, camera );
  130. const intersects = raycaster.intersectObject( plane, false );
  131. if ( intersects.length > 0 ) {
  132. const { point } = intersects[ 0 ];
  133. // move to uniform
  134. clickPosition.value.copy( point );
  135. clickPosition.value.y = - 1;
  136. // compute
  137. renderer.compute( computeHit );
  138. }
  139. }
  140. renderer.domElement.addEventListener( 'pointermove', onMove );
  141. // controls
  142. controls = new OrbitControls( camera, renderer.domElement );
  143. controls.enableDamping = true;
  144. controls.minDistance = 5;
  145. controls.maxDistance = 200;
  146. controls.target.set( 0, - 8, 0 );
  147. controls.update();
  148. controls.addEventListener( 'start', () => {
  149. isOrbitControlsActive = true;
  150. } );
  151. controls.addEventListener( 'end', () => {
  152. isOrbitControlsActive = false;
  153. } );
  154. controls.touches = {
  155. ONE: null,
  156. TWO: THREE.TOUCH.DOLLY_PAN
  157. };
  158. //
  159. window.addEventListener( 'resize', onWindowResize );
  160. // gui
  161. const gui = renderer.inspector.createParameters( 'Settings' );
  162. gui.add( gravity, 'value', - .0098, 0, 0.0001 ).name( 'gravity' );
  163. gui.add( bounce, 'value', .1, 1, 0.01 ).name( 'bounce' );
  164. gui.add( friction, 'value', .96, .99, 0.01 ).name( 'friction' );
  165. gui.add( size, 'value', .12, .5, 0.01 ).name( 'size' );
  166. }
  167. function onWindowResize() {
  168. const { innerWidth, innerHeight } = window;
  169. camera.aspect = innerWidth / innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( innerWidth, innerHeight );
  172. }
  173. function animate() {
  174. controls.update();
  175. renderer.compute( computeParticles );
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号