webgl_gpu_particle_system.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 src="../build/three.js"></script>
  16. <script src="./js/controls/TrackballControls.js"></script>
  17. <script src="./js/libs/dat.gui.min.js"></script>
  18. <script src="./js/libs/stats.min.js"></script>
  19. <script src="./js/GPUParticleSystem.js"></script>
  20. <script>
  21. var camera, tick = 0,
  22. scene, renderer, clock = new THREE.Clock(),
  23. controls, container, gui = new dat.GUI( { width: 350 } ),
  24. options, spawnerOptions, particleSystem;
  25. var stats;
  26. init();
  27. animate();
  28. function init() {
  29. //
  30. container = document.getElementById( 'container' );
  31. camera = new THREE.PerspectiveCamera( 28, window.innerWidth / window.innerHeight, 1, 10000 );
  32. camera.position.z = 100;
  33. scene = new THREE.Scene();
  34. // The GPU Particle system extends THREE.Object3D, and so you can use it
  35. // as you would any other scene graph component. Particle positions will be
  36. // relative to the position of the particle system, but you will probably only need one
  37. // system for your whole scene
  38. particleSystem = new THREE.GPUParticleSystem( {
  39. maxParticles: 250000
  40. } );
  41. scene.add( particleSystem );
  42. // options passed during each spawned
  43. options = {
  44. position: new THREE.Vector3(),
  45. positionRandomness: .3,
  46. velocity: new THREE.Vector3(),
  47. velocityRandomness: .5,
  48. color: 0xaa88ff,
  49. colorRandomness: .2,
  50. turbulence: .5,
  51. lifetime: 2,
  52. size: 5,
  53. sizeRandomness: 1
  54. };
  55. spawnerOptions = {
  56. spawnRate: 15000,
  57. horizontalSpeed: 1.5,
  58. verticalSpeed: 1.33,
  59. timeScale: 1
  60. };
  61. //
  62. gui.add( options, "velocityRandomness", 0, 3 );
  63. gui.add( options, "positionRandomness", 0, 3 );
  64. gui.add( options, "size", 1, 20 );
  65. gui.add( options, "sizeRandomness", 0, 25 );
  66. gui.add( options, "colorRandomness", 0, 1 );
  67. gui.add( options, "lifetime", .1, 10 );
  68. gui.add( options, "turbulence", 0, 1 );
  69. gui.add( spawnerOptions, "spawnRate", 10, 30000 );
  70. gui.add( spawnerOptions, "timeScale", - 1, 1 );
  71. //
  72. stats = new Stats();
  73. container.appendChild( stats.dom );
  74. //
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. //
  80. controls = new THREE.TrackballControls( camera, renderer.domElement );
  81. controls.rotateSpeed = 5.0;
  82. controls.zoomSpeed = 2.2;
  83. controls.panSpeed = 1;
  84. controls.dynamicDampingFactor = 0.3;
  85. window.addEventListener( 'resize', onWindowResize, false );
  86. }
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. controls.update();
  95. var delta = clock.getDelta() * spawnerOptions.timeScale;
  96. tick += delta;
  97. if ( tick < 0 ) tick = 0;
  98. if ( delta > 0 ) {
  99. options.position.x = Math.sin( tick * spawnerOptions.horizontalSpeed ) * 20;
  100. options.position.y = Math.sin( tick * spawnerOptions.verticalSpeed ) * 10;
  101. options.position.z = Math.sin( tick * spawnerOptions.horizontalSpeed + spawnerOptions.verticalSpeed ) * 5;
  102. for ( var x = 0; x < spawnerOptions.spawnRate * delta; x ++ ) {
  103. // Yep, that's really it. Spawning particles is super cheap, and once you spawn them, the rest of
  104. // their lifecycle is handled entirely on the GPU, driven by a time uniform updated below
  105. particleSystem.spawnParticle( options );
  106. }
  107. }
  108. particleSystem.update( tick );
  109. render();
  110. stats.update();
  111. }
  112. function render() {
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>