webgl_buffergeometry_lines.html 3.3 KB

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