webgl_lines_sphere.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - lines - spheres - webgl</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background-color: #000;
  9. margin: 0px;
  10. overflow: hidden;
  11. }
  12. a {
  13. color:#0078ff;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: center;
  23. z-index:100;
  24. }
  25. a {
  26. color: #ff0080;
  27. text-decoration: none;
  28. }
  29. a:hover {
  30. color: #0080ff;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - lines WebGL demo
  37. </div>
  38. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  39. <script type="text/javascript" src="js/Stats.js"></script>
  40. <script type="text/javascript">
  41. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  42. window.onload = init;
  43. var SCREEN_WIDTH = window.innerWidth,
  44. SCREEN_HEIGHT = window.innerHeight,
  45. r = 450,
  46. mouseX = 0, mouseY = 0,
  47. windowHalfX = window.innerWidth / 2,
  48. windowHalfY = window.innerHeight / 2,
  49. camera, scene, renderer,
  50. stats;
  51. function init() {
  52. var container;
  53. container = document.createElement('div');
  54. document.body.appendChild(container);
  55. camera = new THREE.Camera( 80, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000 );
  56. camera.position.z = 1000;
  57. scene = new THREE.Scene();
  58. var i, line, vector1, vector2, material, p,
  59. parameters = [ [ 0.25, 0xff7700, 1, 2 ], [ 0.5, 0xff9900, 1, 1 ], [ 0.75, 0xffaa00, 0.75, 1 ], [ 1, 0xffaa00, 0.5, 1 ], [ 1.25, 0x000833, 0.8, 1 ],
  60. [ 3.0, 0xaaaaaa, 0.75, 2 ], [ 3.5, 0xffffff, 0.5, 1 ], [ 4.5, 0xffffff, 0.25, 1 ], [ 5.5, 0xffffff, 0.125, 1 ] ],
  61. geometry = new THREE.Geometry();
  62. for ( i = 0; i < 1500; ++i ) {
  63. vector1 = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  64. vector1.normalize();
  65. vector1.multiplyScalar( r );
  66. vector2 = vector1.clone();
  67. vector2.multiplyScalar( Math.random() * 0.09 + 1 );
  68. geometry.vertices.push( new THREE.Vertex( vector1 ) );
  69. geometry.vertices.push( new THREE.Vertex( vector2 ) );
  70. }
  71. for( i = 0; i < parameters.length; ++i ) {
  72. p = parameters[ i ];
  73. material = new THREE.LineBasicMaterial( { color: p[ 1 ], opacity: p[ 2 ], linewidth: p[ 3 ] } );
  74. line = new THREE.Line( geometry, material, THREE.LinePieces );
  75. line.scale.x = line.scale.y = line.scale.z = p[ 0 ];
  76. line.originalScale = p[ 0 ];
  77. line.rotation.y = Math.random() * Math.PI;
  78. line.updateMatrix();
  79. scene.addObject( line );
  80. }
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  83. container.appendChild(renderer.domElement);
  84. /*
  85. stats = new Stats();
  86. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. container.appendChild(stats.domElement);
  89. */
  90. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  91. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  92. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  93. loop();
  94. }
  95. function onDocumentMouseMove(event) {
  96. mouseX = event.clientX - windowHalfX;
  97. mouseY = event.clientY - windowHalfY;
  98. }
  99. function onDocumentTouchStart( 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. function onDocumentTouchMove( event ) {
  107. if(event.touches.length == 1) {
  108. event.preventDefault();
  109. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  110. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  111. }
  112. }
  113. //
  114. function loop() {
  115. requestAnimationFrame( loop, renderer.domElement );
  116. //camera.position.x += ( mouseX - camera.position.x ) * .05;
  117. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  118. camera.updateMatrix();
  119. renderer.render( scene, camera );
  120. var time = new Date().getTime() * 0.0001;
  121. for( var i = 0; i<scene.objects.length; i++ ) {
  122. scene.objects[i].rotation.y = time * ( i < 4 ? i+1 : - (i+1) );
  123. if ( i < 5 ) scene.objects[i].scale.x = scene.objects[i].scale.y = scene.objects[i].scale.z = scene.objects[i].originalScale * (i/5+1) * (1 + 0.5 * Math.sin( 7*time ) );
  124. }
  125. //stats.update();
  126. }
  127. </script>
  128. </body>
  129. </html>