webgl_lines_sphere.html 4.4 KB

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