webgl_gpu_particle_system.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - gpu particle system</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GPU particle system plugin<br/>
  13. by <a href="http://charliehoey.com">Charlie Hoey</a>.
  14. </div>
  15. <script type="module">
  16. import {
  17. Clock,
  18. PerspectiveCamera,
  19. Scene,
  20. Vector3,
  21. WebGLRenderer
  22. } from "../build/three.module.js";
  23. import Stats from './jsm/libs/stats.module.js';
  24. import { GUI } from './jsm/libs/dat.gui.module.js';
  25. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  26. import { GPUParticleSystem } from './jsm/objects/GPUParticleSystem.js';
  27. var camera, tick = 0,
  28. scene, renderer, clock = new Clock(),
  29. controls, container, gui = new GUI( { width: 350 } ),
  30. options, spawnerOptions, particleSystem;
  31. var stats;
  32. init();
  33. animate();
  34. function init() {
  35. //
  36. container = document.getElementById( 'container' );
  37. camera = new PerspectiveCamera( 28, window.innerWidth / window.innerHeight, 1, 10000 );
  38. camera.position.z = 100;
  39. scene = new Scene();
  40. // The GPU Particle system extends THREE.Object3D, and so you can use it
  41. // as you would any other scene graph component. Particle positions will be
  42. // relative to the position of the particle system, but you will probably only need one
  43. // system for your whole scene
  44. particleSystem = new GPUParticleSystem( {
  45. maxParticles: 250000
  46. } );
  47. scene.add( particleSystem );
  48. // options passed during each spawned
  49. options = {
  50. position: new Vector3(),
  51. positionRandomness: .3,
  52. velocity: new Vector3(),
  53. velocityRandomness: .5,
  54. color: 0xaa88ff,
  55. colorRandomness: .2,
  56. turbulence: .5,
  57. lifetime: 2,
  58. size: 5,
  59. sizeRandomness: 1
  60. };
  61. spawnerOptions = {
  62. spawnRate: 15000,
  63. horizontalSpeed: 1.5,
  64. verticalSpeed: 1.33,
  65. timeScale: 1
  66. };
  67. //
  68. gui.add( options, "velocityRandomness", 0, 3 );
  69. gui.add( options, "positionRandomness", 0, 3 );
  70. gui.add( options, "size", 1, 20 );
  71. gui.add( options, "sizeRandomness", 0, 25 );
  72. gui.add( options, "colorRandomness", 0, 1 );
  73. gui.add( options, "lifetime", .1, 10 );
  74. gui.add( options, "turbulence", 0, 1 );
  75. gui.add( spawnerOptions, "spawnRate", 10, 30000 );
  76. gui.add( spawnerOptions, "timeScale", - 1, 1 );
  77. //
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. //
  81. renderer = new WebGLRenderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. //
  86. controls = new TrackballControls( camera, renderer.domElement );
  87. controls.rotateSpeed = 5.0;
  88. controls.zoomSpeed = 2.2;
  89. controls.panSpeed = 1;
  90. controls.dynamicDampingFactor = 0.3;
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. controls.update();
  101. var delta = clock.getDelta() * spawnerOptions.timeScale;
  102. tick += delta;
  103. if ( tick < 0 ) tick = 0;
  104. if ( delta > 0 ) {
  105. options.position.x = Math.sin( tick * spawnerOptions.horizontalSpeed ) * 20;
  106. options.position.y = Math.sin( tick * spawnerOptions.verticalSpeed ) * 10;
  107. options.position.z = Math.sin( tick * spawnerOptions.horizontalSpeed + spawnerOptions.verticalSpeed ) * 5;
  108. for ( var x = 0; x < spawnerOptions.spawnRate * delta; x ++ ) {
  109. // Yep, that's really it. Spawning particles is super cheap, and once you spawn them, the rest of
  110. // their lifecycle is handled entirely on the GPU, driven by a time uniform updated below
  111. particleSystem.spawnParticle( options );
  112. }
  113. }
  114. particleSystem.update( tick );
  115. render();
  116. stats.update();
  117. }
  118. function render() {
  119. renderer.render( scene, camera );
  120. }
  121. </script>
  122. </body>
  123. </html>