forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRay.js
219 lines (131 loc) · 5.1 KB
/
Ray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @author mr.doob / http://mrdoob.com/
*/
THREE.Ray = function ( origin, direction ) {
this.origin = origin || new THREE.Vector3();
this.direction = direction || new THREE.Vector3();
var precision = 0.0001;
this.setPrecision = function ( value ) {
precision = value;
};
var a = new THREE.Vector3();
var b = new THREE.Vector3();
var c = new THREE.Vector3();
var d = new THREE.Vector3();
var originCopy = new THREE.Vector3();
var directionCopy = new THREE.Vector3();
var vector = new THREE.Vector3();
var normal = new THREE.Vector3();
var intersectPoint = new THREE.Vector3()
this.intersectObject = function ( object ) {
var intersect, intersects = [];
if ( object instanceof THREE.Particle ) {
var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
if ( distance > object.scale.x ) {
return [];
}
intersect = {
distance: distance,
point: object.position,
face: null,
object: object
};
intersects.push( intersect );
} else if ( object instanceof THREE.Mesh ) {
// Checking boundingSphere
var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
return intersects;
}
// Checking faces
var f, fl, face, dot, scalar,
geometry = object.geometry,
vertices = geometry.vertices,
objMatrix;
object.matrixRotationWorld.extractRotation( object.matrixWorld );
for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
face = geometry.faces[ f ];
originCopy.copy( this.origin );
directionCopy.copy( this.direction );
objMatrix = object.matrixWorld;
// determine if ray intersects the plane of the face
// note: this works regardless of the direction of the face normal
vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
dot = directionCopy.dot( normal );
// bail if ray and plane are parallel
if ( Math.abs( dot ) < precision ) continue;
// calc distance to plane
scalar = normal.dot( vector ) / dot;
// if negative distance, then plane is behind ray
if ( scalar < 0 ) continue;
if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
if ( face instanceof THREE.Face3 ) {
a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
if ( pointInFace3( intersectPoint, a, b, c ) ) {
intersect = {
distance: originCopy.distanceTo( intersectPoint ),
point: intersectPoint.clone(),
face: face,
object: object
};
intersects.push( intersect );
}
} else if ( face instanceof THREE.Face4 ) {
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 ] ) );
if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
intersect = {
distance: originCopy.distanceTo( intersectPoint ),
point: intersectPoint.clone(),
face: face,
object: object
};
intersects.push( intersect );
}
}
}
}
}
return intersects;
}
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 ] ) );
}
intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
return intersects;
};
var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
var dot, intersect, distance;
function distanceFromIntersection( origin, direction, position ) {
v0.sub( position, origin );
dot = v0.dot( direction );
intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
distance = position.distanceTo( intersect );
return distance;
}
// http://www.blackpawn.com/texts/pointinpoly/default.html
var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
function pointInFace3( p, a, b, c ) {
v0.sub( c, a );
v1.sub( b, a );
v2.sub( p, 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 );
}
};