2
0

webgl_buffergeometry_lines_indexed.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - lines - indexed</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 - indexed</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;
  16. let camera, scene, renderer;
  17. let parent_node;
  18. init();
  19. animate();
  20. function init() {
  21. container = document.getElementById( 'container' );
  22. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  23. camera.position.z = 9000;
  24. scene = new THREE.Scene();
  25. const geometry = new THREE.BufferGeometry();
  26. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  27. const indices = [];
  28. const positions = [];
  29. const colors = [];
  30. let next_positions_index = 0;
  31. //
  32. const iteration_count = 4;
  33. const rangle = 60 * Math.PI / 180.0;
  34. function add_vertex( v ) {
  35. if ( next_positions_index == 0xffff ) console.error( 'Too many points.' );
  36. positions.push( v.x, v.y, v.z );
  37. colors.push( Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, 1 );
  38. return next_positions_index ++;
  39. }
  40. // simple Koch curve
  41. function snowflake_iteration( p0, p4, depth ) {
  42. if ( -- depth < 0 ) {
  43. const i = next_positions_index - 1; // p0 already there
  44. add_vertex( p4 );
  45. indices.push( i, i + 1 );
  46. return;
  47. }
  48. const v = p4.clone().sub( p0 );
  49. const v_tier = v.clone().multiplyScalar( 1 / 3 );
  50. const p1 = p0.clone().add( v_tier );
  51. const angle = Math.atan2( v.y, v.x ) + rangle;
  52. const length = v_tier.length();
  53. const p2 = p1.clone();
  54. p2.x += Math.cos( angle ) * length;
  55. p2.y += Math.sin( angle ) * length;
  56. const p3 = p0.clone().add( v_tier ).add( v_tier );
  57. snowflake_iteration( p0, p1, depth );
  58. snowflake_iteration( p1, p2, depth );
  59. snowflake_iteration( p2, p3, depth );
  60. snowflake_iteration( p3, p4, depth );
  61. }
  62. function snowflake( points, loop, x_offset ) {
  63. for ( let iteration = 0; iteration != iteration_count; iteration ++ ) {
  64. add_vertex( points[ 0 ] );
  65. for ( let p_index = 0, p_count = points.length - 1; p_index != p_count; p_index ++ ) {
  66. snowflake_iteration( points[ p_index ], points[ p_index + 1 ], iteration );
  67. }
  68. if ( loop ) snowflake_iteration( points[ points.length - 1 ], points[ 0 ], iteration );
  69. // translate input curve for next iteration
  70. for ( let p_index = 0, p_count = points.length; p_index != p_count; p_index ++ ) {
  71. points[ p_index ].x += x_offset;
  72. }
  73. }
  74. }
  75. let y = 0;
  76. snowflake(
  77. [
  78. new THREE.Vector3( 0, y, 0 ),
  79. new THREE.Vector3( 500, y, 0 )
  80. ],
  81. false, 600
  82. );
  83. y += 600;
  84. snowflake(
  85. [
  86. new THREE.Vector3( 0, y, 0 ),
  87. new THREE.Vector3( 250, y + 400, 0 ),
  88. new THREE.Vector3( 500, y, 0 )
  89. ],
  90. true, 600
  91. );
  92. y += 600;
  93. snowflake(
  94. [
  95. new THREE.Vector3( 0, y, 0 ),
  96. new THREE.Vector3( 500, y, 0 ),
  97. new THREE.Vector3( 500, y + 500, 0 ),
  98. new THREE.Vector3( 0, y + 500, 0 )
  99. ],
  100. true, 600
  101. );
  102. y += 1000;
  103. snowflake(
  104. [
  105. new THREE.Vector3( 250, y, 0 ),
  106. new THREE.Vector3( 500, y, 0 ),
  107. new THREE.Vector3( 250, y, 0 ),
  108. new THREE.Vector3( 250, y + 250, 0 ),
  109. new THREE.Vector3( 250, y, 0 ),
  110. new THREE.Vector3( 0, y, 0 ),
  111. new THREE.Vector3( 250, y, 0 ),
  112. new THREE.Vector3( 250, y - 250, 0 ),
  113. new THREE.Vector3( 250, y, 0 )
  114. ],
  115. false, 600
  116. );
  117. //
  118. geometry.setIndex( indices );
  119. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  120. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  121. geometry.computeBoundingSphere();
  122. const lineSegments = new THREE.LineSegments( geometry, material );
  123. lineSegments.position.x -= 1200;
  124. lineSegments.position.y -= 1200;
  125. parent_node = new THREE.Object3D();
  126. parent_node.add( lineSegments );
  127. scene.add( parent_node );
  128. renderer = new THREE.WebGLRenderer();
  129. renderer.setPixelRatio( window.devicePixelRatio );
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. renderer.outputEncoding = THREE.sRGBEncoding;
  132. container.appendChild( renderer.domElement );
  133. //
  134. stats = new Stats();
  135. container.appendChild( stats.dom );
  136. //
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. render();
  148. stats.update();
  149. }
  150. function render() {
  151. const time = Date.now() * 0.001;
  152. parent_node.rotation.z = time * 0.5;
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>