@@ -53,6 +53,8 @@ module.exports.Component = registerComponent('raycaster', {
5353 this . clearedIntersectedEls = [ ] ;
5454 this . unitLineEndVec3 = new THREE . Vector3 ( ) ;
5555 this . intersectedEls = [ ] ;
56+ this . newIntersectedEls = [ ] ;
57+ this . newIntersections = [ ] ;
5658 this . objects = [ ] ;
5759 this . prevCheckTime = undefined ;
5860 this . prevIntersectedEls = [ ] ;
@@ -61,10 +63,12 @@ module.exports.Component = registerComponent('raycaster', {
6163 this . setDirty = this . setDirty . bind ( this ) ;
6264 this . observer = new MutationObserver ( this . setDirty ) ;
6365 this . dirty = true ;
64- this . intersectionClearedDetail = { clearedEls : this . clearedIntersectedEls } ;
6566 this . lineEndVec3 = new THREE . Vector3 ( ) ;
6667 this . otherLineEndVec3 = new THREE . Vector3 ( ) ;
6768 this . lineData = { end : this . lineEndVec3 } ;
69+
70+ this . intersectedClearedDetail = { el : this . el } ;
71+ this . intersectionClearedDetail = { clearedEls : this . clearedIntersectedEls } ;
6872 } ,
6973
7074 /**
@@ -82,10 +86,11 @@ module.exports.Component = registerComponent('raycaster', {
8286 // Draw line.
8387 if ( data . showLine &&
8488 ( data . far !== oldData . far || data . origin !== oldData . origin ||
85- data . direction !== oldData . direction || data . showLine !== oldData . showLine ) ) {
89+ data . direction !== oldData . direction || ! oldData . showLine ) ) {
8690 this . unitLineEndVec3 . copy ( data . origin ) . add ( data . direction ) . normalize ( ) ;
8791 this . drawLine ( ) ;
8892 }
93+
8994 if ( ! data . showLine && oldData . showLine ) {
9095 el . removeAttribute ( 'line' ) ;
9196 }
@@ -179,8 +184,11 @@ module.exports.Component = registerComponent('raycaster', {
179184 var intersectedEls = this . intersectedEls ;
180185 var intersection ;
181186 var lineLength ;
187+ var newIntersectedEls = this . newIntersectedEls ;
188+ var newIntersections = this . newIntersections ;
182189 var prevIntersectedEls = this . prevIntersectedEls ;
183190 var rawIntersections ;
191+ var self = this ;
184192
185193 if ( ! this . data . enabled ) { return ; }
186194
@@ -196,6 +204,7 @@ module.exports.Component = registerComponent('raycaster', {
196204
197205 // Only keep intersections against objects that have a reference to an entity.
198206 intersections . length = 0 ;
207+ intersectedEls . length = 0 ;
199208 for ( i = 0 ; i < rawIntersections . length ; i ++ ) {
200209 intersection = rawIntersections [ i ] ;
201210 // Don't intersect with own line.
@@ -204,53 +213,61 @@ module.exports.Component = registerComponent('raycaster', {
204213 }
205214 if ( intersection . object . el ) {
206215 intersections . push ( intersection ) ;
216+ intersectedEls . push ( intersection . object . el ) ;
207217 }
208218 }
209219
210- // Update intersectedEls.
211- intersectedEls . length = intersections . length ;
212- for ( i = 0 ; i < intersections . length ; i ++ ) {
213- intersectedEls [ i ] = intersections [ i ] . object . el ;
214- }
215-
216- // Emit intersected on intersected entity per intersected entity.
220+ // Get newly intersected entities.
221+ newIntersections . length = 0 ;
222+ newIntersectedEls . length = 0 ;
217223 for ( i = 0 ; i < intersections . length ; i ++ ) {
218- intersections [ i ] . object . el . emit ( 'raycaster-intersected' , {
219- el : el ,
220- intersection : intersections [ i ]
221- } ) ;
222- }
223-
224- // Emit all intersections at once on raycasting entity.
225- if ( intersections . length ) {
226- el . emit ( 'raycaster-intersection' , {
227- els : intersectedEls ,
228- intersections : intersections
229- } ) ;
224+ if ( prevIntersectedEls . indexOf ( intersections [ i ] . object . el ) === - 1 ) {
225+ newIntersections . push ( intersections [ i ] ) ;
226+ newIntersectedEls . push ( intersections [ i ] . object . el ) ;
227+ }
230228 }
231229
232230 // Emit intersection cleared on both entities per formerly intersected entity.
233231 clearedIntersectedEls . length = 0 ;
234232 for ( i = 0 ; i < prevIntersectedEls . length ; i ++ ) {
235233 if ( intersectedEls . indexOf ( prevIntersectedEls [ i ] ) !== - 1 ) { continue ; }
236- prevIntersectedEls [ i ] . emit ( 'raycaster-intersected-cleared' , { el : el } ) ;
234+ prevIntersectedEls [ i ] . emit ( 'raycaster-intersected-cleared' ,
235+ this . intersectedClearedDetail ) ;
237236 clearedIntersectedEls . push ( prevIntersectedEls [ i ] ) ;
238237 }
239238 if ( clearedIntersectedEls . length ) {
240239 el . emit ( 'raycaster-intersection-cleared' , this . intersectionClearedDetail ) ;
241240 }
242241
242+ // Emit intersected on intersected entity per intersected entity.
243+ for ( i = 0 ; i < newIntersectedEls . length ; i ++ ) {
244+ newIntersectedEls [ i ] . emit ( 'raycaster-intersected' , {
245+ el : el ,
246+ intersection : newIntersections [ i ]
247+ } ) ;
248+ }
249+
250+ // Emit all intersections at once on raycasting entity.
251+ if ( newIntersections . length ) {
252+ el . emit ( 'raycaster-intersection' , {
253+ els : newIntersectedEls ,
254+ intersections : newIntersections
255+ } ) ;
256+ }
257+
243258 // Update line length.
244- if ( data . showLine ) {
245- if ( intersections . length ) {
246- if ( intersections [ 0 ] . object . el === el && intersections [ 1 ] ) {
247- lineLength = intersections [ 1 ] . distance ;
248- } else {
249- lineLength = intersections [ 0 ] . distance ;
259+ setTimeout ( function ( ) {
260+ if ( self . data . showLine ) {
261+ if ( intersections . length ) {
262+ if ( intersections [ 0 ] . object . el === el && intersections [ 1 ] ) {
263+ lineLength = intersections [ 1 ] . distance ;
264+ } else {
265+ lineLength = intersections [ 0 ] . distance ;
266+ }
250267 }
268+ self . drawLine ( lineLength ) ;
251269 }
252- this . drawLine ( lineLength ) ;
253- }
270+ } ) ;
254271 } ;
255272 } ) ( ) ,
256273
0 commit comments