webgl_particles_random.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. vector = new THREE.Vector3( Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 - 1000 );
  57. geometry.vertices.push( new THREE.Vertex( vector ) );
  58. }
  59. 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 ] ];
  60. //parameters = [ [ 0xff0000, 5 ], [ 0xff3300, 4 ], [ 0xff6600, 3 ], [ 0xff9900, 2 ], [ 0xffaa00, 1 ] ];
  61. //parameters = [ [ 0xffffff, 5 ], [ 0xdddddd, 4 ], [ 0xaaaaaa, 3 ], [ 0x999999, 2 ], [ 0x777777, 1 ] ];
  62. for ( i = 0; i < parameters.length; i ++ ) {
  63. size = parameters[i][1];
  64. color = parameters[i][0];
  65. //materials[i] = new THREE.ParticleBasicMaterial( { color: color, size: size } );
  66. materials[i] = new THREE.ParticleBasicMaterial( { size: size } );
  67. materials[i].color.setHSV( color[0], color[1], color[2] );
  68. particles = new THREE.ParticleSystem( geometry, materials[i] );
  69. particles.rotation.x = Math.random() * 6;
  70. particles.rotation.y = Math.random() * 6;
  71. particles.rotation.z = Math.random() * 6;
  72. scene.add( particles );
  73. }
  74. renderer = new THREE.WebGLRenderer();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. container.appendChild( renderer.domElement );
  77. stats = new Stats();
  78. stats.domElement.style.position = 'absolute';
  79. stats.domElement.style.top = '0px';
  80. container.appendChild( stats.domElement );
  81. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  82. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  83. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  84. }
  85. function onDocumentMouseMove( event ) {
  86. mouseX = event.clientX - windowHalfX;
  87. mouseY = event.clientY - windowHalfY;
  88. }
  89. function onDocumentTouchStart( event ) {
  90. if ( event.touches.length == 1 ) {
  91. event.preventDefault();
  92. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  93. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  94. }
  95. }
  96. function onDocumentTouchMove( event ) {
  97. if ( event.touches.length == 1 ) {
  98. event.preventDefault();
  99. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  100. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  101. }
  102. }
  103. //
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. var time = Date.now() * 0.00005;
  111. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  112. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  113. camera.lookAt( scene.position );
  114. for( i = 0; i < scene.objects.length; i ++ ) {
  115. scene.objects[i].rotation.y = time * ( i < 4 ? i+1 : - (i+1) );
  116. }
  117. for( i = 0; i < materials.length; i ++ ) {
  118. color = parameters[i][0];
  119. h = ( 360 * ( color[0] + time ) % 360 ) / 360;
  120. materials[i].color.setHSV( h, color[1], color[2] );
  121. }
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>