webgl_buffergeometry_lines.html 3.4 KB

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