webgl_lines_fat_wireframe.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lines - fat - wireframe</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">three.js</a> - fat lines</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. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
  26. import { Wireframe } from 'three/addons/lines/Wireframe.js';
  27. import { WireframeGeometry2 } from 'three/addons/lines/WireframeGeometry2.js';
  28. let wireframe, renderer, scene, camera, camera2, controls;
  29. let wireframe1;
  30. let matLine, matLineBasic, matLineDashed;
  31. let stats;
  32. let gui;
  33. // viewport
  34. let insetWidth;
  35. let insetHeight;
  36. init();
  37. function init() {
  38. renderer = new THREE.WebGLRenderer( { antialias: true } );
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.setClearColor( 0x000000, 0.0 );
  42. renderer.setAnimationLoop( animate );
  43. document.body.appendChild( renderer.domElement );
  44. scene = new THREE.Scene();
  45. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.set( - 50, 0, 50 );
  47. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  48. camera2.position.copy( camera.position );
  49. controls = new OrbitControls( camera, renderer.domElement );
  50. controls.minDistance = 10;
  51. controls.maxDistance = 500;
  52. // Wireframe ( WireframeGeometry2, LineMaterial )
  53. let geo = new THREE.IcosahedronGeometry( 20, 1 );
  54. const geometry = new WireframeGeometry2( geo );
  55. matLine = new LineMaterial( {
  56. color: 0x4080ff,
  57. linewidth: 5, // in pixels
  58. dashed: false
  59. } );
  60. wireframe = new Wireframe( geometry, matLine );
  61. wireframe.computeLineDistances();
  62. wireframe.scale.set( 1, 1, 1 );
  63. scene.add( wireframe );
  64. // Line ( THREE.WireframeGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE
  65. geo = new THREE.WireframeGeometry( geo );
  66. matLineBasic = new THREE.LineBasicMaterial( { color: 0x4080ff } );
  67. matLineDashed = new THREE.LineDashedMaterial( { scale: 2, dashSize: 1, gapSize: 1 } );
  68. wireframe1 = new THREE.LineSegments( geo, matLineBasic );
  69. wireframe1.computeLineDistances();
  70. wireframe1.visible = false;
  71. scene.add( wireframe1 );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. onWindowResize();
  75. stats = new Stats();
  76. document.body.appendChild( stats.dom );
  77. initGui();
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. insetWidth = window.innerHeight / 4; // square
  84. insetHeight = window.innerHeight / 4;
  85. camera2.aspect = insetWidth / insetHeight;
  86. camera2.updateProjectionMatrix();
  87. }
  88. function animate() {
  89. // main scene
  90. renderer.setClearColor( 0x000000, 0 );
  91. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  92. renderer.render( scene, camera );
  93. // inset scene
  94. renderer.setClearColor( 0x222222, 1 );
  95. renderer.clearDepth(); // important!
  96. renderer.setScissorTest( true );
  97. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  98. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  99. camera2.position.copy( camera.position );
  100. camera2.quaternion.copy( camera.quaternion );
  101. renderer.render( scene, camera2 );
  102. renderer.setScissorTest( false );
  103. stats.update();
  104. }
  105. //
  106. function initGui() {
  107. gui = new GUI();
  108. const param = {
  109. 'line type': 0,
  110. 'width (px)': 5,
  111. 'dashed': false,
  112. 'dash scale': 1,
  113. 'dash / gap': 1
  114. };
  115. gui.add( param, 'line type', { 'LineGeometry': 0, 'gl.LINE': 1 } ).onChange( function ( val ) {
  116. switch ( val ) {
  117. case 0:
  118. wireframe.visible = true;
  119. wireframe1.visible = false;
  120. break;
  121. case 1:
  122. wireframe.visible = false;
  123. wireframe1.visible = true;
  124. break;
  125. }
  126. } );
  127. gui.add( param, 'width (px)', 1, 10 ).onChange( function ( val ) {
  128. matLine.linewidth = val;
  129. } );
  130. gui.add( param, 'dashed' ).onChange( function ( val ) {
  131. matLine.dashed = val;
  132. // dashed is implemented as a defines -- not as a uniform. this could be changed.
  133. // ... or THREE.LineDashedMaterial could be implemented as a separate material
  134. // temporary hack - renderer should do this eventually
  135. if ( val ) matLine.defines.USE_DASH = ''; else delete matLine.defines.USE_DASH;
  136. matLine.needsUpdate = true;
  137. wireframe1.material = val ? matLineDashed : matLineBasic;
  138. } );
  139. gui.add( param, 'dash scale', 0.5, 1, 0.1 ).onChange( function ( val ) {
  140. matLine.dashScale = val;
  141. matLineDashed.scale = val;
  142. } );
  143. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  144. switch ( val ) {
  145. case 0:
  146. matLine.dashSize = 2;
  147. matLine.gapSize = 1;
  148. matLineDashed.dashSize = 2;
  149. matLineDashed.gapSize = 1;
  150. break;
  151. case 1:
  152. matLine.dashSize = 1;
  153. matLine.gapSize = 1;
  154. matLineDashed.dashSize = 1;
  155. matLineDashed.gapSize = 1;
  156. break;
  157. case 2:
  158. matLine.dashSize = 1;
  159. matLine.gapSize = 2;
  160. matLineDashed.dashSize = 1;
  161. matLineDashed.gapSize = 2;
  162. break;
  163. }
  164. } );
  165. }
  166. </script>
  167. </body>
  168. </html>