1
0

webgl_buffergeometry_lines.html 3.6 KB

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