Ray.js 3.0 KB
Newer Older
B
Ben Houston 已提交
1 2 3 4
/**
 * @author bhouston / http://exocortex.com
 */

5
THREE.Ray = function ( origin, direction ) {
B
Ben Houston 已提交
6

7 8
	this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3();
	this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3();
B
Ben Houston 已提交
9 10 11

};

12 13 14
THREE.Ray.prototype = {

	constructor: THREE.Ray,
B
Ben Houston 已提交
15

B
Ben Houston 已提交
16
	set: function ( origin, direction ) {
B
Ben Houston 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

		this.origin.copy( origin );
		this.direction.copy( direction );

		return this;

	},

	copy: function ( ray ) {

		this.origin.copy( ray.origin );
		this.direction.copy( ray.direction );

		return this;

	},

34
	at: function( t, optionalTarget ) {
B
Ben Houston 已提交
35

36 37
		var result = optionalTarget || new THREE.Vector3();

38
		return result.copy( this.direction ).multiplyScalar( t ).add( this.origin );
B
Ben Houston 已提交
39 40 41

	},

42
	recast: function() {
43

44
		var v1 = new THREE.Vector3();
45

46
		return function ( t ) {
47

48 49 50 51 52 53 54
			this.origin.copy( this.at( t, v1 ) );

			return this;

		};

	}(),
55

56
	closestPointToPoint: function ( point, optionalTarget ) {
B
Ben Houston 已提交
57

58
		var result = optionalTarget || new THREE.Vector3();
59
		result.subVectors( point, this.origin );
B
Ben Houston 已提交
60 61
		var directionDistance = result.dot( this.direction );

62
		return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
B
Ben Houston 已提交
63 64 65

	},

66
	distanceToPoint: function() {
B
Ben Houston 已提交
67

68
		var v1 = new THREE.Vector3();
B
Ben Houston 已提交
69

70
		return function ( point ) {
B
Ben Houston 已提交
71

72 73 74 75 76 77 78 79
			var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
			v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );

			return v1.distanceTo( point );

		};

	}(),
B
Ben Houston 已提交
80

81 82 83 84 85 86
	isIntersectionSphere: function( sphere ) {

		return ( this.distanceToPoint( sphere.center ) <= sphere.radius );

	},

B
Ben Houston 已提交
87 88
	isIntersectionPlane: function ( plane ) {

B
Ben Houston 已提交
89 90 91 92 93 94 95 96
		// check if the line and plane are non-perpendicular, if they
		// eventually they will intersect.
		var denominator = plane.normal.dot( this.direction );
		if ( denominator != 0 ) {

			return true;

		}
97

B
Ben Houston 已提交
98 99 100 101 102 103 104 105
		// line is coplanar, return origin
		if( plane.distanceToPoint( this.origin ) == 0 ) {

			return true;

		}

		return false;
B
Ben Houston 已提交
106 107 108

	},

109
	distanceToPlane: function ( plane ) {
B
Ben Houston 已提交
110

B
Ben Houston 已提交
111 112
		var denominator = plane.normal.dot( this.direction );
		if ( denominator == 0 ) {
B
Ben Houston 已提交
113

B
Ben Houston 已提交
114 115 116
			// line is coplanar, return origin
			if( plane.distanceToPoint( this.origin ) == 0 ) {

117
				return 0;
B
Ben Houston 已提交
118 119 120

			}

B
Ben Houston 已提交
121 122 123 124 125
			// Unsure if this is the correct method to handle this case.
			return undefined;

		}

126
		var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
127

128 129 130 131
		return t;

	},

132
	intersectPlane: function ( plane, optionalTarget ) {
133 134 135

		var t = this.distanceToPlane( plane );

M
Mr.doob 已提交
136
		if ( t === undefined ) {
137 138 139 140

			return undefined;
		}

141
		return this.at( t, optionalTarget );
B
Ben Houston 已提交
142 143 144

	},

145
	applyMatrix4: function ( matrix4 ) {
146

147 148
		this.direction.add( this.origin ).applyMatrix4( matrix4 );
		this.origin.applyMatrix4( matrix4 );
149
		this.direction.sub( this.origin );
150 151 152 153

		return this;
	},

B
Ben Houston 已提交
154 155 156 157 158 159 160 161
	equals: function ( ray ) {

		return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );

	},

	clone: function () {

162
		return new THREE.Ray().copy( this );
B
Ben Houston 已提交
163 164 165

	}

166
};