webgl_buffergeometry_lines_indexed.html 5.3 KB

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