Newer
Older
HuangJiPC / public / static / three / examples / js / lines / LineSegments2.js
@zhangdeliang zhangdeliang on 21 Jun 8 KB update
  1. ( function () {
  2.  
  3. const _start = new THREE.Vector3();
  4.  
  5. const _end = new THREE.Vector3();
  6.  
  7. const _start4 = new THREE.Vector4();
  8.  
  9. const _end4 = new THREE.Vector4();
  10.  
  11. const _ssOrigin = new THREE.Vector4();
  12.  
  13. const _ssOrigin3 = new THREE.Vector3();
  14.  
  15. const _mvMatrix = new THREE.Matrix4();
  16.  
  17. const _line = new THREE.Line3();
  18.  
  19. const _closestPoint = new THREE.Vector3();
  20.  
  21. const _box = new THREE.Box3();
  22.  
  23. const _sphere = new THREE.Sphere();
  24.  
  25. const _clipToWorldVector = new THREE.Vector4();
  26.  
  27. let _ray, _instanceStart, _instanceEnd, _lineWidth; // Returns the margin required to expand by in world space given the distance from the camera,
  28. // line width, resolution, and camera projection
  29.  
  30.  
  31. function getWorldSpaceHalfWidth( camera, distance, resolution ) {
  32.  
  33. // transform into clip space, adjust the x and y values by the pixel width offset, then
  34. // transform back into world space to get world offset. Note clip space is [-1, 1] so full
  35. // width does not need to be halved.
  36. _clipToWorldVector.set( 0, 0, - distance, 1.0 ).applyMatrix4( camera.projectionMatrix );
  37.  
  38. _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w );
  39.  
  40. _clipToWorldVector.x = _lineWidth / resolution.width;
  41. _clipToWorldVector.y = _lineWidth / resolution.height;
  42.  
  43. _clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
  44.  
  45. _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w );
  46.  
  47. return Math.abs( Math.max( _clipToWorldVector.x, _clipToWorldVector.y ) );
  48.  
  49. }
  50.  
  51. function raycastWorldUnits( lineSegments, intersects ) {
  52.  
  53. for ( let i = 0, l = _instanceStart.count; i < l; i ++ ) {
  54.  
  55. _line.start.fromBufferAttribute( _instanceStart, i );
  56.  
  57. _line.end.fromBufferAttribute( _instanceEnd, i );
  58.  
  59. const pointOnLine = new THREE.Vector3();
  60. const point = new THREE.Vector3();
  61.  
  62. _ray.distanceSqToSegment( _line.start, _line.end, point, pointOnLine );
  63.  
  64. const isInside = point.distanceTo( pointOnLine ) < _lineWidth * 0.5;
  65.  
  66. if ( isInside ) {
  67.  
  68. intersects.push( {
  69. point,
  70. pointOnLine,
  71. distance: _ray.origin.distanceTo( point ),
  72. object: lineSegments,
  73. face: null,
  74. faceIndex: i,
  75. uv: null,
  76. uv2: null
  77. } );
  78.  
  79. }
  80.  
  81. }
  82.  
  83. }
  84.  
  85. function raycastScreenSpace( lineSegments, camera, intersects ) {
  86.  
  87. const projectionMatrix = camera.projectionMatrix;
  88. const material = lineSegments.material;
  89. const resolution = material.resolution;
  90. const matrixWorld = lineSegments.matrixWorld;
  91. const geometry = lineSegments.geometry;
  92. const instanceStart = geometry.attributes.instanceStart;
  93. const instanceEnd = geometry.attributes.instanceEnd;
  94. const near = - camera.near; //
  95. // pick a point 1 unit out along the ray to avoid the ray origin
  96. // sitting at the camera origin which will cause "w" to be 0 when
  97. // applying the projection matrix.
  98.  
  99. _ray.at( 1, _ssOrigin ); // ndc space [ - 1.0, 1.0 ]
  100.  
  101.  
  102. _ssOrigin.w = 1;
  103.  
  104. _ssOrigin.applyMatrix4( camera.matrixWorldInverse );
  105.  
  106. _ssOrigin.applyMatrix4( projectionMatrix );
  107.  
  108. _ssOrigin.multiplyScalar( 1 / _ssOrigin.w ); // screen space
  109.  
  110.  
  111. _ssOrigin.x *= resolution.x / 2;
  112. _ssOrigin.y *= resolution.y / 2;
  113. _ssOrigin.z = 0;
  114.  
  115. _ssOrigin3.copy( _ssOrigin );
  116.  
  117. _mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  118.  
  119. for ( let i = 0, l = instanceStart.count; i < l; i ++ ) {
  120.  
  121. _start4.fromBufferAttribute( instanceStart, i );
  122.  
  123. _end4.fromBufferAttribute( instanceEnd, i );
  124.  
  125. _start4.w = 1;
  126. _end4.w = 1; // camera space
  127.  
  128. _start4.applyMatrix4( _mvMatrix );
  129.  
  130. _end4.applyMatrix4( _mvMatrix ); // skip the segment if it's entirely behind the camera
  131.  
  132.  
  133. const isBehindCameraNear = _start4.z > near && _end4.z > near;
  134.  
  135. if ( isBehindCameraNear ) {
  136.  
  137. continue;
  138.  
  139. } // trim the segment if it extends behind camera near
  140.  
  141.  
  142. if ( _start4.z > near ) {
  143.  
  144. const deltaDist = _start4.z - _end4.z;
  145. const t = ( _start4.z - near ) / deltaDist;
  146.  
  147. _start4.lerp( _end4, t );
  148.  
  149. } else if ( _end4.z > near ) {
  150.  
  151. const deltaDist = _end4.z - _start4.z;
  152. const t = ( _end4.z - near ) / deltaDist;
  153.  
  154. _end4.lerp( _start4, t );
  155.  
  156. } // clip space
  157.  
  158.  
  159. _start4.applyMatrix4( projectionMatrix );
  160.  
  161. _end4.applyMatrix4( projectionMatrix ); // ndc space [ - 1.0, 1.0 ]
  162.  
  163.  
  164. _start4.multiplyScalar( 1 / _start4.w );
  165.  
  166. _end4.multiplyScalar( 1 / _end4.w ); // screen space
  167.  
  168.  
  169. _start4.x *= resolution.x / 2;
  170. _start4.y *= resolution.y / 2;
  171. _end4.x *= resolution.x / 2;
  172. _end4.y *= resolution.y / 2; // create 2d segment
  173.  
  174. _line.start.copy( _start4 );
  175.  
  176. _line.start.z = 0;
  177.  
  178. _line.end.copy( _end4 );
  179.  
  180. _line.end.z = 0; // get closest point on ray to segment
  181.  
  182. const param = _line.closestPointToPointParameter( _ssOrigin3, true );
  183.  
  184. _line.at( param, _closestPoint ); // check if the intersection point is within clip space
  185.  
  186.  
  187. const zPos = THREE.MathUtils.lerp( _start4.z, _end4.z, param );
  188. const isInClipSpace = zPos >= - 1 && zPos <= 1;
  189.  
  190. const isInside = _ssOrigin3.distanceTo( _closestPoint ) < _lineWidth * 0.5;
  191.  
  192. if ( isInClipSpace && isInside ) {
  193.  
  194. _line.start.fromBufferAttribute( instanceStart, i );
  195.  
  196. _line.end.fromBufferAttribute( instanceEnd, i );
  197.  
  198. _line.start.applyMatrix4( matrixWorld );
  199.  
  200. _line.end.applyMatrix4( matrixWorld );
  201.  
  202. const pointOnLine = new THREE.Vector3();
  203. const point = new THREE.Vector3();
  204.  
  205. _ray.distanceSqToSegment( _line.start, _line.end, point, pointOnLine );
  206.  
  207. intersects.push( {
  208. point: point,
  209. pointOnLine: pointOnLine,
  210. distance: _ray.origin.distanceTo( point ),
  211. object: lineSegments,
  212. face: null,
  213. faceIndex: i,
  214. uv: null,
  215. uv2: null
  216. } );
  217.  
  218. }
  219.  
  220. }
  221.  
  222. }
  223.  
  224. class LineSegments2 extends THREE.Mesh {
  225.  
  226. constructor( geometry = new THREE.LineSegmentsGeometry(), material = new THREE.LineMaterial( {
  227. color: Math.random() * 0xffffff
  228. } ) ) {
  229.  
  230. super( geometry, material );
  231. this.isLineSegments2 = true;
  232. this.type = 'LineSegments2';
  233.  
  234. } // for backwards-compatibility, but could be a method of THREE.LineSegmentsGeometry...
  235.  
  236.  
  237. computeLineDistances() {
  238.  
  239. const geometry = this.geometry;
  240. const instanceStart = geometry.attributes.instanceStart;
  241. const instanceEnd = geometry.attributes.instanceEnd;
  242. const lineDistances = new Float32Array( 2 * instanceStart.count );
  243.  
  244. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  245.  
  246. _start.fromBufferAttribute( instanceStart, i );
  247.  
  248. _end.fromBufferAttribute( instanceEnd, i );
  249.  
  250. lineDistances[ j ] = j === 0 ? 0 : lineDistances[ j - 1 ];
  251. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  252.  
  253. }
  254.  
  255. const instanceDistanceBuffer = new THREE.InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  256.  
  257. geometry.setAttribute( 'instanceDistanceStart', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  258.  
  259. geometry.setAttribute( 'instanceDistanceEnd', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  260.  
  261. return this;
  262.  
  263. }
  264.  
  265. raycast( raycaster, intersects ) {
  266.  
  267. const worldUnits = this.material.worldUnits;
  268. const camera = raycaster.camera;
  269.  
  270. if ( camera === null && ! worldUnits ) {
  271.  
  272. console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2 while worldUnits is set to false.' );
  273.  
  274. }
  275.  
  276. const threshold = raycaster.params.Line2 !== undefined ? raycaster.params.Line2.threshold || 0 : 0;
  277. _ray = raycaster.ray;
  278. const matrixWorld = this.matrixWorld;
  279. const geometry = this.geometry;
  280. const material = this.material;
  281. _lineWidth = material.linewidth + threshold;
  282. _instanceStart = geometry.attributes.instanceStart;
  283. _instanceEnd = geometry.attributes.instanceEnd; // check if we intersect the sphere bounds
  284.  
  285. if ( geometry.boundingSphere === null ) {
  286.  
  287. geometry.computeBoundingSphere();
  288.  
  289. }
  290.  
  291. _sphere.copy( geometry.boundingSphere ).applyMatrix4( matrixWorld ); // increase the sphere bounds by the worst case line screen space width
  292.  
  293.  
  294. let sphereMargin;
  295.  
  296. if ( worldUnits ) {
  297.  
  298. sphereMargin = _lineWidth * 0.5;
  299.  
  300. } else {
  301.  
  302. const distanceToSphere = Math.max( camera.near, _sphere.distanceToPoint( _ray.origin ) );
  303. sphereMargin = getWorldSpaceHalfWidth( camera, distanceToSphere, material.resolution );
  304.  
  305. }
  306.  
  307. _sphere.radius += sphereMargin;
  308.  
  309. if ( _ray.intersectsSphere( _sphere ) === false ) {
  310.  
  311. return;
  312.  
  313. } // check if we intersect the box bounds
  314.  
  315.  
  316. if ( geometry.boundingBox === null ) {
  317.  
  318. geometry.computeBoundingBox();
  319.  
  320. }
  321.  
  322. _box.copy( geometry.boundingBox ).applyMatrix4( matrixWorld ); // increase the box bounds by the worst case line width
  323.  
  324.  
  325. let boxMargin;
  326.  
  327. if ( worldUnits ) {
  328.  
  329. boxMargin = _lineWidth * 0.5;
  330.  
  331. } else {
  332.  
  333. const distanceToBox = Math.max( camera.near, _box.distanceToPoint( _ray.origin ) );
  334. boxMargin = getWorldSpaceHalfWidth( camera, distanceToBox, material.resolution );
  335.  
  336. }
  337.  
  338. _box.expandByScalar( boxMargin );
  339.  
  340. if ( _ray.intersectsBox( _box ) === false ) {
  341.  
  342. return;
  343.  
  344. }
  345.  
  346. if ( worldUnits ) {
  347.  
  348. raycastWorldUnits( this, intersects );
  349.  
  350. } else {
  351.  
  352. raycastScreenSpace( this, camera, intersects );
  353.  
  354. }
  355.  
  356. }
  357.  
  358. }
  359.  
  360. THREE.LineSegments2 = LineSegments2;
  361.  
  362. } )();