webgl_lines_fat_raycasting.html 9.8 KB

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