webgl_gpu_particle_system.html 4.4 KB

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