|
@@ -9,40 +9,40 @@ const _edge1 = new Vector3();
|
|
const _edge2 = new Vector3();
|
|
const _edge2 = new Vector3();
|
|
const _normal = new Vector3();
|
|
const _normal = new Vector3();
|
|
|
|
|
|
-function Ray( origin, direction ) {
|
|
|
|
|
|
+class Ray {
|
|
|
|
|
|
- this.origin = ( origin !== undefined ) ? origin : new Vector3();
|
|
|
|
- this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
|
|
|
|
|
|
+ constructor( origin, direction ) {
|
|
|
|
|
|
-}
|
|
|
|
|
|
+ this.origin = ( origin !== undefined ) ? origin : new Vector3();
|
|
|
|
+ this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
|
|
|
|
|
|
-Object.assign( Ray.prototype, {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- set: function ( origin, direction ) {
|
|
|
|
|
|
+ set( origin, direction ) {
|
|
|
|
|
|
this.origin.copy( origin );
|
|
this.origin.copy( origin );
|
|
this.direction.copy( direction );
|
|
this.direction.copy( direction );
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- clone: function () {
|
|
|
|
|
|
+ clone() {
|
|
|
|
|
|
return new this.constructor().copy( this );
|
|
return new this.constructor().copy( this );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- copy: function ( ray ) {
|
|
|
|
|
|
+ copy( ray ) {
|
|
|
|
|
|
this.origin.copy( ray.origin );
|
|
this.origin.copy( ray.origin );
|
|
this.direction.copy( ray.direction );
|
|
this.direction.copy( ray.direction );
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- at: function ( t, target ) {
|
|
|
|
|
|
+ at( t, target ) {
|
|
|
|
|
|
if ( target === undefined ) {
|
|
if ( target === undefined ) {
|
|
|
|
|
|
@@ -53,25 +53,25 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
|
|
return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- lookAt: function ( v ) {
|
|
|
|
|
|
+ lookAt( v ) {
|
|
|
|
|
|
this.direction.copy( v ).sub( this.origin ).normalize();
|
|
this.direction.copy( v ).sub( this.origin ).normalize();
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- recast: function ( t ) {
|
|
|
|
|
|
+ recast( t ) {
|
|
|
|
|
|
this.origin.copy( this.at( t, _vector ) );
|
|
this.origin.copy( this.at( t, _vector ) );
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- closestPointToPoint: function ( point, target ) {
|
|
|
|
|
|
+ closestPointToPoint( point, target ) {
|
|
|
|
|
|
if ( target === undefined ) {
|
|
if ( target === undefined ) {
|
|
|
|
|
|
@@ -92,15 +92,15 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
|
|
return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- distanceToPoint: function ( point ) {
|
|
|
|
|
|
+ distanceToPoint( point ) {
|
|
|
|
|
|
return Math.sqrt( this.distanceSqToPoint( point ) );
|
|
return Math.sqrt( this.distanceSqToPoint( point ) );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- distanceSqToPoint: function ( point ) {
|
|
|
|
|
|
+ distanceSqToPoint( point ) {
|
|
|
|
|
|
const directionDistance = _vector.subVectors( point, this.origin ).dot( this.direction );
|
|
const directionDistance = _vector.subVectors( point, this.origin ).dot( this.direction );
|
|
|
|
|
|
@@ -116,9 +116,9 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return _vector.distanceToSquared( point );
|
|
return _vector.distanceToSquared( point );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
|
|
|
|
|
|
+ distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
|
|
|
|
|
|
// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
|
|
// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
|
|
// It returns the min distance between the ray and the segment
|
|
// It returns the min distance between the ray and the segment
|
|
@@ -235,9 +235,9 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return sqrDist;
|
|
return sqrDist;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectSphere: function ( sphere, target ) {
|
|
|
|
|
|
+ intersectSphere( sphere, target ) {
|
|
|
|
|
|
_vector.subVectors( sphere.center, this.origin );
|
|
_vector.subVectors( sphere.center, this.origin );
|
|
const tca = _vector.dot( this.direction );
|
|
const tca = _vector.dot( this.direction );
|
|
@@ -265,15 +265,15 @@ Object.assign( Ray.prototype, {
|
|
// else t0 is in front of the ray, so return the first collision point scaled by t0
|
|
// else t0 is in front of the ray, so return the first collision point scaled by t0
|
|
return this.at( t0, target );
|
|
return this.at( t0, target );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectsSphere: function ( sphere ) {
|
|
|
|
|
|
+ intersectsSphere( sphere ) {
|
|
|
|
|
|
return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
|
|
return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- distanceToPlane: function ( plane ) {
|
|
|
|
|
|
+ distanceToPlane( plane ) {
|
|
|
|
|
|
const denominator = plane.normal.dot( this.direction );
|
|
const denominator = plane.normal.dot( this.direction );
|
|
|
|
|
|
@@ -298,9 +298,9 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return t >= 0 ? t : null;
|
|
return t >= 0 ? t : null;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectPlane: function ( plane, target ) {
|
|
|
|
|
|
+ intersectPlane( plane, target ) {
|
|
|
|
|
|
const t = this.distanceToPlane( plane );
|
|
const t = this.distanceToPlane( plane );
|
|
|
|
|
|
@@ -312,9 +312,9 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return this.at( t, target );
|
|
return this.at( t, target );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectsPlane: function ( plane ) {
|
|
|
|
|
|
+ intersectsPlane( plane ) {
|
|
|
|
|
|
// check if the ray lies on the plane first
|
|
// check if the ray lies on the plane first
|
|
|
|
|
|
@@ -338,9 +338,9 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectBox: function ( box, target ) {
|
|
|
|
|
|
+ intersectBox( box, target ) {
|
|
|
|
|
|
let tmin, tmax, tymin, tymax, tzmin, tzmax;
|
|
let tmin, tmax, tymin, tymax, tzmin, tzmax;
|
|
|
|
|
|
@@ -407,15 +407,15 @@ Object.assign( Ray.prototype, {
|
|
|
|
|
|
return this.at( tmin >= 0 ? tmin : tmax, target );
|
|
return this.at( tmin >= 0 ? tmin : tmax, target );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectsBox: function ( box ) {
|
|
|
|
|
|
+ intersectsBox( box ) {
|
|
|
|
|
|
return this.intersectBox( box, _vector ) !== null;
|
|
return this.intersectBox( box, _vector ) !== null;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- intersectTriangle: function ( a, b, c, backfaceCulling, target ) {
|
|
|
|
|
|
+ intersectTriangle( a, b, c, backfaceCulling, target ) {
|
|
|
|
|
|
// Compute the offset origin, edges, and normal.
|
|
// Compute the offset origin, edges, and normal.
|
|
|
|
|
|
@@ -488,24 +488,24 @@ Object.assign( Ray.prototype, {
|
|
// Ray intersects triangle.
|
|
// Ray intersects triangle.
|
|
return this.at( QdN / DdN, target );
|
|
return this.at( QdN / DdN, target );
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- applyMatrix4: function ( matrix4 ) {
|
|
|
|
|
|
+ applyMatrix4( matrix4 ) {
|
|
|
|
|
|
this.origin.applyMatrix4( matrix4 );
|
|
this.origin.applyMatrix4( matrix4 );
|
|
this.direction.transformDirection( matrix4 );
|
|
this.direction.transformDirection( matrix4 );
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- equals: function ( ray ) {
|
|
|
|
|
|
+ equals( ray ) {
|
|
|
|
|
|
return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
|
|
return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-} );
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
export { Ray };
|
|
export { Ray };
|