webgl_particles_random.html 4.6 KB

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