particles_random_gl.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - particles - webgl</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. #oldie {
  28. font-family:monospace;
  29. font-size:13px;
  30. text-align:center;
  31. background:#eee;
  32. color:#000;
  33. padding:1em;
  34. width:475px;
  35. margin:5em auto 0;
  36. display:none;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="info">
  42. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl particles example
  43. </div>
  44. <center>
  45. <div id="oldie">
  46. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
  47. and <a href="http://www.whatwg.org/specs/web-workers/current-work/">Web Workers</a>.<br/>
  48. <br/>
  49. Please try in
  50. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  51. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  52. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  53. </div>
  54. </center>
  55. <script type="text/javascript" src="../build/Three.js"></script>
  56. <script type="text/javascript" src="js/Stats.js"></script>
  57. <script type="text/javascript">
  58. if ( !is_browser_compatible() ) {
  59. document.getElementById( "oldie" ).style.display = "block";
  60. }
  61. var container, stats;
  62. var camera, scene, renderer, particles, geometry, materials = [], parameters, i, h, color;
  63. var mouseX = 0, mouseY = 0;
  64. var windowHalfX = window.innerWidth / 2;
  65. var windowHalfY = window.innerHeight / 2;
  66. init();
  67. setInterval( loop, 1000 / 60 );
  68. function init() {
  69. container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  72. camera.position.z = 1000;
  73. scene = new THREE.Scene();
  74. scene.fog = new THREE.FogExp2( 0x000000, 0.0007 );
  75. geometry = new THREE.Geometry();
  76. for ( i = 0; i < 4000; i++ ) {
  77. vector = new THREE.Vector3( Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 - 1000 );
  78. geometry.vertices.push( new THREE.Vertex( vector ) );
  79. }
  80. 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 ] ];
  81. //parameters = [ [ 0xff0000, 5 ], [ 0xff3300, 4 ], [ 0xff6600, 3 ], [ 0xff9900, 2 ], [ 0xffaa00, 1 ] ];
  82. //parameters = [ [ 0xffffff, 5 ], [ 0xdddddd, 4 ], [ 0xaaaaaa, 3 ], [ 0x999999, 2 ], [ 0x777777, 1 ] ];
  83. for ( i = 0; i < parameters.length; i++ ) {
  84. size = parameters[i][1];
  85. color = parameters[i][0];
  86. //materials[i] = new THREE.ParticleBasicMaterial( { color: color, size: size } );
  87. materials[i] = new THREE.ParticleBasicMaterial( { size: size } );
  88. materials[i].color.setHSV( color[0], color[1], color[2] );
  89. particles = new THREE.ParticleSystem( geometry, materials[i] );
  90. particles.rotation.x = Math.random() * 6;
  91. particles.rotation.y = Math.random() * 6;
  92. particles.rotation.z = Math.random() * 6;
  93. scene.addObject( particles );
  94. }
  95. renderer = new THREE.WebGLRenderer();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. stats = new Stats();
  99. stats.domElement.style.position = 'absolute';
  100. stats.domElement.style.top = '0px';
  101. container.appendChild( stats.domElement );
  102. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  103. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  104. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  105. }
  106. function onDocumentMouseMove( event ) {
  107. mouseX = event.clientX - windowHalfX;
  108. mouseY = event.clientY - windowHalfY;
  109. }
  110. function onDocumentTouchStart( event ) {
  111. if ( event.touches.length == 1 ) {
  112. event.preventDefault();
  113. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  114. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  115. }
  116. }
  117. function onDocumentTouchMove( event ) {
  118. if ( event.touches.length == 1 ) {
  119. event.preventDefault();
  120. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  121. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  122. }
  123. }
  124. function loop() {
  125. var time = new Date().getTime() * 0.00005;
  126. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  127. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  128. for( i = 0; i < scene.objects.length; i++ ) {
  129. scene.objects[i].rotation.y = time * ( i < 4 ? i+1 : - (i+1) );
  130. }
  131. for( i = 0; i < materials.length; i++ ) {
  132. color = parameters[i][0];
  133. h = ( 360 * ( color[0] + time ) % 360 ) / 360;
  134. materials[i].color.setHSV( h, color[1], color[2] );
  135. }
  136. renderer.render( scene, camera );
  137. stats.update();
  138. }
  139. function is_browser_compatible() {
  140. // WebGL support
  141. try { var test = new Float32Array(1); } catch(e) { return false; }
  142. // Web workers
  143. return !!window.Worker;
  144. }
  145. </script>
  146. </body>
  147. </html>