webgl_lines_fat_raycasting.html 10.0 KB

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