@@ -7,19 +7,13 @@ THREE.Ray = function ( origin, direction ) {
7
7
this . origin = origin || new THREE . Vector3 ( ) ;
8
8
this . direction = direction || new THREE . Vector3 ( ) ;
9
9
10
- }
11
-
12
- THREE . Ray . prototype = {
13
-
14
- constructor : THREE . Ray ,
15
-
16
- intersectScene : function ( scene ) {
10
+ this . intersectScene = function ( scene ) {
17
11
18
12
return this . intersectObjects ( scene . children ) ;
19
13
20
- } ,
14
+ } ;
21
15
22
- intersectObjects : function ( objects ) {
16
+ this . intersectObjects = function ( objects ) {
23
17
24
18
var i , l , object ,
25
19
intersects = [ ] ;
@@ -34,52 +28,18 @@ THREE.Ray.prototype = {
34
28
35
29
return intersects ;
36
30
37
- } ,
38
-
39
- intersectObject : function ( object ) {
40
-
41
- function distanceFromIntersection ( origin , direction , position ) {
42
-
43
- var vector , dot , intersect , distance ;
44
-
45
- vector = position . clone ( ) . subSelf ( origin ) ;
46
- dot = vector . dot ( direction ) ;
47
-
48
- if ( dot <= 0 ) return null ; // check if position behind origin.
49
-
50
- intersect = origin . clone ( ) . addSelf ( direction . clone ( ) . multiplyScalar ( dot ) ) ;
51
- distance = position . distanceTo ( intersect ) ;
52
-
53
- return distance ;
54
-
55
- }
56
-
57
- // http://www.blackpawn.com/texts/pointinpoly/default.html
58
-
59
- var v0 = new THREE . Vector3 ( ) , v1 = new THREE . Vector3 ( ) , v2 = new THREE . Vector3 ( ) ;
60
- var dot00 , dot01 , dot02 , dot11 , dot12 , invDenom , u , v ;
61
-
62
- function pointInFace3 ( p , a , b , c ) {
63
-
64
- v0 . copy ( c ) . subSelf ( a ) ;
65
- v1 . copy ( b ) . subSelf ( a ) ;
66
- v2 . copy ( p ) . subSelf ( a ) ;
67
-
68
- dot00 = v0 . dot ( v0 ) ;
69
- dot01 = v0 . dot ( v1 ) ;
70
- dot02 = v0 . dot ( v2 ) ;
71
- dot11 = v1 . dot ( v1 ) ;
72
- dot12 = v1 . dot ( v2 ) ;
73
-
74
- invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 ) ;
75
- u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom ;
76
- v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom ;
31
+ } ;
77
32
78
- return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 ) ;
33
+ var a = new THREE . Vector3 ( ) ;
34
+ var b = new THREE . Vector3 ( ) ;
35
+ var c = new THREE . Vector3 ( ) ;
36
+ var d = new THREE . Vector3 ( ) ;
79
37
80
- }
38
+ var origin = new THREE . Vector3 ( ) ;
39
+ var direction = new THREE . Vector3 ( ) ;
40
+ var normal = new THREE . Vector3 ( ) ;
81
41
82
- //
42
+ this . intersectObject = function ( object ) {
83
43
84
44
var intersect , intersects = [ ] ;
85
45
@@ -125,7 +85,6 @@ THREE.Ray.prototype = {
125
85
// Checking faces
126
86
127
87
var f , fl , face ,
128
- a , b , c , d , normal ,
129
88
vector , dot , scalar ,
130
89
origin , direction ,
131
90
geometry = object . geometry ,
@@ -153,12 +112,12 @@ THREE.Ray.prototype = {
153
112
154
113
//
155
114
156
- a = objMatrix . multiplyVector3 ( vertices [ face . a ] . position . clone ( ) ) ;
157
- b = objMatrix . multiplyVector3 ( vertices [ face . b ] . position . clone ( ) ) ;
158
- c = objMatrix . multiplyVector3 ( vertices [ face . c ] . position . clone ( ) ) ;
159
- d = face instanceof THREE . Face4 ? objMatrix . multiplyVector3 ( vertices [ face . d ] . position . clone ( ) ) : null ;
115
+ a = objMatrix . multiplyVector3 ( a . copy ( vertices [ face . a ] . position ) ) ;
116
+ b = objMatrix . multiplyVector3 ( b . copy ( vertices [ face . b ] . position ) ) ;
117
+ c = objMatrix . multiplyVector3 ( c . copy ( vertices [ face . c ] . position ) ) ;
118
+ d = face instanceof THREE . Face4 ? objMatrix . multiplyVector3 ( d . copy ( vertices [ face . d ] . position ) ) : null ;
160
119
161
- normal = object . matrixRotationWorld . multiplyVector3 ( face . normal . clone ( ) ) ;
120
+ normal = object . matrixRotationWorld . multiplyVector3 ( normal . copy ( face . normal ) ) ;
162
121
dot = direction . dot ( normal ) ;
163
122
164
123
if ( object . doubleSided || ( object . flipSided ? dot > 0 : dot < 0 ) ) { // Math.abs( dot ) > 0.0001
@@ -212,4 +171,45 @@ THREE.Ray.prototype = {
212
171
213
172
}
214
173
174
+ function distanceFromIntersection ( origin , direction , position ) {
175
+
176
+ var vector , dot , intersect , distance ;
177
+
178
+ vector = position . clone ( ) . subSelf ( origin ) ;
179
+ dot = vector . dot ( direction ) ;
180
+
181
+ if ( dot <= 0 ) return null ; // check if position behind origin.
182
+
183
+ intersect = origin . clone ( ) . addSelf ( direction . clone ( ) . multiplyScalar ( dot ) ) ;
184
+ distance = position . distanceTo ( intersect ) ;
185
+
186
+ return distance ;
187
+
188
+ }
189
+
190
+ // http://www.blackpawn.com/texts/pointinpoly/default.html
191
+
192
+ var v0 = new THREE . Vector3 ( ) , v1 = new THREE . Vector3 ( ) , v2 = new THREE . Vector3 ( ) ;
193
+ var dot00 , dot01 , dot02 , dot11 , dot12 , invDenom , u , v ;
194
+
195
+ function pointInFace3 ( p , a , b , c ) {
196
+
197
+ v0 . copy ( c ) . subSelf ( a ) ;
198
+ v1 . copy ( b ) . subSelf ( a ) ;
199
+ v2 . copy ( p ) . subSelf ( a ) ;
200
+
201
+ dot00 = v0 . dot ( v0 ) ;
202
+ dot01 = v0 . dot ( v1 ) ;
203
+ dot02 = v0 . dot ( v2 ) ;
204
+ dot11 = v1 . dot ( v1 ) ;
205
+ dot12 = v1 . dot ( v2 ) ;
206
+
207
+ invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 ) ;
208
+ u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom ;
209
+ v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom ;
210
+
211
+ return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 ) ;
212
+
213
+ }
214
+
215
215
} ;
0 commit comments