webgl_particles_random.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles</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. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color: #fff;
  23. position: absolute;
  24. top: 0px; width: 100%;
  25. padding: 5px;
  26. z-index: 100;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl particles example
  33. </div>
  34. <script src="../build/Three.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/Stats.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var container, stats;
  40. var camera, scene, renderer, particles, geometry, materials = [], parameters, i, h, color;
  41. var mouseX = 0, mouseY = 0;
  42. var windowHalfX = window.innerWidth / 2;
  43. var windowHalfY = window.innerHeight / 2;
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. scene = new THREE.Scene();
  50. scene.fog = new THREE.FogExp2( 0x000000, 0.0007 );
  51. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  52. camera.position.z = 1000;
  53. scene.add( camera );
  54. geometry = new THREE.Geometry();
  55. for ( i = 0; i < 20000; i ++ ) {
  56. var vertex = new THREE.Vertex();
  57. vertex.x = Math.random() * 2000 - 1000;
  58. vertex.y = Math.random() * 2000 - 1000;
  59. vertex.z = Math.random() * 2000 - 1000;
  60. geometry.vertices.push( vertex );
  61. }
  62. parameters = [ [ [1.0, 1.0, 1.0], 5 ], [ [0.95, 1, 1], 4 ], [ [0.90, 1, 1], 3 ], [ [0.85, 1, 1], 2 ], [ [0.80, 1, 1], 1 ] ];
  63. //parameters = [ [ 0xff0000, 5 ], [ 0xff3300, 4 ], [ 0xff6600, 3 ], [ 0xff9900, 2 ], [ 0xffaa00, 1 ] ];
  64. //parameters = [ [ 0xffffff, 5 ], [ 0xdddddd, 4 ], [ 0xaaaaaa, 3 ], [ 0x999999, 2 ], [ 0x777777, 1 ] ];
  65. for ( i = 0; i < parameters.length; i ++ ) {
  66. size = parameters[i][1];
  67. color = parameters[i][0];
  68. //materials[i] = new THREE.ParticleBasicMaterial( { color: color, size: size } );
  69. materials[i] = new THREE.ParticleBasicMaterial( { size: size } );
  70. materials[i].color.setHSV( color[0], color[1], color[2] );
  71. particles = new THREE.ParticleSystem( geometry, materials[i] );
  72. particles.rotation.x = Math.random() * 6;
  73. particles.rotation.y = Math.random() * 6;
  74. particles.rotation.z = Math.random() * 6;
  75. scene.add( particles );
  76. }
  77. renderer = new THREE.WebGLRenderer();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. stats.domElement.style.position = 'absolute';
  82. stats.domElement.style.top = '0px';
  83. container.appendChild( stats.domElement );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  86. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  87. }
  88. function onDocumentMouseMove( event ) {
  89. mouseX = event.clientX - windowHalfX;
  90. mouseY = event.clientY - windowHalfY;
  91. }
  92. function onDocumentTouchStart( event ) {
  93. if ( event.touches.length == 1 ) {
  94. event.preventDefault();
  95. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  96. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  97. }
  98. }
  99. function onDocumentTouchMove( event ) {
  100. if ( event.touches.length == 1 ) {
  101. event.preventDefault();
  102. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  103. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  104. }
  105. }
  106. //
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. render();
  110. stats.update();
  111. }
  112. function render() {
  113. var time = Date.now() * 0.00005;
  114. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  115. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  116. camera.lookAt( scene.position );
  117. for ( i = 0; i < scene.children.length; i ++ ) {
  118. var object = scene.children[ i ];
  119. if ( object instanceof THREE.ParticleSystem ) {
  120. object.rotation.y = time * ( i < 4 ? i + 1 : - ( i + 1 ) );
  121. }
  122. }
  123. for ( i = 0; i < materials.length; i ++ ) {
  124. color = parameters[i][0];
  125. h = ( 360 * ( color[0] + time ) % 360 ) / 360;
  126. materials[i].color.setHSV( h, color[1], color[2] );
  127. }
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>