webgl_lines_fat_raycasting.html 9.0 KB

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