|
@@ -13,285 +13,274 @@ import {
|
|
|
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry.js';
|
|
|
import { LineMaterial } from '../lines/LineMaterial.js';
|
|
|
|
|
|
-var LineSegments2 = function ( geometry, material ) {
|
|
|
+const _start = new Vector3();
|
|
|
+const _end = new Vector3();
|
|
|
|
|
|
- if ( geometry === undefined ) geometry = new LineSegmentsGeometry();
|
|
|
- if ( material === undefined ) material = new LineMaterial( { color: Math.random() * 0xffffff } );
|
|
|
+const _start4 = new Vector4();
|
|
|
+const _end4 = new Vector4();
|
|
|
|
|
|
- Mesh.call( this, geometry, material );
|
|
|
+const _ssOrigin = new Vector4();
|
|
|
+const _ssOrigin3 = new Vector3();
|
|
|
+const _mvMatrix = new Matrix4();
|
|
|
+const _line = new Line3();
|
|
|
+const _closestPoint = new Vector3();
|
|
|
|
|
|
- this.type = 'LineSegments2';
|
|
|
+const _box = new Box3();
|
|
|
+const _sphere = new Sphere();
|
|
|
+const _clipToWorldVector = new Vector4();
|
|
|
|
|
|
-};
|
|
|
+class LineSegments2 extends Mesh {
|
|
|
|
|
|
-LineSegments2.prototype = Object.assign( Object.create( Mesh.prototype ), {
|
|
|
+ constructor( geometry = new LineSegmentsGeometry(), material = new LineMaterial( { color: Math.random() * 0xffffff } ) ) {
|
|
|
|
|
|
- constructor: LineSegments2,
|
|
|
+ super( geometry, material );
|
|
|
|
|
|
- isLineSegments2: true,
|
|
|
+ this.type = 'LineSegments2';
|
|
|
|
|
|
- computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
|
|
|
+ }
|
|
|
|
|
|
- var start = new Vector3();
|
|
|
- var end = new Vector3();
|
|
|
+ // for backwards-compatability, but could be a method of LineSegmentsGeometry...
|
|
|
|
|
|
- return function computeLineDistances() {
|
|
|
+ computeLineDistances() {
|
|
|
|
|
|
- var geometry = this.geometry;
|
|
|
+ const geometry = this.geometry;
|
|
|
|
|
|
- var instanceStart = geometry.attributes.instanceStart;
|
|
|
- var instanceEnd = geometry.attributes.instanceEnd;
|
|
|
- var lineDistances = new Float32Array( 2 * instanceStart.count );
|
|
|
+ const instanceStart = geometry.attributes.instanceStart;
|
|
|
+ const instanceEnd = geometry.attributes.instanceEnd;
|
|
|
+ const lineDistances = new Float32Array( 2 * instanceStart.count );
|
|
|
|
|
|
- for ( var i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
|
|
|
+ for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
|
|
|
|
|
|
- start.fromBufferAttribute( instanceStart, i );
|
|
|
- end.fromBufferAttribute( instanceEnd, i );
|
|
|
+ _start.fromBufferAttribute( instanceStart, i );
|
|
|
+ _end.fromBufferAttribute( instanceEnd, i );
|
|
|
|
|
|
- lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
|
|
|
- lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
|
|
|
+ lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
|
|
|
+ lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- var instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
|
|
|
+ }
|
|
|
|
|
|
- geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
|
|
|
- geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
|
|
|
+ const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
|
|
|
|
|
|
- return this;
|
|
|
+ geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
|
|
|
+ geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
|
|
|
|
|
|
- };
|
|
|
+ return this;
|
|
|
|
|
|
- }() ),
|
|
|
+ }
|
|
|
|
|
|
- raycast: ( function () {
|
|
|
+ raycast( raycaster, intersects ) {
|
|
|
|
|
|
- var start = new Vector4();
|
|
|
- var end = new Vector4();
|
|
|
+ if ( raycaster.camera === null ) {
|
|
|
|
|
|
- var ssOrigin = new Vector4();
|
|
|
- var ssOrigin3 = new Vector3();
|
|
|
- var mvMatrix = new Matrix4();
|
|
|
- var line = new Line3();
|
|
|
- var closestPoint = new Vector3();
|
|
|
+ console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2.' );
|
|
|
|
|
|
- var box = new Box3();
|
|
|
- var sphere = new Sphere();
|
|
|
- var clipToWorldVector = new Vector4();
|
|
|
+ }
|
|
|
|
|
|
- return function raycast( raycaster, intersects ) {
|
|
|
+ const threshold = ( raycaster.params.Line2 !== undefined ) ? raycaster.params.Line2.threshold || 0 : 0;
|
|
|
|
|
|
- if ( raycaster.camera === null ) {
|
|
|
+ const ray = raycaster.ray;
|
|
|
+ const camera = raycaster.camera;
|
|
|
+ const projectionMatrix = camera.projectionMatrix;
|
|
|
|
|
|
- console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2.' );
|
|
|
+ const matrixWorld = this.matrixWorld;
|
|
|
+ const geometry = this.geometry;
|
|
|
+ const material = this.material;
|
|
|
+ const resolution = material.resolution;
|
|
|
+ const lineWidth = material.linewidth + threshold;
|
|
|
|
|
|
- }
|
|
|
+ const instanceStart = geometry.attributes.instanceStart;
|
|
|
+ const instanceEnd = geometry.attributes.instanceEnd;
|
|
|
|
|
|
- var threshold = ( raycaster.params.Line2 !== undefined ) ? raycaster.params.Line2.threshold || 0 : 0;
|
|
|
+ // camera forward is negative
|
|
|
+ const near = - camera.near;
|
|
|
|
|
|
- var ray = raycaster.ray;
|
|
|
- var camera = raycaster.camera;
|
|
|
- var projectionMatrix = camera.projectionMatrix;
|
|
|
+ // clip space is [ - 1, 1 ] so multiply by two to get the full
|
|
|
+ // width in clip space
|
|
|
+ const ssMaxWidth = 2.0 * Math.max( lineWidth / resolution.width, lineWidth / resolution.height );
|
|
|
|
|
|
- var matrixWorld = this.matrixWorld;
|
|
|
- var geometry = this.geometry;
|
|
|
- var material = this.material;
|
|
|
- var resolution = material.resolution;
|
|
|
- var lineWidth = material.linewidth + threshold;
|
|
|
+ //
|
|
|
|
|
|
- var instanceStart = geometry.attributes.instanceStart;
|
|
|
- var instanceEnd = geometry.attributes.instanceEnd;
|
|
|
+ // check if we intersect the sphere bounds
|
|
|
+ if ( geometry.boundingSphere === null ) {
|
|
|
|
|
|
- // camera forward is negative
|
|
|
- var near = - camera.near;
|
|
|
+ geometry.computeBoundingSphere();
|
|
|
|
|
|
- // clip space is [ - 1, 1 ] so multiply by two to get the full
|
|
|
- // width in clip space
|
|
|
- var ssMaxWidth = 2.0 * Math.max( lineWidth / resolution.width, lineWidth / resolution.height );
|
|
|
+ }
|
|
|
|
|
|
- //
|
|
|
+ _sphere.copy( geometry.boundingSphere ).applyMatrix4( matrixWorld );
|
|
|
+ const distanceToSphere = Math.max( camera.near, _sphere.distanceToPoint( ray.origin ) );
|
|
|
|
|
|
- // check if we intersect the sphere bounds
|
|
|
- if ( geometry.boundingSphere === null ) {
|
|
|
+ // get the w component to scale the world space line width
|
|
|
+ _clipToWorldVector.set( 0, 0, - distanceToSphere, 1.0 ).applyMatrix4( camera.projectionMatrix );
|
|
|
+ _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w );
|
|
|
+ _clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
|
|
|
|
|
|
- geometry.computeBoundingSphere();
|
|
|
-
|
|
|
- }
|
|
|
+ // increase the sphere bounds by the worst case line screen space width
|
|
|
+ const sphereMargin = Math.abs( ssMaxWidth / _clipToWorldVector.w ) * 0.5;
|
|
|
+ _sphere.radius += sphereMargin;
|
|
|
|
|
|
- sphere.copy( geometry.boundingSphere ).applyMatrix4( matrixWorld );
|
|
|
- var distanceToSphere = Math.max( camera.near, sphere.distanceToPoint( ray.origin ) );
|
|
|
+ if ( raycaster.ray.intersectsSphere( _sphere ) === false ) {
|
|
|
|
|
|
- // get the w component to scale the world space line width
|
|
|
- clipToWorldVector.set( 0, 0, - distanceToSphere, 1.0 ).applyMatrix4( camera.projectionMatrix );
|
|
|
- clipToWorldVector.multiplyScalar( 1.0 / clipToWorldVector.w );
|
|
|
- clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
|
|
|
+ return;
|
|
|
|
|
|
- // increase the sphere bounds by the worst case line screen space width
|
|
|
- var sphereMargin = Math.abs( ssMaxWidth / clipToWorldVector.w ) * 0.5;
|
|
|
- sphere.radius += sphereMargin;
|
|
|
+ }
|
|
|
|
|
|
- if ( raycaster.ray.intersectsSphere( sphere ) === false ) {
|
|
|
+ //
|
|
|
|
|
|
- return;
|
|
|
+ // check if we intersect the box bounds
|
|
|
+ if ( geometry.boundingBox === null ) {
|
|
|
|
|
|
- }
|
|
|
+ geometry.computeBoundingBox();
|
|
|
|
|
|
- //
|
|
|
+ }
|
|
|
|
|
|
- // check if we intersect the box bounds
|
|
|
- if ( geometry.boundingBox === null ) {
|
|
|
+ _box.copy( geometry.boundingBox ).applyMatrix4( matrixWorld );
|
|
|
+ const distanceToBox = Math.max( camera.near, _box.distanceToPoint( ray.origin ) );
|
|
|
|
|
|
- geometry.computeBoundingBox();
|
|
|
+ // get the w component to scale the world space line width
|
|
|
+ _clipToWorldVector.set( 0, 0, - distanceToBox, 1.0 ).applyMatrix4( camera.projectionMatrix );
|
|
|
+ _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w );
|
|
|
+ _clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
|
|
|
|
|
|
- }
|
|
|
+ // increase the sphere bounds by the worst case line screen space width
|
|
|
+ const boxMargin = Math.abs( ssMaxWidth / _clipToWorldVector.w ) * 0.5;
|
|
|
+ _box.max.x += boxMargin;
|
|
|
+ _box.max.y += boxMargin;
|
|
|
+ _box.max.z += boxMargin;
|
|
|
+ _box.min.x -= boxMargin;
|
|
|
+ _box.min.y -= boxMargin;
|
|
|
+ _box.min.z -= boxMargin;
|
|
|
|
|
|
- box.copy( geometry.boundingBox ).applyMatrix4( matrixWorld );
|
|
|
- var distanceToBox = Math.max( camera.near, box.distanceToPoint( ray.origin ) );
|
|
|
+ if ( raycaster.ray.intersectsBox( _box ) === false ) {
|
|
|
|
|
|
- // get the w component to scale the world space line width
|
|
|
- clipToWorldVector.set( 0, 0, - distanceToBox, 1.0 ).applyMatrix4( camera.projectionMatrix );
|
|
|
- clipToWorldVector.multiplyScalar( 1.0 / clipToWorldVector.w );
|
|
|
- clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
|
|
|
+ return;
|
|
|
|
|
|
- // increase the sphere bounds by the worst case line screen space width
|
|
|
- var boxMargin = Math.abs( ssMaxWidth / clipToWorldVector.w ) * 0.5;
|
|
|
- box.max.x += boxMargin;
|
|
|
- box.max.y += boxMargin;
|
|
|
- box.max.z += boxMargin;
|
|
|
- box.min.x -= boxMargin;
|
|
|
- box.min.y -= boxMargin;
|
|
|
- box.min.z -= boxMargin;
|
|
|
+ }
|
|
|
|
|
|
- if ( raycaster.ray.intersectsBox( box ) === false ) {
|
|
|
+ //
|
|
|
|
|
|
- return;
|
|
|
+ // pick a point 1 unit out along the ray to avoid the ray origin
|
|
|
+ // sitting at the camera origin which will cause "w" to be 0 when
|
|
|
+ // applying the projection matrix.
|
|
|
+ ray.at( 1, _ssOrigin );
|
|
|
|
|
|
- }
|
|
|
+ // ndc space [ - 1.0, 1.0 ]
|
|
|
+ _ssOrigin.w = 1;
|
|
|
+ _ssOrigin.applyMatrix4( camera.matrixWorldInverse );
|
|
|
+ _ssOrigin.applyMatrix4( projectionMatrix );
|
|
|
+ _ssOrigin.multiplyScalar( 1 / _ssOrigin.w );
|
|
|
|
|
|
- //
|
|
|
+ // screen space
|
|
|
+ _ssOrigin.x *= resolution.x / 2;
|
|
|
+ _ssOrigin.y *= resolution.y / 2;
|
|
|
+ _ssOrigin.z = 0;
|
|
|
|
|
|
- // pick a point 1 unit out along the ray to avoid the ray origin
|
|
|
- // sitting at the camera origin which will cause "w" to be 0 when
|
|
|
- // applying the projection matrix.
|
|
|
- ray.at( 1, ssOrigin );
|
|
|
+ _ssOrigin3.copy( _ssOrigin );
|
|
|
|
|
|
- // ndc space [ - 1.0, 1.0 ]
|
|
|
- ssOrigin.w = 1;
|
|
|
- ssOrigin.applyMatrix4( camera.matrixWorldInverse );
|
|
|
- ssOrigin.applyMatrix4( projectionMatrix );
|
|
|
- ssOrigin.multiplyScalar( 1 / ssOrigin.w );
|
|
|
+ _mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
|
|
|
|
|
|
- // screen space
|
|
|
- ssOrigin.x *= resolution.x / 2;
|
|
|
- ssOrigin.y *= resolution.y / 2;
|
|
|
- ssOrigin.z = 0;
|
|
|
+ for ( let i = 0, l = instanceStart.count; i < l; i ++ ) {
|
|
|
|
|
|
- ssOrigin3.copy( ssOrigin );
|
|
|
+ _start4.fromBufferAttribute( instanceStart, i );
|
|
|
+ _end4.fromBufferAttribute( instanceEnd, i );
|
|
|
|
|
|
- mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
|
|
|
+ _start.w = 1;
|
|
|
+ _end.w = 1;
|
|
|
|
|
|
- for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
|
|
|
+ // camera space
|
|
|
+ _start4.applyMatrix4( _mvMatrix );
|
|
|
+ _end4.applyMatrix4( _mvMatrix );
|
|
|
|
|
|
- start.fromBufferAttribute( instanceStart, i );
|
|
|
- end.fromBufferAttribute( instanceEnd, i );
|
|
|
+ // skip the segment if it's entirely behind the camera
|
|
|
+ var isBehindCameraNear = _start4.z > near && _end4.z > near;
|
|
|
+ if ( isBehindCameraNear ) {
|
|
|
|
|
|
- start.w = 1;
|
|
|
- end.w = 1;
|
|
|
+ continue;
|
|
|
|
|
|
- // camera space
|
|
|
- start.applyMatrix4( mvMatrix );
|
|
|
- end.applyMatrix4( mvMatrix );
|
|
|
-
|
|
|
- // skip the segment if it's entirely behind the camera
|
|
|
- var isBehindCameraNear = start.z > near && end.z > near;
|
|
|
- if ( isBehindCameraNear ) {
|
|
|
-
|
|
|
- continue;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // trim the segment if it extends behind camera near
|
|
|
- if ( start.z > near ) {
|
|
|
+ }
|
|
|
|
|
|
- const deltaDist = start.z - end.z;
|
|
|
- const t = ( start.z - near ) / deltaDist;
|
|
|
- start.lerp( end, t );
|
|
|
+ // trim the segment if it extends behind camera near
|
|
|
+ if ( _start4.z > near ) {
|
|
|
|
|
|
- } else if ( end.z > near ) {
|
|
|
+ const deltaDist = _start4.z - _end4.z;
|
|
|
+ const t = ( _start4.z - near ) / deltaDist;
|
|
|
+ _start4.lerp( _end4, t );
|
|
|
|
|
|
- const deltaDist = end.z - start.z;
|
|
|
- const t = ( end.z - near ) / deltaDist;
|
|
|
- end.lerp( start, t );
|
|
|
+ } else if ( _end4.z > near ) {
|
|
|
|
|
|
- }
|
|
|
+ const deltaDist = _end4.z - _start4.z;
|
|
|
+ const t = ( _end4.z - near ) / deltaDist;
|
|
|
+ _end4.lerp( _start4, t );
|
|
|
|
|
|
- // clip space
|
|
|
- start.applyMatrix4( projectionMatrix );
|
|
|
- end.applyMatrix4( projectionMatrix );
|
|
|
+ }
|
|
|
|
|
|
- // ndc space [ - 1.0, 1.0 ]
|
|
|
- start.multiplyScalar( 1 / start.w );
|
|
|
- end.multiplyScalar( 1 / end.w );
|
|
|
+ // clip space
|
|
|
+ _start4.applyMatrix4( projectionMatrix );
|
|
|
+ _end4.applyMatrix4( projectionMatrix );
|
|
|
|
|
|
- // screen space
|
|
|
- start.x *= resolution.x / 2;
|
|
|
- start.y *= resolution.y / 2;
|
|
|
+ // ndc space [ - 1.0, 1.0 ]
|
|
|
+ _start4.multiplyScalar( 1 / _start4.w );
|
|
|
+ _end4.multiplyScalar( 1 / _end4.w );
|
|
|
|
|
|
- end.x *= resolution.x / 2;
|
|
|
- end.y *= resolution.y / 2;
|
|
|
+ // screen space
|
|
|
+ _start4.x *= resolution.x / 2;
|
|
|
+ _start4.y *= resolution.y / 2;
|
|
|
|
|
|
- // create 2d segment
|
|
|
- line.start.copy( start );
|
|
|
- line.start.z = 0;
|
|
|
+ _end4.x *= resolution.x / 2;
|
|
|
+ _end4.y *= resolution.y / 2;
|
|
|
|
|
|
- line.end.copy( end );
|
|
|
- line.end.z = 0;
|
|
|
+ // create 2d segment
|
|
|
+ _line.start.copy( _start4 );
|
|
|
+ _line.start.z = 0;
|
|
|
|
|
|
- // get closest point on ray to segment
|
|
|
- var param = line.closestPointToPointParameter( ssOrigin3, true );
|
|
|
- line.at( param, closestPoint );
|
|
|
+ _line.end.copy( _end4 );
|
|
|
+ _line.end.z = 0;
|
|
|
|
|
|
- // check if the intersection point is within clip space
|
|
|
- var zPos = MathUtils.lerp( start.z, end.z, param );
|
|
|
- var isInClipSpace = zPos >= - 1 && zPos <= 1;
|
|
|
+ // get closest point on ray to segment
|
|
|
+ const param = _line.closestPointToPointParameter( _ssOrigin3, true );
|
|
|
+ _line.at( param, _closestPoint );
|
|
|
|
|
|
- var isInside = ssOrigin3.distanceTo( closestPoint ) < lineWidth * 0.5;
|
|
|
+ // check if the intersection point is within clip space
|
|
|
+ const zPos = MathUtils.lerp( _start4.z, _end4.z, param );
|
|
|
+ const isInClipSpace = zPos >= - 1 && zPos <= 1;
|
|
|
|
|
|
- if ( isInClipSpace && isInside ) {
|
|
|
+ const isInside = _ssOrigin3.distanceTo( _closestPoint ) < lineWidth * 0.5;
|
|
|
|
|
|
- line.start.fromBufferAttribute( instanceStart, i );
|
|
|
- line.end.fromBufferAttribute( instanceEnd, i );
|
|
|
+ if ( isInClipSpace && isInside ) {
|
|
|
|
|
|
- line.start.applyMatrix4( matrixWorld );
|
|
|
- line.end.applyMatrix4( matrixWorld );
|
|
|
+ _line.start.fromBufferAttribute( instanceStart, i );
|
|
|
+ _line.end.fromBufferAttribute( instanceEnd, i );
|
|
|
|
|
|
- var pointOnLine = new Vector3();
|
|
|
- var point = new Vector3();
|
|
|
+ _line.start.applyMatrix4( matrixWorld );
|
|
|
+ _line.end.applyMatrix4( matrixWorld );
|
|
|
|
|
|
- ray.distanceSqToSegment( line.start, line.end, point, pointOnLine );
|
|
|
+ const pointOnLine = new Vector3();
|
|
|
+ const point = new Vector3();
|
|
|
|
|
|
- intersects.push( {
|
|
|
+ ray.distanceSqToSegment( _line.start, _line.end, point, pointOnLine );
|
|
|
|
|
|
- point: point,
|
|
|
- pointOnLine: pointOnLine,
|
|
|
- distance: ray.origin.distanceTo( point ),
|
|
|
+ intersects.push( {
|
|
|
|
|
|
- object: this,
|
|
|
- face: null,
|
|
|
- faceIndex: i,
|
|
|
- uv: null,
|
|
|
- uv2: null,
|
|
|
+ point: point,
|
|
|
+ pointOnLine: pointOnLine,
|
|
|
+ distance: ray.origin.distanceTo( point ),
|
|
|
|
|
|
- } );
|
|
|
+ object: this,
|
|
|
+ face: null,
|
|
|
+ faceIndex: i,
|
|
|
+ uv: null,
|
|
|
+ uv2: null,
|
|
|
|
|
|
- }
|
|
|
+ } );
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- }() )
|
|
|
+}
|
|
|
|
|
|
-} );
|
|
|
+LineSegments2.prototype.LineSegments2 = true;
|
|
|
|
|
|
export { LineSegments2 };
|