webgpu_compute_particles.html 7.3 KB

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