webgl_lines_sphere.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - lines WebGL demo
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. var SCREEN_WIDTH = window.innerWidth,
  16. SCREEN_HEIGHT = window.innerHeight,
  17. r = 450,
  18. mouseY = 0,
  19. windowHalfY = window.innerHeight / 2,
  20. camera, scene, renderer;
  21. init();
  22. animate();
  23. function init() {
  24. camera = new THREE.PerspectiveCamera( 80, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000 );
  25. camera.position.z = 1000;
  26. scene = new THREE.Scene();
  27. var i, line, material, p,
  28. parameters = [[ 0.25, 0xff7700, 1 ], [ 0.5, 0xff9900, 1 ], [ 0.75, 0xffaa00, 0.75 ], [ 1, 0xffaa00, 0.5 ], [ 1.25, 0x000833, 0.8 ],
  29. [ 3.0, 0xaaaaaa, 0.75 ], [ 3.5, 0xffffff, 0.5 ], [ 4.5, 0xffffff, 0.25 ], [ 5.5, 0xffffff, 0.125 ]];
  30. var geometry = createGeometry();
  31. for ( i = 0; i < parameters.length; ++ i ) {
  32. p = parameters[ i ];
  33. material = new THREE.LineBasicMaterial( { color: p[ 1 ], opacity: p[ 2 ] } );
  34. line = new THREE.LineSegments( geometry, material );
  35. line.scale.x = line.scale.y = line.scale.z = p[ 0 ];
  36. line.userData.originalScale = p[ 0 ];
  37. line.rotation.y = Math.random() * Math.PI;
  38. line.updateMatrix();
  39. scene.add( line );
  40. }
  41. renderer = new THREE.WebGLRenderer( { antialias: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  44. document.body.appendChild( renderer.domElement );
  45. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  46. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  47. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  48. //
  49. window.addEventListener( 'resize', onWindowResize, false );
  50. // test geometry swapability
  51. setInterval( function () {
  52. var geometry = createGeometry();
  53. scene.traverse( function ( object ) {
  54. if ( object.isLine ) {
  55. object.geometry.dispose();
  56. object.geometry = geometry;
  57. }
  58. } );
  59. }, 1000 );
  60. }
  61. function createGeometry() {
  62. var geometry = new THREE.BufferGeometry();
  63. var vertices = [];
  64. var vertex = new THREE.Vector3();
  65. for ( var i = 0; i < 1500; i ++ ) {
  66. vertex.x = Math.random() * 2 - 1;
  67. vertex.y = Math.random() * 2 - 1;
  68. vertex.z = Math.random() * 2 - 1;
  69. vertex.normalize();
  70. vertex.multiplyScalar( r );
  71. vertices.push( vertex.x, vertex.y, vertex.z );
  72. vertex.multiplyScalar( Math.random() * 0.09 + 1 );
  73. vertices.push( vertex.x, vertex.y, vertex.z );
  74. }
  75. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  76. return geometry;
  77. }
  78. function onWindowResize() {
  79. windowHalfY = window.innerHeight / 2;
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function onDocumentMouseMove( event ) {
  85. mouseY = event.clientY - windowHalfY;
  86. }
  87. function onDocumentTouchStart( event ) {
  88. if ( event.touches.length > 1 ) {
  89. event.preventDefault();
  90. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  91. }
  92. }
  93. function onDocumentTouchMove( event ) {
  94. if ( event.touches.length == 1 ) {
  95. event.preventDefault();
  96. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  97. }
  98. }
  99. //
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. }
  104. function render() {
  105. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  106. camera.lookAt( scene.position );
  107. renderer.render( scene, camera );
  108. var time = Date.now() * 0.0001;
  109. for ( var i = 0; i < scene.children.length; i ++ ) {
  110. var object = scene.children[ i ];
  111. if ( object.isLine ) {
  112. object.rotation.y = time * ( i < 4 ? ( i + 1 ) : - ( i + 1 ) );
  113. if ( i < 5 ) {
  114. var scale = object.userData.originalScale * ( i / 5 + 1 ) * ( 1 + 0.5 * Math.sin( 7 * time ) );
  115. object.scale.x = object.scale.y = object.scale.z = scale;
  116. }
  117. }
  118. }
  119. }
  120. </script>
  121. </body>
  122. </html>