webgl_lines_fat.html 6.7 KB

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