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

5
THREE.Ray = function ( origin, direction ) {
6 7 8 9 10 11 12 13

	this.origin = origin || new THREE.Vector3();
	this.direction = direction || new THREE.Vector3();

}

THREE.Ray.prototype = {

14 15
	constructor: THREE.Ray,

16
	intersectScene: function ( scene ) {
17

18
		return this.intersectObjects( scene.objects );
19 20 21

	},

22
	intersectObjects: function ( objects ) {
23

24 25 26
		var i, l, object,
		intersects = [];

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

29
			Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
30 31 32 33 34 35 36

		}

		intersects.sort( function ( a, b ) { return a.distance - b.distance; } );

		return intersects;

37
	},
38

39
	intersectObject: function ( object ) {
40

M
Mr.doob 已提交
41 42 43 44 45 46 47 48
		var intersect, intersects = [];

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

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

		}

49
		if ( object instanceof THREE.Particle ) {
50

51
			var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
52

53
			if ( distance == null || distance > object.scale.x ) {
54

55
				return [];
56

57
			}
58

M
Mr.doob 已提交
59
			intersect = {
60

61 62 63 64
				distance: distance,
				point: object.position,
				face: null,
				object: object
65

M
Mr.doob 已提交
66 67 68
			};

			intersects.push( intersect );
69

70
		} else if ( object instanceof THREE.Mesh ) {
71

72 73
			// Checking boundingSphere

74
			var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
75

76
			if ( distance == null || distance > object.geometry.boundingSphere.radius * Math.max( object.scale.x, Math.max( object.scale.y, object.scale.z ) ) ) {
77

M
Mr.doob 已提交
78
				return intersects;
79 80 81 82 83

			}

			// Checking faces

84 85 86
			var f, fl, face,
			a, b, c, d, normal,
			vector, dot, scalar,
87 88 89 90 91
			origin, direction,
			geometry = object.geometry,
			vertices = geometry.vertices,
			objMatrix,
			intersectPoint;
92

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

95
				face = geometry.faces[ f ];
96

97 98
				origin = this.origin.clone();
				direction = this.direction.clone();
99

100
				objMatrix = object.matrixWorld;
101

102 103 104 105 106
				// check if face.centroid is behind the origin

				vector = objMatrix.multiplyVector3( face.centroid.clone() ).subSelf( origin );
				dot = vector.dot( direction );

M
Mr.doob 已提交
107
				if ( dot <= 0 ) continue;
108 109 110

				//

111 112 113 114
				a = objMatrix.multiplyVector3( vertices[ face.a ].position.clone() );
				b = objMatrix.multiplyVector3( vertices[ face.b ].position.clone() );
				c = objMatrix.multiplyVector3( vertices[ face.c ].position.clone() );
				d = face instanceof THREE.Face4 ? objMatrix.multiplyVector3( vertices[ face.d ].position.clone() ) : null;
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
				normal = object.matrixRotationWorld.multiplyVector3( face.normal.clone() );
				dot = direction.dot( normal );

				if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) { // Math.abs( dot ) > 0.0001

					scalar = normal.dot( new THREE.Vector3().sub( a, origin ) ) / dot;
					intersectPoint = origin.addSelf( direction.multiplyScalar( scalar ) );

					if ( face instanceof THREE.Face3 ) {

						if ( pointInFace3( intersectPoint, a, b, c ) ) {

							intersect = {

								distance: this.origin.distanceTo( intersectPoint ),
								point: intersectPoint,
								face: face,
								object: object

							};
136

137
							intersects.push( intersect );
138

139
						}
140

141
					} else if ( face instanceof THREE.Face4 ) {
142

143
						if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
144

145
							intersect = {
146

147 148 149 150 151 152 153 154 155 156
								distance: this.origin.distanceTo( intersectPoint ),
								point: intersectPoint,
								face: face,
								object: object

							};

							intersects.push( intersect );

						}
157 158 159 160 161 162

					}

				}

			}
163

M
Mr.doob 已提交
164
		}
165

M
Mr.doob 已提交
166
		return intersects;
167

M
Mr.doob 已提交
168
		//
169

170
		function distanceFromIntersection( origin, direction, position ) {
171

172
			var vector, dot, intersect, distance;
173

174
			vector = position.clone().subSelf( origin );
175 176
			dot = vector.dot( direction );

M
Mr.doob 已提交
177
			if ( dot <= 0 ) return null; // check if position behind origin.
178

179
			intersect = origin.clone().addSelf( direction.clone().multiplyScalar( dot ) );
180
			distance = position.distanceTo( intersect );
181 182 183 184

			return distance;

		}
185

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

188 189 190 191 192 193 194 195 196 197 198 199 200
		function pointInFace3( p, a, b, c ) {

			var v0 = c.clone().subSelf( a ), v1 = b.clone().subSelf( a ), v2 = p.clone().subSelf( a ),
			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 );

		}

201
	}
202 203

};