webgl_buffergeometry_lines_indexed.html 5.3 KB

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