webgl_lines_sphere.html 4.4 KB

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