webgl_buffergeometry_lines.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. function init() {
  31. container = document.getElementById( 'container' );
  32. //
  33. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
  34. camera.position.z = 2750;
  35. scene = new THREE.Scene();
  36. clock = new THREE.Clock();
  37. const geometry = new THREE.BufferGeometry();
  38. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  39. const positions = [];
  40. const colors = [];
  41. for ( let i = 0; i < segments; i ++ ) {
  42. const x = Math.random() * r - r / 2;
  43. const y = Math.random() * r - r / 2;
  44. const z = Math.random() * r - r / 2;
  45. // positions
  46. positions.push( x, y, z );
  47. // colors
  48. colors.push( ( x / r ) + 0.5 );
  49. colors.push( ( y / r ) + 0.5 );
  50. colors.push( ( z / r ) + 0.5 );
  51. }
  52. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  53. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  54. generateMorphTargets( geometry );
  55. geometry.computeBoundingSphere();
  56. line = new THREE.Line( geometry, material );
  57. scene.add( line );
  58. //
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.setAnimationLoop( animate );
  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. const delta = clock.getDelta();
  78. const time = clock.getElapsedTime();
  79. line.rotation.x = time * 0.25;
  80. line.rotation.y = time * 0.5;
  81. t += delta * 0.5;
  82. line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
  83. renderer.render( scene, camera );
  84. stats.update();
  85. }
  86. function generateMorphTargets( geometry ) {
  87. const data = [];
  88. for ( let i = 0; i < segments; i ++ ) {
  89. const x = Math.random() * r - r / 2;
  90. const y = Math.random() * r - r / 2;
  91. const z = Math.random() * r - r / 2;
  92. data.push( x, y, z );
  93. }
  94. const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
  95. morphTarget.name = 'target1';
  96. geometry.morphAttributes.position = [ morphTarget ];
  97. }
  98. </script>
  99. </body>
  100. </html>