1 /**
2 * @author mrdoob / http://mrdoob.com/
3 */
4
5 ( function ( THREE ) {
6
7 /**@constructor*/
8 THREE.Ray = function ( origin, direction, near, far ) {
9
10 this.origin = origin || new THREE.Vector3();
11 this.direction = direction || new THREE.Vector3();
12 this.near = near || 0;
13 this.far = far || Infinity;
14
15 };
16
17 var originCopy = new THREE.Vector3();
18
19 var localOriginCopy = new THREE.Vector3();
20 var localDirectionCopy = new THREE.Vector3();
21
22 var vector = new THREE.Vector3();
23 var normal = new THREE.Vector3();
24 var intersectPoint = new THREE.Vector3();
25
26 var inverseMatrix = new THREE.Matrix4();
27
28 var descSort = function ( a, b ) {
29
30 return a.distance - b.distance;
31
32 };
33
34 var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
35
36 var distanceFromIntersection = function ( origin, direction, position ) {
37
38 v0.sub( position, origin );
39
40 var dot = v0.dot( direction );
41
42 var intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
43 var distance = position.distanceTo( intersect );
44
45 return distance;
46
47 };
48
49 // http://www.blackpawn.com/texts/pointinpoly/default.html
50
51 var pointInFace3 = function ( p, a, b, c ) {
52
53 v0.sub( c, a );
54 v1.sub( b, a );
55 v2.sub( p, a );
56
57 var dot00 = v0.dot( v0 );
58 var dot01 = v0.dot( v1 );
59 var dot02 = v0.dot( v2 );
60 var dot11 = v1.dot( v1 );
61 var dot12 = v1.dot( v2 );
62
63 var invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
64 var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
65 var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
66
67 return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
68
69 };
70
71 var intersectObject = function ( object, ray, intersects ) {
72
73 if ( object instanceof THREE.Particle ) {
74
75 var distance = distanceFromIntersection( ray.origin, ray.direction, object.matrixWorld.getPosition() );
76
77 if ( distance > object.scale.x ) {
78
79 return intersects;
80
81 }
82
83 intersects.push( {
84
85 distance: distance,
86 point: object.position,
87 face: null,
88 object: object
89
90 } );
91
92 } else if ( object instanceof THREE.Mesh ) {
93
94 // Checking boundingSphere
95
96 var scaledRadius = object.geometry.boundingSphere.radius * object.matrixWorld.getMaxScaleOnAxis();
97
98 // Checking distance to ray
99
100 var distance = distanceFromIntersection( ray.origin, ray.direction, object.matrixWorld.getPosition() );
101
102 if ( distance > scaledRadius) {
103
104 return intersects;
105
106 }
107
108 // Checking faces
109
110 var geometry = object.geometry;
111 var vertices = geometry.vertices;
112
113 var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
114 var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
115
116 var side = object.material.side;
117
118 var a, b, c, d;
119 var precision = ray.precision;
120
121 object.matrixRotationWorld.extractRotation( object.matrixWorld );
122
123 originCopy.copy( ray.origin );
124
125 inverseMatrix.getInverse( object.matrixWorld );
126
127 localOriginCopy.copy( originCopy );
128 inverseMatrix.multiplyVector3( localOriginCopy );
129
130 localDirectionCopy.copy( ray.direction );
131 inverseMatrix.rotateAxis( localDirectionCopy ).normalize();
132
133 for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
134
135 var face = geometry.faces[ f ];
136
137 var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
138
139 if ( material === undefined ) continue;
140
141 side = material.side;
142
143 vector.sub( face.centroid, localOriginCopy );
144
145 var normal = face.normal;
146 var dot = localDirectionCopy.dot( normal );
147
148 // bail if ray and plane are parallel
149
150 if ( Math.abs( dot ) < precision ) continue;
151
152 // calc distance to plane
153
154 var scalar = normal.dot( vector ) / dot;
155
156 // if negative distance, then plane is behind ray
157
158 if ( scalar < 0 ) continue;
159
160 if ( side === THREE.DoubleSide || ( side === THREE.FrontSide ? dot < 0 : dot > 0 ) ) {
161
162 intersectPoint.add( localOriginCopy, localDirectionCopy.multiplyScalar( scalar ) );
163
164 if ( face instanceof THREE.Face3 ) {
165
166 a = vertices[ face.a ];
167 b = vertices[ face.b ];
168 c = vertices[ face.c ];
169
170 if ( pointInFace3( intersectPoint, a, b, c ) ) {
171
172 var point = object.matrixWorld.multiplyVector3( intersectPoint.clone() );
173 distance = originCopy.distanceTo( point );
174
175 if ( distance < ray.near || distance > ray.far ) continue;
176
177 intersects.push( {
178
179 distance: distance,
180 point: point,
181 face: face,
182 faceIndex: f,
183 object: object
184
185 } );
186
187 }
188
189 } else if ( face instanceof THREE.Face4 ) {
190
191 a = vertices[ face.a ];
192 b = vertices[ face.b ];
193 c = vertices[ face.c ];
194 d = vertices[ face.d ];
195
196 if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
197
198 var point = object.matrixWorld.multiplyVector3( intersectPoint.clone() );
199 distance = originCopy.distanceTo( point );
200
201 if ( distance < ray.near || distance > ray.far ) continue;
202
203 intersects.push( {
204
205 distance: distance,
206 point: point,
207 face: face,
208 faceIndex: f,
209 object: object
210
211 } );
212
213 }
214
215 }
216
217 }
218
219 }
220
221 }
222
223 };
224
225 var intersectDescendants = function ( object, ray, intersects ) {
226
227 var descendants = object.getDescendants();
228
229 for ( var i = 0, l = descendants.length; i < l; i ++ ) {
230
231 intersectObject( descendants[ i ], ray, intersects );
232
233 }
234 };
235
236 //
237
238 THREE.Ray.prototype.precision = 0.0001;
239
240 THREE.Ray.prototype.set = function ( origin, direction ) {
241
242 this.origin = origin;
243 this.direction = direction;
244
245 };
246
247 THREE.Ray.prototype.intersectObject = function ( object, recursive ) {
248
249 var intersects = [];
250
251 if ( recursive === true ) {
252
253 intersectDescendants( object, this, intersects );
254
255 }
256
257 intersectObject( object, this, intersects );
258
259 intersects.sort( descSort );
260
261 return intersects;
262
263 };
264
265 THREE.Ray.prototype.intersectObjects = function ( objects, recursive ) {
266
267 var intersects = [];
268
269 for ( var i = 0, l = objects.length; i < l; i ++ ) {
270
271 intersectObject( objects[ i ], this, intersects );
272
273 if ( recursive === true ) {
274
275 intersectDescendants( objects[ i ], this, intersects );
276
277 }
278 }
279
280 intersects.sort( descSort );
281
282 return intersects;
283
284 };
285
286 }( THREE ) );
287
nike free rn
new balance hombre baratas
cinturones gucci
ugg rebajas
cinturon gucci
ray ban baratas
nike cortez
peuterey mujer
christian louboutin madrid
mbt zapatos
gafas ray ban baratas
mbt ofertas
air max blancas
mbt barcelona
nike air max 90
woolrich barcelona
nike mujer
botas ugg
gafas de sol carrera aratas
air max 2016 baratas
oakley baratas
nike air max 2016