webgl_lines_fat.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. var mouse, raycaster, raycastPoint;
  27. // viewport
  28. var insetWidth;
  29. var 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. var positions = [];
  48. var colors = [];
  49. var points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  50. var spline = new THREE.CatmullRomCurve3( points );
  51. var divisions = Math.round( 12 * points.length );
  52. var color = new THREE.Color();
  53. for ( var i = 0, l = divisions; i < l; i ++ ) {
  54. var point = spline.getPoint( i / l );
  55. positions.push( point.x, point.y, point.z );
  56. color.setHSL( i / l, 1.0, 0.5 );
  57. colors.push( color.r, color.g, color.b );
  58. }
  59. // Line2 ( LineGeometry, LineMaterial )
  60. var geometry = new LineGeometry();
  61. geometry.setPositions( positions );
  62. geometry.setColors( colors );
  63. matLine = new LineMaterial( {
  64. color: 0xffffff,
  65. linewidth: 5, // in pixels
  66. vertexColors: THREE.VertexColors,
  67. //resolution: // to be set by renderer, eventually
  68. dashed: false
  69. } );
  70. line = new Line2( geometry, matLine );
  71. line.computeLineDistances();
  72. line.scale.set( 1, 1, 1 );
  73. scene.add( line );
  74. // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicMaterial ) - rendered with gl.LINE_STRIP
  75. var geo = new THREE.BufferGeometry();
  76. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  77. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  78. matLineBasic = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  79. matLineDashed = new THREE.LineDashedMaterial( { vertexColors: THREE.VertexColors, scale: 2, dashSize: 1, gapSize: 1 } );
  80. line1 = new THREE.Line( geo, matLineBasic );
  81. line1.computeLineDistances();
  82. line1.visible = false;
  83. scene.add( line1 );
  84. //
  85. window.addEventListener( 'resize', onWindowResize, false );
  86. onWindowResize();
  87. mouse = new THREE.Vector2();
  88. raycaster = new THREE.Raycaster();
  89. raycastPoint = new THREE.Mesh( new THREE.SphereBufferGeometry( 1, 20, 10 ) );
  90. raycastPoint.material.color.set( 0xff0000 );
  91. raycastPoint.scale.setScalar( 0.25 );
  92. scene.add( raycastPoint );
  93. document.addEventListener( 'mousemove', onMouseMove, false );
  94. stats = new Stats();
  95. document.body.appendChild( stats.dom );
  96. initGui();
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. insetWidth = window.innerHeight / 4; // square
  103. insetHeight = window.innerHeight / 4;
  104. camera2.aspect = insetWidth / insetHeight;
  105. camera2.updateProjectionMatrix();
  106. }
  107. function onMouseMove( e ) {
  108. e.preventDefault();
  109. mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1;
  110. mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
  111. }
  112. function doRaycast() {
  113. camera.updateMatrixWorld();
  114. raycaster.setFromCamera( mouse, camera );
  115. var intersects = raycaster.intersectObjects( [ line, line1 ] );
  116. if ( intersects.length !== 0 ) {
  117. raycastPoint.position.copy( intersects[ 0 ].point );
  118. raycastPoint.visible = true;
  119. } else {
  120. raycastPoint.visible = false;
  121. }
  122. }
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. stats.update();
  126. // main scene
  127. renderer.setClearColor( 0x000000, 0 );
  128. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  129. // renderer will set this eventually
  130. matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  131. // perform raycast after the resolution has been updated for the camera
  132. // view we care about
  133. doRaycast();
  134. renderer.render( scene, camera );
  135. // inset scene
  136. renderer.setClearColor( 0x222222, 1 );
  137. renderer.clearDepth(); // important!
  138. renderer.setScissorTest( true );
  139. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  140. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  141. camera2.position.copy( camera.position );
  142. camera2.quaternion.copy( camera.quaternion );
  143. // renderer will set this eventually
  144. matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
  145. renderer.render( scene, camera2 );
  146. renderer.setScissorTest( false );
  147. }
  148. //
  149. function initGui() {
  150. gui = new GUI();
  151. var param = {
  152. 'line type': 0,
  153. 'width (px)': 5,
  154. 'dashed': false,
  155. 'dash scale': 1,
  156. 'dash / gap': 1
  157. };
  158. gui.add( param, 'line type', { 'LineGeometry': 0, 'gl.LINE': 1 } ).onChange( function ( val ) {
  159. switch ( val ) {
  160. case '0':
  161. line.visible = true;
  162. line1.visible = false;
  163. break;
  164. case '1':
  165. line.visible = false;
  166. line1.visible = true;
  167. break;
  168. }
  169. } );
  170. gui.add( param, 'width (px)', 1, 10 ).onChange( function ( val ) {
  171. matLine.linewidth = val;
  172. } );
  173. gui.add( param, 'dashed' ).onChange( function ( val ) {
  174. matLine.dashed = val;
  175. // dashed is implemented as a defines -- not as a uniform. this could be changed.
  176. // ... or THREE.LineDashedMaterial could be implemented as a separate material
  177. // temporary hack - renderer should do this eventually
  178. if ( val ) matLine.defines.USE_DASH = ""; else delete matLine.defines.USE_DASH;
  179. matLine.needsUpdate = true;
  180. line1.material = val ? matLineDashed : matLineBasic;
  181. } );
  182. gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
  183. matLine.dashScale = val;
  184. matLineDashed.scale = val;
  185. } );
  186. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  187. switch ( val ) {
  188. case '0':
  189. matLine.dashSize = 2;
  190. matLine.gapSize = 1;
  191. matLineDashed.dashSize = 2;
  192. matLineDashed.gapSize = 1;
  193. break;
  194. case '1':
  195. matLine.dashSize = 1;
  196. matLine.gapSize = 1;
  197. matLineDashed.dashSize = 1;
  198. matLineDashed.gapSize = 1;
  199. break;
  200. case '2':
  201. matLine.dashSize = 1;
  202. matLine.gapSize = 2;
  203. matLineDashed.dashSize = 1;
  204. matLineDashed.gapSize = 2;
  205. break;
  206. }
  207. } );
  208. }
  209. </script>
  210. </body>
  211. </html>