webgl_gpu_particle_system.html 4.2 KB

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