webgl_buffergeometry_lines.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - lines</div>
  12. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. let container, stats, clock;
  27. let camera, scene, renderer;
  28. let line;
  29. const segments = 10000;
  30. const r = 800;
  31. let t = 0;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.getElementById( 'container' );
  36. //
  37. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
  38. camera.position.z = 2750;
  39. scene = new THREE.Scene();
  40. clock = new THREE.Clock();
  41. const geometry = new THREE.BufferGeometry();
  42. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  43. const positions = [];
  44. const colors = [];
  45. for ( let i = 0; i < segments; i ++ ) {
  46. const x = Math.random() * r - r / 2;
  47. const y = Math.random() * r - r / 2;
  48. const z = Math.random() * r - r / 2;
  49. // positions
  50. positions.push( x, y, z );
  51. // colors
  52. colors.push( ( x / r ) + 0.5 );
  53. colors.push( ( y / r ) + 0.5 );
  54. colors.push( ( z / r ) + 0.5 );
  55. }
  56. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  57. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  58. generateMorphTargets( geometry );
  59. geometry.computeBoundingSphere();
  60. line = new THREE.Line( geometry, material );
  61. scene.add( line );
  62. //
  63. renderer = new THREE.WebGLRenderer();
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.outputEncoding = THREE.sRGBEncoding;
  67. container.appendChild( renderer.domElement );
  68. //
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. //
  72. window.addEventListener( 'resize', onWindowResize );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. //
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. render();
  83. stats.update();
  84. }
  85. function render() {
  86. const delta = clock.getDelta();
  87. const time = clock.getElapsedTime();
  88. line.rotation.x = time * 0.25;
  89. line.rotation.y = time * 0.5;
  90. t += delta * 0.5;
  91. line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
  92. renderer.render( scene, camera );
  93. }
  94. function generateMorphTargets( geometry ) {
  95. const data = [];
  96. for ( let i = 0; i < segments; i ++ ) {
  97. const x = Math.random() * r - r / 2;
  98. const y = Math.random() * r - r / 2;
  99. const z = Math.random() * r - r / 2;
  100. data.push( x, y, z );
  101. }
  102. const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
  103. morphTarget.name = 'target1';
  104. geometry.morphAttributes.position = [ morphTarget ];
  105. }
  106. </script>
  107. </body>
  108. </html>