src/components/raycaster.js
function () {
    var clearedIntersectedEls = this.clearedIntersectedEls;
    var el = this.el;
    var data = this.data;
    var i;
    var intersectedEls = this.intersectedEls;
    var intersection;
    var intersections = this.intersections;
    var newIntersectedEls = this.newIntersectedEls;
    var newIntersections = this.newIntersections;
    var prevIntersectedEls = this.prevIntersectedEls;
    var rawIntersections = this.rawIntersections;

    // Refresh the object whitelist if needed.
    if (this.dirty) { this.refreshObjects(); }

    // Store old previously intersected entities.
    copyArray(this.prevIntersectedEls, this.intersectedEls);

    // Raycast.
    this.updateOriginDirection();
    rawIntersections.length = 0;
    this.raycaster.intersectObjects(this.objects, true, rawIntersections);

    // Only keep intersections against objects that have a reference to an entity.
    intersections.length = 0;
    intersectedEls.length = 0;
    for (i = 0; i < rawIntersections.length; i++) {
      intersection = rawIntersections[i];
      // Don't intersect with own line.
      if (data.showLine && intersection.object === el.getObject3D('line')) {
        continue;
      }
      if (intersection.object.el) {
        intersections.push(intersection);
        intersectedEls.push(intersection.object.el);
      }
    }

    // Get newly intersected entities.
    newIntersections.length = 0;
    newIntersectedEls.length = 0;
    for (i = 0; i < intersections.length; i++) {
      if (prevIntersectedEls.indexOf(intersections[i].object.el) === -1) {
        newIntersections.push(intersections[i]);
        newIntersectedEls.push(intersections[i].object.el);
      }
    }

    // Emit intersection cleared on both entities per formerly intersected entity.
    clearedIntersectedEls.length = 0;
    for (i = 0; i < prevIntersectedEls.length; i++) {
      if (intersectedEls.indexOf(prevIntersectedEls[i]) !== -1) { continue; }
      prevIntersectedEls[i].emit(EVENTS.INTERSECT_CLEAR,
                                 this.intersectedClearedDetail);
      clearedIntersectedEls.push(prevIntersectedEls[i]);
    }
    if (clearedIntersectedEls.length) {
      el.emit(EVENTS.INTERSECTION_CLEAR, this.intersectionClearedDetail);
    }

    // Emit intersected on intersected entity per intersected entity.
    for (i = 0; i < newIntersectedEls.length; i++) {
      newIntersectedEls[i].emit(EVENTS.INTERSECT, this.intersectedDetail);
    }

    // Emit all intersections at once on raycasting entity.
    if (newIntersections.length) {
      this.intersectionDetail.els = newIntersectedEls;
      this.intersectionDetail.intersections = newIntersections;
      el.emit(EVENTS.INTERSECTION, this.intersectionDetail);
    }

    // Update line length.
    setTimeout(this.updateLine);
  }
function () {
    var clearedIntersectedEls = this.clearedIntersectedEls;
    var el = this.el;
    var data = this.data;
    var i;
    var intersectedEls = this.intersectedEls;
    var intersection;
    var intersections = this.intersections;
    var newIntersectedEls = this.newIntersectedEls;
    var newIntersections = this.newIntersections;
    var prevIntersectedEls = this.prevIntersectedEls;
    var rawIntersections = this.rawIntersections;

    // Refresh the object whitelist if needed.
    if (this.dirty) { this.refreshObjects(); }

    // Store old previously intersected entities.
    copyArray(this.prevIntersectedEls, this.intersectedEls);

    // Raycast.
    this.updateOriginDirection();
    rawIntersections.length = 0;
    this.raycaster.intersectObjects(this.objects, true, rawIntersections);

    // Only keep intersections against objects that have a reference to an entity.
    intersections.length = 0;
    intersectedEls.length = 0;
    for (i = 0; i < rawIntersections.length; i++) {
      intersection = rawIntersections[i];
      // Don't intersect with own line.
      if (data.showLine && intersection.object === el.getObject3D('line')) {
        continue;
      }
      if (intersection.object.el) {
        intersections.push(intersection);
        intersectedEls.push(intersection.object.el);
      }
    }

    // Get newly intersected entities.
    newIntersections.length = 0;
    newIntersectedEls.length = 0;
    for (i = 0; i < intersections.length; i++) {
      if (prevIntersectedEls.indexOf(intersections[i].object.el) === -1) {
        newIntersections.push(intersections[i]);
        newIntersectedEls.push(intersections[i].object.el);
      }
    }

    // Emit intersection cleared on both entities per formerly intersected entity.
    clearedIntersectedEls.length = 0;
    for (i = 0; i < prevIntersectedEls.length; i++) {
      if (intersectedEls.indexOf(prevIntersectedEls[i]) !== -1) { continue; }
      prevIntersectedEls[i].emit(EVENTS.INTERSECT_CLEAR,
                                 this.intersectedClearedDetail);
      clearedIntersectedEls.push(prevIntersectedEls[i]);
    }
    if (clearedIntersectedEls.length) {
      el.emit(EVENTS.INTERSECTION_CLEAR, this.intersectionClearedDetail);
    }

    // Emit intersected on intersected entity per intersected entity.
    for (i = 0; i < newIntersectedEls.length; i++) {
      newIntersectedEls[i].emit(EVENTS.INTERSECT, this.intersectedDetail);
    }

    // Emit all intersections at once on raycasting entity.
    if (newIntersections.length) {
      this.intersectionDetail.els = newIntersectedEls;
      this.intersectionDetail.intersections = newIntersections;
      el.emit(EVENTS.INTERSECTION, this.intersectionDetail);
    }

    // Update line length.
    setTimeout(this.updateLine);
  }
function ( ) { var clearedIntersectedEls = this . clearedIntersectedEls ; var el = this . el ; var data = this . data ; var i ; var intersectedEls = this . intersectedEls ; var intersection ; var intersections = this . intersections ; var newIntersectedEls = this . newIntersectedEls ; var newIntersections = this . newIntersections ; var prevIntersectedEls = this . prevIntersectedEls ; var rawIntersections = this . rawIntersections ; // Refresh the object whitelist if needed. if ( this . dirty ) { this . refreshObjects ( ) ; } // Store old previously intersected entities. copyArray ( this . prevIntersectedEls , this . intersectedEls ) ; // Raycast. this . updateOriginDirection ( ) ; rawIntersections . length = 0 ; this . raycaster . intersectObjects ( this . objects , true , rawIntersections ) ; // Only keep intersections against objects that have a reference to an entity. intersections . length = 0 ; intersectedEls . length = 0 ; for ( i = 0 ; i < rawIntersections . length ; i ++ ) { intersection = rawIntersections [ i ] ; // Don't intersect with own line. if ( data . showLine && intersection . object === el . getObject3D ( 'line' ) ) { continue ; } if ( intersection . object . el ) { intersections . push ( intersection ) ; intersectedEls . push ( intersection . object . el ) ; } } // Get newly intersected entities. newIntersections . length = 0 ; newIntersectedEls . length = 0 ; for ( i = 0 ; i < intersections . length ; i ++ ) { if ( prevIntersectedEls . indexOf ( intersections [ i ] . object . el ) === - 1 ) { newIntersections . push ( intersections [ i ] ) ; newIntersectedEls . push ( intersections [ i ] . object . el ) ; } } // Emit intersection cleared on both entities per formerly intersected entity. clearedIntersectedEls . length = 0 ; for ( i = 0 ; i < prevIntersectedEls . length ; i ++ ) { if ( intersectedEls . indexOf ( prevIntersectedEls [ i ] ) !== - 1 ) { continue ; } prevIntersectedEls [ i ] . emit ( EVENTS . INTERSECT_CLEAR , this . intersectedClearedDetail ) ; clearedIntersectedEls . push ( prevIntersectedEls [ i ] ) ; } if ( clearedIntersectedEls . length ) { el . emit ( EVENTS . INTERSECTION_CLEAR , this . intersectionClearedDetail ) ; } // Emit intersected on intersected entity per intersected entity. for ( i = 0 ; i < newIntersectedEls . length ; i ++ ) { newIntersectedEls [ i ] . emit ( EVENTS . INTERSECT , this . intersectedDetail ) ; } // Emit all intersections at once on raycasting entity. if ( newIntersections . length ) { this . intersectionDetail . els = newIntersectedEls ; this . intersectionDetail . intersections = newIntersections ; el . emit ( EVENTS . INTERSECTION , this . intersectionDetail ) ; } // Update line length. setTimeout ( this . updateLine ) ; }
Raycast for intersections and emit events for current and cleared inersections.
Raycast for intersections and emit events for current and cleared inersections .