webgl_lines_fat.html 7.5 KB

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