Ray.js 6.2 KB
Newer Older
1 2 3 4
/**
 * @author mr.doob / http://mrdoob.com/
 */

N
niklassa 已提交
5
THREE.Ray = function ( origin, direction, near, far ) {
6 7 8

	this.origin = origin || new THREE.Vector3();
	this.direction = direction || new THREE.Vector3();
N
niklassa 已提交
9 10
	this.near = near || 0;
	this.far = far || Infinity;
11

12 13 14 15 16 17 18 19
	var precision = 0.0001;

	this.setPrecision = function ( value ) {

		precision = value;

	};

M
Mr.doob 已提交
20 21 22 23
	var a = new THREE.Vector3();
	var b = new THREE.Vector3();
	var c = new THREE.Vector3();
	var d = new THREE.Vector3();
M
Mr.doob 已提交
24

25 26 27
	var originCopy = new THREE.Vector3();
	var directionCopy = new THREE.Vector3();

28
	var vector = new THREE.Vector3();
M
Mr.doob 已提交
29
	var normal = new THREE.Vector3();
M
Mr.doob 已提交
30
	var intersectPoint = new THREE.Vector3()
31 32 33 34 35 36
	
	var descSort = function ( a, b ) {
	
			return a.distance - b.distance;
			
	};
M
Mr.doob 已提交
37

M
Mr.doob 已提交
38
	this.intersectObject = function ( object ) {
M
Mr.doob 已提交
39

M
Mr.doob 已提交
40 41
		var intersect, intersects = [];

42
		if ( object instanceof THREE.Particle ) {
43

44 45 46 47 48 49

			var distance; 
			
			// Checking range, note that distance may be actually greater then this.range,
			// because we also consider object.scale.x
			
N
niklassa 已提交
50 51 52 53
			if ( this.far < Infinity || this.near > 0 ) {
				distance = vector.sub(this.origin, object.matrixWorld.getPosition()).length();
				if ( distance > object.scale.x + this.far ) return [];
				if ( distance < -object.scale.x + this.near ) return [];
54 55 56
			}
			
			distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
57

M
Mr.doob 已提交
58
			if ( distance > object.scale.x ) {
59

60
				return [];
61

62
			}
63

M
Mr.doob 已提交
64
			intersect = {
65

66 67 68 69
				distance: distance,
				point: object.position,
				face: null,
				object: object
70

M
Mr.doob 已提交
71 72 73
			};

			intersects.push( intersect );
74

75
		} else if ( object instanceof THREE.Mesh ) {
76

77

78
			// Checking boundingSphere
M
r47  
Mr.doob 已提交
79
			var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
80 81
			var scaledRadius = object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ); 
			var distance;
82

83
			// Checking distance to origin
N
niklassa 已提交
84
			if ( this.far < Infinity || this.near > 0 ) {
85 86
			
				distance = vector.sub( object.matrixWorld.getPosition(), this.origin ).length();
N
niklassa 已提交
87 88
				if ( distance > scaledRadius + this.far ) return intersects;
				if ( distance < -scaledRadius + this.near ) return intersects;
89 90 91 92 93 94 95 96 97
				
			}
			

			// Checking distance to ray
			
			distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );

			if ( distance > scaledRadius) {
98

M
Mr.doob 已提交
99
				return intersects;
100 101 102 103 104

			}

			// Checking faces

105
			var f, fl, face, dot, scalar,
106
			rangeSq = this.range*this.range,
107 108
			geometry = object.geometry,
			vertices = geometry.vertices,
M
Mr.doob 已提交
109
			objMatrix;
110

M
Mr.doob 已提交
111 112
			object.matrixRotationWorld.extractRotation( object.matrixWorld );

113
			for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
114

115
				face = geometry.faces[ f ];
116

117 118
				originCopy.copy( this.origin );
				directionCopy.copy( this.direction );
119

120
				objMatrix = object.matrixWorld;
121

122 123
				// determine if ray intersects the plane of the face
				// note: this works regardless of the direction of the face normal
124

125
				vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
126 127
				normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
				dot = directionCopy.dot( normal );
128

129
				// bail if ray and plane are parallel
130

131
				if ( Math.abs( dot ) < precision ) continue;
132

133
				// calc distance to plane
134

135
				scalar = normal.dot( vector ) / dot;
136

137 138 139 140 141
				// if negative distance, then plane is behind ray

				if ( scalar < 0 ) continue;

				if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
142

143
					intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
144

N
niklassa 已提交
145 146 147 148
					// Checking distance to origin (would be calculated anyway)
					distance = originCopy.distanceTo( intersectPoint );
					if ( distance > this.far ) continue;
					if ( distance < this.near ) continue;
149

150 151
					if ( face instanceof THREE.Face3 ) {

152 153 154
						a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
						b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
						c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
155

156 157 158 159
						if ( pointInFace3( intersectPoint, a, b, c ) ) {

							intersect = {

N
niklassa 已提交
160
								distance: distance,
A
alteredq 已提交
161
								point: intersectPoint.clone(),
162 163 164 165
								face: face,
								object: object

							};
166

167
							intersects.push( intersect );
168

169
						}
170

171
					} else if ( face instanceof THREE.Face4 ) {
172

173 174 175 176
						a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
						b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
						c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
						d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ] ) );
177

178
						if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
179

180
							intersect = {
181

N
niklassa 已提交
182
								distance: distance,
A
alteredq 已提交
183
								point: intersectPoint.clone(),
184 185 186 187 188 189 190 191
								face: face,
								object: object

							};

							intersects.push( intersect );

						}
192 193 194 195 196 197

					}

				}

			}
198

M
Mr.doob 已提交
199
		}
200

201 202
		intersects.sort( descSort );

M
Mr.doob 已提交
203
		return intersects;
204

205
	}
206

M
Mr.doob 已提交
207 208 209 210 211 212 213 214 215 216
	this.intersectObjects = function ( objects ) {

		var intersects = [];

		for ( var i = 0, l = objects.length; i < l; i ++ ) {

			Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );

		}

217
		intersects.sort( descSort );
M
Mr.doob 已提交
218 219 220 221 222

		return intersects;

	};

223 224
	var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
	var dot, intersect, distance;
M
Mr.doob 已提交
225

226
	function distanceFromIntersection( origin, direction, position ) {
M
Mr.doob 已提交
227

M
Mr.doob 已提交
228
		v0.sub( position, origin );
229
		dot = v0.dot( direction );
M
Mr.doob 已提交
230

M
Mr.doob 已提交
231
		intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
M
Mr.doob 已提交
232 233 234 235 236 237 238 239
		distance = position.distanceTo( intersect );

		return distance;

	}

	// http://www.blackpawn.com/texts/pointinpoly/default.html

A
alteredq 已提交
240
	var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
M
Mr.doob 已提交
241 242 243

	function pointInFace3( p, a, b, c ) {

M
Mr.doob 已提交
244 245 246
		v0.sub( c, a );
		v1.sub( b, a );
		v2.sub( p, a );
M
Mr.doob 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

		dot00 = v0.dot( v0 );
		dot01 = v0.dot( v1 );
		dot02 = v0.dot( v2 );
		dot11 = v1.dot( v1 );
		dot12 = v1.dot( v2 );

		invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
		u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
		v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;

		return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );

	}

262
};