webgl_lines_fat_raycasting.html 11 KB

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