webgl_lines_fat_raycasting.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 raycasting</div>
  12. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
  30. import { LineSegments2 } from 'three/addons/lines/LineSegments2.js';
  31. import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js';
  32. import { Line2 } from 'three/addons/lines/Line2.js';
  33. import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
  34. let line, thresholdLine, segments, thresholdSegments;
  35. let renderer, scene, camera, camera2, controls;
  36. let raycaster, sphereInter, sphereOnLine;
  37. let matLine, matThresholdLine;
  38. let stats, gpuPanel;
  39. let gui;
  40. // viewport
  41. let insetWidth;
  42. let insetHeight;
  43. const pointer = new THREE.Vector2( Infinity, Infinity );
  44. init();
  45. animate();
  46. function init() {
  47. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setClearColor( 0x000000, 0.0 );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. scene = new THREE.Scene();
  53. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  54. camera.position.set( - 40, 0, 60 );
  55. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  56. camera2.position.copy( camera.position );
  57. controls = new OrbitControls( camera, renderer.domElement );
  58. controls.minDistance = 10;
  59. controls.maxDistance = 500;
  60. raycaster = new THREE.Raycaster();
  61. raycaster.params.Line2 = {};
  62. raycaster.params.Line2.threshold = 0;
  63. const sphereGeometry = new THREE.SphereGeometry( 0.25 );
  64. const sphereInterMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, depthTest: false } );
  65. const sphereOnLineMaterial = new THREE.MeshBasicMaterial( { color: 0x00ff00, depthTest: false } );
  66. sphereInter = new THREE.Mesh( sphereGeometry, sphereInterMaterial );
  67. sphereOnLine = new THREE.Mesh( sphereGeometry, sphereOnLineMaterial );
  68. sphereInter.visible = false;
  69. sphereOnLine.visible = false;
  70. sphereInter.renderOrder = 10;
  71. sphereOnLine.renderOrder = 10;
  72. scene.add( sphereInter );
  73. scene.add( sphereOnLine );
  74. // Position and THREE.Color Data
  75. const positions = [];
  76. const colors = [];
  77. const points = [];
  78. for ( let i = - 50; i < 50; i ++ ) {
  79. const t = i / 3;
  80. points.push( new THREE.Vector3( t * Math.sin( 2 * t ), t, t * Math.cos( 2 * t ) ) );
  81. }
  82. const spline = new THREE.CatmullRomCurve3( points );
  83. const divisions = Math.round( 3 * points.length );
  84. const point = new THREE.Vector3();
  85. const color = new THREE.Color();
  86. for ( let i = 0, l = divisions; i < l; i ++ ) {
  87. const t = i / l;
  88. spline.getPoint( t, point );
  89. positions.push( point.x, point.y, point.z );
  90. color.setHSL( t, 1.0, 0.5 );
  91. colors.push( color.r, color.g, color.b );
  92. }
  93. const lineGeometry = new LineGeometry();
  94. lineGeometry.setPositions( positions );
  95. lineGeometry.setColors( colors );
  96. const segmentsGeometry = new LineSegmentsGeometry();
  97. segmentsGeometry.setPositions( positions );
  98. segmentsGeometry.setColors( colors );
  99. matLine = new LineMaterial( {
  100. color: 0xffffff,
  101. linewidth: 1, // in world units with size attenuation, pixels otherwise
  102. worldUnits: true,
  103. vertexColors: true,
  104. //resolution: // to be set by renderer, eventually
  105. alphaToCoverage: true,
  106. } );
  107. matThresholdLine = new LineMaterial( {
  108. color: 0xffffff,
  109. linewidth: matLine.linewidth, // in world units with size attenuation, pixels otherwise
  110. worldUnits: true,
  111. // vertexColors: true,
  112. transparent: true,
  113. opacity: 0.2,
  114. depthTest: false,
  115. visible: false,
  116. //resolution: // to be set by renderer, eventually
  117. } );
  118. segments = new LineSegments2( segmentsGeometry, matLine );
  119. segments.computeLineDistances();
  120. segments.scale.set( 1, 1, 1 );
  121. scene.add( segments );
  122. segments.visible = false;
  123. thresholdSegments = new LineSegments2( segmentsGeometry, matThresholdLine );
  124. thresholdSegments.computeLineDistances();
  125. thresholdSegments.scale.set( 1, 1, 1 );
  126. scene.add( thresholdSegments );
  127. thresholdSegments.visible = false;
  128. line = new Line2( lineGeometry, matLine );
  129. line.computeLineDistances();
  130. line.scale.set( 1, 1, 1 );
  131. scene.add( line );
  132. thresholdLine = new Line2( lineGeometry, matThresholdLine );
  133. thresholdLine.computeLineDistances();
  134. thresholdLine.scale.set( 1, 1, 1 );
  135. scene.add( thresholdLine );
  136. const geo = new THREE.BufferGeometry();
  137. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  138. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  139. //
  140. document.addEventListener( 'pointermove', onPointerMove );
  141. window.addEventListener( 'resize', onWindowResize );
  142. onWindowResize();
  143. stats = new Stats();
  144. document.body.appendChild( stats.dom );
  145. gpuPanel = new GPUStatsPanel( renderer.getContext() );
  146. stats.addPanel( gpuPanel );
  147. stats.showPanel( 0 );
  148. initGui();
  149. }
  150. function onWindowResize() {
  151. camera.aspect = window.innerWidth / window.innerHeight;
  152. camera.updateProjectionMatrix();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. insetWidth = window.innerHeight / 4; // square
  155. insetHeight = window.innerHeight / 4;
  156. camera2.aspect = insetWidth / insetHeight;
  157. camera2.updateProjectionMatrix();
  158. }
  159. function onPointerMove( event ) {
  160. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  161. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  162. }
  163. function animate() {
  164. requestAnimationFrame( animate );
  165. stats.update();
  166. // main scene
  167. renderer.setClearColor( 0x000000, 0 );
  168. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  169. raycaster.setFromCamera( pointer, camera );
  170. const obj = line.visible ? line : segments;
  171. const intersects = raycaster.intersectObject( obj, true );
  172. if ( intersects.length > 0 ) {
  173. sphereInter.visible = true;
  174. sphereOnLine.visible = true;
  175. sphereInter.position.copy( intersects[ 0 ].point );
  176. sphereOnLine.position.copy( intersects[ 0 ].pointOnLine );
  177. const i = intersects[ 0 ].faceIndex;
  178. const colors = obj.geometry.getAttribute( 'instanceColorStart' );
  179. const color = new THREE.Color().setRGB( colors.getX( i ), colors.getY( i ), colors.getZ( i ) );
  180. sphereInter.material.color.copy( color.clone().offsetHSL( 0.3, 0, 0 ) );
  181. sphereOnLine.material.color.copy( color.clone().offsetHSL( 0.7, 0, 0 ) );
  182. renderer.domElement.style.cursor = 'crosshair';
  183. } else {
  184. sphereInter.visible = false;
  185. sphereOnLine.visible = false;
  186. renderer.domElement.style.cursor = '';
  187. }
  188. // renderer will set this eventually
  189. matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  190. matThresholdLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  191. gpuPanel.startQuery();
  192. renderer.render( scene, camera );
  193. gpuPanel.endQuery();
  194. // inset scene
  195. renderer.setClearColor( 0x222222, 1 );
  196. renderer.clearDepth(); // important!
  197. renderer.setScissorTest( true );
  198. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  199. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  200. camera2.position.copy( camera.position );
  201. camera2.quaternion.copy( camera.quaternion );
  202. // renderer will set this eventually
  203. matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
  204. renderer.render( scene, camera2 );
  205. renderer.setScissorTest( false );
  206. }
  207. //
  208. function switchLine( val ) {
  209. switch ( val ) {
  210. case 0:
  211. line.visible = true;
  212. thresholdLine.visible = true;
  213. segments.visible = false;
  214. thresholdSegments.visible = false;
  215. break;
  216. case 1:
  217. line.visible = false;
  218. thresholdLine.visible = false;
  219. segments.visible = true;
  220. thresholdSegments.visible = true;
  221. break;
  222. }
  223. }
  224. function initGui() {
  225. gui = new GUI();
  226. const param = {
  227. 'line type': 0,
  228. 'world units': matLine.worldUnits,
  229. 'visualize threshold': matThresholdLine.visible,
  230. 'width': matLine.linewidth,
  231. 'alphaToCoverage': matLine.alphaToCoverage,
  232. 'threshold': raycaster.params.Line2.threshold
  233. };
  234. gui.add( param, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
  235. switchLine( val );
  236. } ).setValue( 1 );
  237. gui.add( param, 'world units' ).onChange( function ( val ) {
  238. matLine.worldUnits = val;
  239. matLine.needsUpdate = true;
  240. matThresholdLine.worldUnits = val;
  241. matThresholdLine.needsUpdate = true;
  242. } );
  243. gui.add( param, 'visualize threshold' ).onChange( function ( val ) {
  244. matThresholdLine.visible = val;
  245. } );
  246. gui.add( param, 'width', 1, 10 ).onChange( function ( val ) {
  247. matLine.linewidth = val;
  248. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  249. } );
  250. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  251. matLine.alphaToCoverage = val;
  252. } );
  253. gui.add( param, 'threshold', 0, 10 ).onChange( function ( val ) {
  254. raycaster.params.Line2.threshold = val;
  255. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  256. } );
  257. }
  258. </script>
  259. </body>
  260. </html>