webgl_lines_fat.html 7.0 KB

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