webgl_lines_fat.html 7.0 KB

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