webgl_buffergeometry_lines.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { Timer } from 'three/addons/misc/Timer.js';
  23. import Stats from 'three/addons/libs/stats.module.js';
  24. let container, stats, timer;
  25. let camera, scene, renderer;
  26. let line;
  27. const segments = 10000;
  28. const r = 800;
  29. let t = 0;
  30. init();
  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. timer = new Timer();
  38. timer.connect( document );
  39. const geometry = new THREE.BufferGeometry();
  40. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  41. const positions = [];
  42. const colors = [];
  43. for ( let i = 0; i < segments; i ++ ) {
  44. const x = Math.random() * r - r / 2;
  45. const y = Math.random() * r - r / 2;
  46. const z = Math.random() * r - r / 2;
  47. // positions
  48. positions.push( x, y, z );
  49. // colors
  50. colors.push( ( x / r ) + 0.5 );
  51. colors.push( ( y / r ) + 0.5 );
  52. colors.push( ( z / r ) + 0.5 );
  53. }
  54. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  55. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  56. generateMorphTargets( geometry );
  57. geometry.computeBoundingSphere();
  58. line = new THREE.Line( geometry, material );
  59. scene.add( line );
  60. //
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. container.appendChild( renderer.domElement );
  66. //
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. //
  78. function animate() {
  79. timer.update();
  80. const delta = timer.getDelta();
  81. const time = timer.getElapsed();
  82. line.rotation.x = time * 0.25;
  83. line.rotation.y = time * 0.5;
  84. t += delta * 0.5;
  85. line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
  86. renderer.render( scene, camera );
  87. stats.update();
  88. }
  89. function generateMorphTargets( geometry ) {
  90. const data = [];
  91. for ( let i = 0; i < segments; i ++ ) {
  92. const x = Math.random() * r - r / 2;
  93. const y = Math.random() * r - r / 2;
  94. const z = Math.random() * r - r / 2;
  95. data.push( x, y, z );
  96. }
  97. const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
  98. morphTarget.name = 'target1';
  99. geometry.morphAttributes.position = [ morphTarget ];
  100. }
  101. </script>
  102. </body>
  103. </html>
粤ICP备19079148号