webgl_buffergeometry_lines.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, timer;
  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. timer = new THREE.Timer();
  37. timer.connect( document );
  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. renderer.setAnimationLoop( animate );
  64. container.appendChild( renderer.domElement );
  65. //
  66. stats = new Stats();
  67. container.appendChild( stats.dom );
  68. //
  69. window.addEventListener( 'resize', onWindowResize );
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. //
  77. function animate() {
  78. timer.update();
  79. const delta = timer.getDelta();
  80. const time = timer.getElapsed();
  81. line.rotation.x = time * 0.25;
  82. line.rotation.y = time * 0.5;
  83. t += delta * 0.5;
  84. line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
  85. renderer.render( scene, camera );
  86. stats.update();
  87. }
  88. function generateMorphTargets( geometry ) {
  89. const data = [];
  90. for ( let i = 0; i < segments; i ++ ) {
  91. const x = Math.random() * r - r / 2;
  92. const y = Math.random() * r - r / 2;
  93. const z = Math.random() * r - r / 2;
  94. data.push( x, y, z );
  95. }
  96. const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
  97. morphTarget.name = 'target1';
  98. geometry.morphAttributes.position = [ morphTarget ];
  99. }
  100. </script>
  101. </body>
  102. </html>
粤ICP备19079148号