webgl_lines_fat.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. alphaToCoverage: true,
  71. } );
  72. line = new Line2( geometry, matLine );
  73. line.computeLineDistances();
  74. line.scale.set( 1, 1, 1 );
  75. scene.add( line );
  76. // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE_STRIP
  77. const geo = new THREE.BufferGeometry();
  78. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  79. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  80. matLineBasic = new THREE.LineBasicMaterial( { vertexColors: true } );
  81. matLineDashed = new THREE.LineDashedMaterial( { vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 } );
  82. line1 = new THREE.Line( geo, matLineBasic );
  83. line1.computeLineDistances();
  84. line1.visible = false;
  85. scene.add( line1 );
  86. //
  87. window.addEventListener( 'resize', onWindowResize );
  88. onWindowResize();
  89. stats = new Stats();
  90. document.body.appendChild( stats.dom );
  91. initGui();
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. insetWidth = window.innerHeight / 4; // square
  98. insetHeight = window.innerHeight / 4;
  99. camera2.aspect = insetWidth / insetHeight;
  100. camera2.updateProjectionMatrix();
  101. }
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. stats.update();
  105. // main scene
  106. renderer.setClearColor( 0x000000, 0 );
  107. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  108. // renderer will set this eventually
  109. matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  110. renderer.render( scene, camera );
  111. // inset scene
  112. renderer.setClearColor( 0x222222, 1 );
  113. renderer.clearDepth(); // important!
  114. renderer.setScissorTest( true );
  115. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  116. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  117. camera2.position.copy( camera.position );
  118. camera2.quaternion.copy( camera.quaternion );
  119. // renderer will set this eventually
  120. matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
  121. renderer.render( scene, camera2 );
  122. renderer.setScissorTest( false );
  123. }
  124. //
  125. function initGui() {
  126. gui = new GUI();
  127. const param = {
  128. 'line type': 0,
  129. 'width (px)': 5,
  130. 'alphaToCoverage': true,
  131. 'dashed': false,
  132. 'dash scale': 1,
  133. 'dash / gap': 1
  134. };
  135. gui.add( param, 'line type', { 'LineGeometry': 0, 'gl.LINE': 1 } ).onChange( function ( val ) {
  136. switch ( val ) {
  137. case '0':
  138. line.visible = true;
  139. line1.visible = false;
  140. break;
  141. case '1':
  142. line.visible = false;
  143. line1.visible = true;
  144. break;
  145. }
  146. } );
  147. gui.add( param, 'width (px)', 1, 10 ).onChange( function ( val ) {
  148. matLine.linewidth = val;
  149. } );
  150. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  151. matLine.alphaToCoverage = val;
  152. } );
  153. gui.add( param, 'dashed' ).onChange( function ( val ) {
  154. matLine.dashed = val;
  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>