2
0

webgl_gpu_particle_system.html 4.3 KB

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