webgl_lines_fat.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lines - fat</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 { Line2 } from './jsm/lines/Line2.js';
  18. import { LineMaterial } from './jsm/lines/LineMaterial.js';
  19. import { LineGeometry } from './jsm/lines/LineGeometry.js';
  20. import { GeometryUtils } from './jsm/utils/GeometryUtils.js';
  21. let line, renderer, scene, camera, camera2, controls;
  22. let line1;
  23. let matLine, matLineBasic, matLineDashed;
  24. let stats;
  25. let gui;
  26. // viewport
  27. let insetWidth;
  28. let insetHeight;
  29. init();
  30. animate();
  31. function init() {
  32. renderer = new THREE.WebGLRenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setClearColor( 0x000000, 0.0 );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. document.body.appendChild( renderer.domElement );
  37. scene = new THREE.Scene();
  38. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  39. camera.position.set( - 40, 0, 60 );
  40. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  41. camera2.position.copy( camera.position );
  42. controls = new OrbitControls( camera, renderer.domElement );
  43. controls.minDistance = 10;
  44. controls.maxDistance = 500;
  45. // Position and THREE.Color Data
  46. const positions = [];
  47. const colors = [];
  48. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  49. const spline = new THREE.CatmullRomCurve3( points );
  50. const divisions = Math.round( 12 * points.length );
  51. const point = new THREE.Vector3();
  52. const color = new THREE.Color();
  53. for ( let i = 0, l = divisions; i < l; i ++ ) {
  54. const t = i / l;
  55. spline.getPoint( t, point );
  56. positions.push( point.x, point.y, point.z );
  57. color.setHSL( t, 1.0, 0.5 );
  58. colors.push( color.r, color.g, color.b );
  59. }
  60. // Line2 ( LineGeometry, LineMaterial )
  61. const geometry = new LineGeometry();
  62. geometry.setPositions( positions );
  63. geometry.setColors( colors );
  64. matLine = new LineMaterial( {
  65. color: 0xffffff,
  66. linewidth: 5, // in pixels
  67. vertexColors: true,
  68. //resolution: // to be set by renderer, eventually
  69. dashed: false
  70. } );
  71. line = new Line2( geometry, matLine );
  72. line.computeLineDistances();
  73. line.scale.set( 1, 1, 1 );
  74. scene.add( line );
  75. // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE_STRIP
  76. const geo = new THREE.BufferGeometry();
  77. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  78. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  79. matLineBasic = new THREE.LineBasicMaterial( { vertexColors: true } );
  80. matLineDashed = new THREE.LineDashedMaterial( { vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 } );
  81. line1 = new THREE.Line( geo, matLineBasic );
  82. line1.computeLineDistances();
  83. line1.visible = false;
  84. scene.add( line1 );
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. onWindowResize();
  88. stats = new Stats();
  89. document.body.appendChild( stats.dom );
  90. initGui();
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. insetWidth = window.innerHeight / 4; // square
  97. insetHeight = window.innerHeight / 4;
  98. camera2.aspect = insetWidth / insetHeight;
  99. camera2.updateProjectionMatrix();
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. stats.update();
  104. // main scene
  105. renderer.setClearColor( 0x000000, 0 );
  106. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  107. // renderer will set this eventually
  108. matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  109. renderer.render( scene, camera );
  110. // inset scene
  111. renderer.setClearColor( 0x222222, 1 );
  112. renderer.clearDepth(); // important!
  113. renderer.setScissorTest( true );
  114. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  115. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  116. camera2.position.copy( camera.position );
  117. camera2.quaternion.copy( camera.quaternion );
  118. // renderer will set this eventually
  119. matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
  120. renderer.render( scene, camera2 );
  121. renderer.setScissorTest( false );
  122. }
  123. //
  124. function initGui() {
  125. gui = new GUI();
  126. const param = {
  127. 'line type': 0,
  128. 'width (px)': 5,
  129. 'dashed': false,
  130. 'dash scale': 1,
  131. 'dash / gap': 1
  132. };
  133. gui.add( param, 'line type', { 'LineGeometry': 0, 'gl.LINE': 1 } ).onChange( function ( val ) {
  134. switch ( val ) {
  135. case '0':
  136. line.visible = true;
  137. line1.visible = false;
  138. break;
  139. case '1':
  140. line.visible = false;
  141. line1.visible = true;
  142. break;
  143. }
  144. } );
  145. gui.add( param, 'width (px)', 1, 10 ).onChange( function ( val ) {
  146. matLine.linewidth = val;
  147. } );
  148. gui.add( param, 'dashed' ).onChange( function ( val ) {
  149. matLine.dashed = val;
  150. // dashed is implemented as a defines -- not as a uniform. this could be changed.
  151. // ... or THREE.LineDashedMaterial could be implemented as a separate material
  152. // temporary hack - renderer should do this eventually
  153. if ( val ) matLine.defines.USE_DASH = ""; else delete matLine.defines.USE_DASH;
  154. matLine.needsUpdate = true;
  155. line1.material = val ? matLineDashed : matLineBasic;
  156. } );
  157. gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
  158. matLine.dashScale = val;
  159. matLineDashed.scale = val;
  160. } );
  161. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  162. switch ( val ) {
  163. case '0':
  164. matLine.dashSize = 2;
  165. matLine.gapSize = 1;
  166. matLineDashed.dashSize = 2;
  167. matLineDashed.gapSize = 1;
  168. break;
  169. case '1':
  170. matLine.dashSize = 1;
  171. matLine.gapSize = 1;
  172. matLineDashed.dashSize = 1;
  173. matLineDashed.gapSize = 1;
  174. break;
  175. case '2':
  176. matLine.dashSize = 1;
  177. matLine.gapSize = 2;
  178. matLineDashed.dashSize = 1;
  179. matLineDashed.gapSize = 2;
  180. break;
  181. }
  182. } );
  183. }
  184. </script>
  185. </body>
  186. </html>