webgl_lines_fat_raycasting.html 9.3 KB

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