webgl_lines_fat.html 7.6 KB

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