webgl_lines_fat.html 7.4 KB

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