webgl_buffergeometry_lines.html 3.7 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. <meta property="og:title" content="three.js webgl - buffergeometry - lines">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_lines.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_lines.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - lines</div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. let container, stats, timer;
  28. let camera, scene, renderer;
  29. let line;
  30. const segments = 10000;
  31. const r = 800;
  32. let t = 0;
  33. init();
  34. function init() {
  35. container = document.getElementById( 'container' );
  36. //
  37. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
  38. camera.position.z = 2750;
  39. scene = new THREE.Scene();
  40. timer = new THREE.Timer();
  41. timer.connect( document );
  42. const geometry = new THREE.BufferGeometry();
  43. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  44. const positions = [];
  45. const colors = [];
  46. for ( let i = 0; i < segments; i ++ ) {
  47. const x = Math.random() * r - r / 2;
  48. const y = Math.random() * r - r / 2;
  49. const z = Math.random() * r - r / 2;
  50. // positions
  51. positions.push( x, y, z );
  52. // colors
  53. colors.push( ( x / r ) + 0.5 );
  54. colors.push( ( y / r ) + 0.5 );
  55. colors.push( ( z / r ) + 0.5 );
  56. }
  57. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  58. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  59. generateMorphTargets( geometry );
  60. geometry.computeBoundingSphere();
  61. line = new THREE.Line( geometry, material );
  62. scene.add( line );
  63. //
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. container.appendChild( renderer.domElement );
  69. //
  70. stats = new Stats();
  71. container.appendChild( stats.dom );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. //
  81. function animate() {
  82. timer.update();
  83. const delta = timer.getDelta();
  84. const time = timer.getElapsed();
  85. line.rotation.x = time * 0.25;
  86. line.rotation.y = time * 0.5;
  87. t += delta * 0.5;
  88. line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
  89. renderer.render( scene, camera );
  90. stats.update();
  91. }
  92. function generateMorphTargets( geometry ) {
  93. const data = [];
  94. for ( let i = 0; i < segments; i ++ ) {
  95. const x = Math.random() * r - r / 2;
  96. const y = Math.random() * r - r / 2;
  97. const z = Math.random() * r - r / 2;
  98. data.push( x, y, z );
  99. }
  100. const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
  101. morphTarget.name = 'target1';
  102. geometry.morphAttributes.position = [ morphTarget ];
  103. }
  104. </script>
  105. </body>
  106. </html>
粤ICP备19079148号