webgl_lines_fat.html 6.9 KB

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