webgl_buffergeometry_lines.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - lines</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. <style>
  9. a {
  10. color: #08f;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - lines</div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import Stats from './jsm/libs/stats.module.js';
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var line;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.getElementById( 'container' );
  27. //
  28. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
  29. camera.position.z = 2750;
  30. scene = new THREE.Scene();
  31. var segments = 10000;
  32. var geometry = new THREE.BufferGeometry();
  33. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  34. var positions = [];
  35. var colors = [];
  36. var r = 800;
  37. for ( var i = 0; i < segments; i ++ ) {
  38. var x = Math.random() * r - r / 2;
  39. var y = Math.random() * r - r / 2;
  40. var z = Math.random() * r - r / 2;
  41. // positions
  42. positions.push( x, y, z );
  43. // colors
  44. colors.push( ( x / r ) + 0.5 );
  45. colors.push( ( y / r ) + 0.5 );
  46. colors.push( ( z / r ) + 0.5 );
  47. }
  48. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  49. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  50. geometry.computeBoundingSphere();
  51. line = new THREE.Line( geometry, material );
  52. scene.add( line );
  53. //
  54. renderer = new THREE.WebGLRenderer();
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.outputEncoding = THREE.sRGBEncoding;
  58. container.appendChild( renderer.domElement );
  59. //
  60. stats = new Stats();
  61. container.appendChild( stats.dom );
  62. //
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. camera.aspect = window.innerWidth / window.innerHeight;
  67. camera.updateProjectionMatrix();
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. //
  71. function animate() {
  72. requestAnimationFrame( animate );
  73. render();
  74. stats.update();
  75. }
  76. function render() {
  77. var time = Date.now() * 0.001;
  78. line.rotation.x = time * 0.25;
  79. line.rotation.y = time * 0.5;
  80. renderer.render( scene, camera );
  81. }
  82. </script>
  83. </body>
  84. </html>