webgl_lines_fat.html 7.6 KB

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