particles_random_gl.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  12. a {
  13. color:#0078ff;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script type="text/javascript" src="../build/Three.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <script type="text/javascript">
  21. var container, stats;
  22. var camera, scene, renderer, particles, geometry, materials = [], parameters, i, h, color;
  23. var mouseX = 0, mouseY = 0;
  24. var windowHalfX = window.innerWidth / 2;
  25. var windowHalfY = window.innerHeight / 2;
  26. init();
  27. setInterval( loop, 1000 / 60 );
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  32. camera.position.z = 1000;
  33. scene = new THREE.Scene();
  34. scene.fog = new THREE.FogExp2( 0x000000, 0.0007 );
  35. geometry = new THREE.Geometry();
  36. for ( i = 0; i < 4000; i++ ) {
  37. vector = new THREE.Vector3( Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 - 1000 );
  38. geometry.vertices.push( new THREE.Vertex( vector ) );
  39. }
  40. 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 ] ];
  41. //parameters = [ [ 0xff0000, 5 ], [ 0xff3300, 4 ], [ 0xff6600, 3 ], [ 0xff9900, 2 ], [ 0xffaa00, 1 ] ];
  42. //parameters = [ [ 0xffffff, 5 ], [ 0xdddddd, 4 ], [ 0xaaaaaa, 3 ], [ 0x999999, 2 ], [ 0x777777, 1 ] ];
  43. for ( i = 0; i < parameters.length; i++ ) {
  44. size = parameters[i][1];
  45. color = parameters[i][0];
  46. //materials[i] = new THREE.ParticleBasicMaterial( { color: color, size: size } );
  47. materials[i] = new THREE.ParticleBasicMaterial( { size: size } );
  48. materials[i].color.setHSV( color[0], color[1], color[2] );
  49. particles = new THREE.ParticleSystem( geometry, materials[i] );
  50. particles.rotation.x = Math.random() * 6;
  51. particles.rotation.y = Math.random() * 6;
  52. particles.rotation.z = Math.random() * 6;
  53. particles.updateMatrix();
  54. scene.addObject( particles );
  55. }
  56. scene.addObject( particles );
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. container.appendChild( renderer.domElement );
  60. stats = new Stats();
  61. stats.domElement.style.position = 'absolute';
  62. stats.domElement.style.top = '0px';
  63. container.appendChild( stats.domElement );
  64. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  65. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  66. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  67. }
  68. function onDocumentMouseMove( event ) {
  69. mouseX = event.clientX - windowHalfX;
  70. mouseY = event.clientY - windowHalfY;
  71. }
  72. function onDocumentTouchStart( event ) {
  73. if ( event.touches.length == 1 ) {
  74. event.preventDefault();
  75. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  76. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  77. }
  78. }
  79. function onDocumentTouchMove( event ) {
  80. if ( event.touches.length == 1 ) {
  81. event.preventDefault();
  82. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  83. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  84. }
  85. }
  86. function loop() {
  87. var time = new Date().getTime() * 0.00005;
  88. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  89. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  90. for( i = 0; i < scene.objects.length; i++ ) {
  91. scene.objects[i].rotation.y = time * ( i < 4 ? i+1 : - (i+1) );
  92. scene.objects[i].updateMatrix();
  93. }
  94. for( i = 0; i < materials.length; i++ ) {
  95. color = parameters[i][0];
  96. h = ( 360 * ( color[0] + time ) % 360 ) / 360;
  97. materials[i].color.setHSV( h, color[1], color[2] );
  98. }
  99. renderer.render( scene, camera );
  100. stats.update();
  101. }
  102. </script>
  103. </body>
  104. </html>
粤ICP备19079148号