webgl_lines_fat_raycasting.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // renderer will set this eventually
  171. // set the new resolution before raycasting so it is set correctly
  172. matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  173. matThresholdLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport
  174. const obj = line.visible ? line : segments;
  175. const intersects = raycaster.intersectObject( obj, true );
  176. if ( intersects.length > 0 ) {
  177. sphereInter.visible = true;
  178. sphereOnLine.visible = true;
  179. sphereInter.position.copy( intersects[ 0 ].point );
  180. sphereOnLine.position.copy( intersects[ 0 ].pointOnLine );
  181. const i = intersects[ 0 ].faceIndex;
  182. const colors = obj.geometry.getAttribute( 'instanceColorStart' );
  183. const color = new THREE.Color().setRGB( colors.getX( i ), colors.getY( i ), colors.getZ( i ) );
  184. sphereInter.material.color.copy( color.clone().offsetHSL( 0.3, 0, 0 ) );
  185. sphereOnLine.material.color.copy( color.clone().offsetHSL( 0.7, 0, 0 ) );
  186. renderer.domElement.style.cursor = 'crosshair';
  187. } else {
  188. sphereInter.visible = false;
  189. sphereOnLine.visible = false;
  190. renderer.domElement.style.cursor = '';
  191. }
  192. gpuPanel.startQuery();
  193. renderer.render( scene, camera );
  194. gpuPanel.endQuery();
  195. // inset scene
  196. renderer.setClearColor( 0x222222, 1 );
  197. renderer.clearDepth(); // important!
  198. renderer.setScissorTest( true );
  199. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  200. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  201. camera2.position.copy( camera.position );
  202. camera2.quaternion.copy( camera.quaternion );
  203. // renderer will set this eventually
  204. matLine.resolution.set( insetWidth, insetHeight ); // resolution of the inset viewport
  205. renderer.render( scene, camera2 );
  206. renderer.setScissorTest( false );
  207. }
  208. //
  209. function switchLine( val ) {
  210. switch ( val ) {
  211. case 0:
  212. line.visible = true;
  213. thresholdLine.visible = true;
  214. segments.visible = false;
  215. thresholdSegments.visible = false;
  216. break;
  217. case 1:
  218. line.visible = false;
  219. thresholdLine.visible = false;
  220. segments.visible = true;
  221. thresholdSegments.visible = true;
  222. break;
  223. }
  224. }
  225. function initGui() {
  226. gui = new GUI();
  227. const param = {
  228. 'line type': 0,
  229. 'world units': matLine.worldUnits,
  230. 'visualize threshold': matThresholdLine.visible,
  231. 'width': matLine.linewidth,
  232. 'alphaToCoverage': matLine.alphaToCoverage,
  233. 'threshold': raycaster.params.Line2.threshold
  234. };
  235. gui.add( param, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
  236. switchLine( val );
  237. } ).setValue( 1 );
  238. gui.add( param, 'world units' ).onChange( function ( val ) {
  239. matLine.worldUnits = val;
  240. matLine.needsUpdate = true;
  241. matThresholdLine.worldUnits = val;
  242. matThresholdLine.needsUpdate = true;
  243. } );
  244. gui.add( param, 'visualize threshold' ).onChange( function ( val ) {
  245. matThresholdLine.visible = val;
  246. } );
  247. gui.add( param, 'width', 1, 10 ).onChange( function ( val ) {
  248. matLine.linewidth = val;
  249. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  250. } );
  251. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  252. matLine.alphaToCoverage = val;
  253. } );
  254. gui.add( param, 'threshold', 0, 10 ).onChange( function ( val ) {
  255. raycaster.params.Line2.threshold = val;
  256. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  257. } );
  258. }
  259. </script>
  260. </body>
  261. </html>