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