webgl_lines_fat_raycasting.html 10 KB

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