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