webgl_lines_fat_raycasting.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. import { GPUStatsPanel } from 'three/addons/utils/GPUStatsPanel.js';
  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, gpuPanel;
  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();
  146. document.body.appendChild( stats.dom );
  147. gpuPanel = new GPUStatsPanel( renderer.getContext() );
  148. stats.addPanel( gpuPanel );
  149. stats.showPanel( 0 );
  150. initGui();
  151. }
  152. function onWindowResize() {
  153. camera.aspect = window.innerWidth / window.innerHeight;
  154. camera.updateProjectionMatrix();
  155. renderer.setSize( window.innerWidth, window.innerHeight );
  156. }
  157. function onPointerMove( event ) {
  158. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  159. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  160. }
  161. function animate() {
  162. const delta = clock.getDelta();
  163. const obj = line.visible ? line : segments;
  164. thresholdLine.position.copy( line.position );
  165. thresholdLine.quaternion.copy( line.quaternion );
  166. thresholdSegments.position.copy( segments.position );
  167. thresholdSegments.quaternion.copy( segments.quaternion );
  168. if ( params.animate ) {
  169. line.rotation.y += delta * 0.1;
  170. segments.rotation.y = line.rotation.y;
  171. }
  172. raycaster.setFromCamera( pointer, camera );
  173. const intersects = raycaster.intersectObject( obj );
  174. if ( intersects.length > 0 ) {
  175. sphereInter.visible = true;
  176. sphereOnLine.visible = true;
  177. sphereInter.position.copy( intersects[ 0 ].point );
  178. sphereOnLine.position.copy( intersects[ 0 ].pointOnLine );
  179. const index = intersects[ 0 ].faceIndex;
  180. const colors = obj.geometry.getAttribute( 'instanceColorStart' );
  181. color.fromBufferAttribute( colors, index );
  182. sphereInter.material.color.copy( color ).offsetHSL( 0.3, 0, 0 );
  183. sphereOnLine.material.color.copy( color ).offsetHSL( 0.7, 0, 0 );
  184. renderer.domElement.style.cursor = 'crosshair';
  185. } else {
  186. sphereInter.visible = false;
  187. sphereOnLine.visible = false;
  188. renderer.domElement.style.cursor = '';
  189. }
  190. gpuPanel.startQuery();
  191. renderer.render( scene, camera );
  192. gpuPanel.endQuery();
  193. stats.update();
  194. }
  195. //
  196. function switchLine( val ) {
  197. switch ( val ) {
  198. case 0:
  199. line.visible = true;
  200. thresholdLine.visible = true;
  201. segments.visible = false;
  202. thresholdSegments.visible = false;
  203. break;
  204. case 1:
  205. line.visible = false;
  206. thresholdLine.visible = false;
  207. segments.visible = true;
  208. thresholdSegments.visible = true;
  209. break;
  210. }
  211. }
  212. function initGui() {
  213. gui = new GUI();
  214. gui.add( params, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
  215. switchLine( val );
  216. } ).setValue( 1 );
  217. gui.add( params, 'world units' ).onChange( function ( val ) {
  218. matLine.worldUnits = val;
  219. matLine.needsUpdate = true;
  220. matThresholdLine.worldUnits = val;
  221. matThresholdLine.needsUpdate = true;
  222. } );
  223. gui.add( params, 'visualize threshold' ).onChange( function ( val ) {
  224. matThresholdLine.visible = val;
  225. } );
  226. gui.add( params, 'width', 1, 10 ).onChange( function ( val ) {
  227. matLine.linewidth = val;
  228. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  229. } );
  230. gui.add( params, 'alphaToCoverage' ).onChange( function ( val ) {
  231. matLine.alphaToCoverage = val;
  232. } );
  233. gui.add( params, 'threshold', 0, 10 ).onChange( function ( val ) {
  234. raycaster.params.Line2.threshold = val;
  235. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  236. } );
  237. gui.add( params, 'translation', 0, 10 ).onChange( function ( val ) {
  238. line.position.x = val;
  239. segments.position.x = val;
  240. } );
  241. gui.add( params, 'animate' );
  242. }
  243. </script>
  244. </body>
  245. </html>