webgl_gpu_particle_system.html 4.1 KB

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