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. <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/Three.js"></script>
  39. <script type="text/javascript" src="js/Detector.js"></script>
  40. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  41. <script type="text/javascript" src="js/Stats.js"></script>
  42. <script type="text/javascript">
  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. camera = new THREE.Camera( 80, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000 );
  58. camera.position.z = 1000;
  59. scene = new THREE.Scene();
  60. var i, line, vector1, vector2, material, p,
  61. 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 ],
  62. [ 3.0, 0xaaaaaa, 0.75, 2 ], [ 3.5, 0xffffff, 0.5, 1 ], [ 4.5, 0xffffff, 0.25, 1 ], [ 5.5, 0xffffff, 0.125, 1 ] ],
  63. geometry = new THREE.Geometry();
  64. for ( i = 0; i < 1500; ++i ) {
  65. vector1 = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  66. vector1.normalize();
  67. vector1.multiplyScalar( r );
  68. vector2 = vector1.clone();
  69. vector2.multiplyScalar( Math.random() * 0.09 + 1 );
  70. geometry.vertices.push( new THREE.Vertex( vector1 ) );
  71. geometry.vertices.push( new THREE.Vertex( vector2 ) );
  72. }
  73. for( i = 0; i < parameters.length; ++i ) {
  74. p = parameters[ i ];
  75. material = new THREE.LineBasicMaterial( { color: p[ 1 ], opacity: p[ 2 ], linewidth: p[ 3 ] } );
  76. line = new THREE.Line( geometry, material, THREE.LinePieces );
  77. line.scale.x = line.scale.y = line.scale.z = p[ 0 ];
  78. line.originalScale = p[ 0 ];
  79. line.rotation.y = Math.random() * Math.PI;
  80. line.updateMatrix();
  81. scene.add( line );
  82. }
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  85. container.appendChild(renderer.domElement);
  86. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  87. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  88. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  89. }
  90. function onDocumentMouseMove(event) {
  91. mouseX = event.clientX - windowHalfX;
  92. mouseY = event.clientY - windowHalfY;
  93. }
  94. function onDocumentTouchStart( event ) {
  95. if(event.touches.length > 1) {
  96. event.preventDefault();
  97. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  98. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  99. }
  100. }
  101. function onDocumentTouchMove( event ) {
  102. if(event.touches.length == 1) {
  103. event.preventDefault();
  104. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  105. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  106. }
  107. }
  108. //
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. render();
  112. }
  113. function render() {
  114. //camera.position.x += ( mouseX - camera.position.x ) * .05;
  115. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  116. camera.updateMatrix();
  117. renderer.render( scene, camera );
  118. var time = new Date().getTime() * 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>