@@ -112,6 +112,8 @@ class Drawable {
112112 this . _convexHullDirty = true ;
113113
114114 this . _skinWasAltered = this . _skinWasAltered . bind ( this ) ;
115+
116+ this . isTouching = this . _isTouchingNever ;
115117 }
116118
117119 /**
@@ -452,25 +454,33 @@ class Drawable {
452454 }
453455
454456 /**
457+ * @function
458+ * @name isTouching
455459 * Check if the world position touches the skin.
456460 * The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
457461 * @see updateCPURenderAttributes
458462 * @param {twgl.v3 } vec World coordinate vector.
459463 * @return {boolean } True if the world position touches the skin.
460464 */
461- isTouching ( vec ) {
462- if ( ! this . skin ) {
463- return false ;
464- }
465465
466- const localPosition = getLocalPosition ( this , vec ) ;
466+ // `updateCPURenderAttributes` sets this Drawable instance's `isTouching` method
467+ // to one of the following three functions:
468+ // If this drawable has no skin, set it to `_isTouchingNever`.
469+ // Otherwise, if this drawable uses nearest-neighbor scaling at its current scale, set it to `_isTouchingNearest`.
470+ // Otherwise, set it to `_isTouchingLinear`.
471+ // This allows several checks to be moved from the `isTouching` function to `updateCPURenderAttributes`.
467472
468- // We're not passing in a scale to useNearest, but that's okay because "touching" queries
469- // happen at the "native" size anyway.
470- if ( this . useNearest ( ) ) {
471- return this . skin . isTouchingNearest ( localPosition ) ;
472- }
473- return this . skin . isTouchingLinear ( localPosition ) ;
473+ // eslint-disable-next-line no-unused-vars
474+ _isTouchingNever ( vec ) {
475+ return false ;
476+ }
477+
478+ _isTouchingNearest ( vec ) {
479+ return this . skin . isTouchingNearest ( getLocalPosition ( this , vec ) ) ;
480+ }
481+
482+ _isTouchingLinear ( vec ) {
483+ return this . skin . isTouchingLinear ( getLocalPosition ( this , vec ) ) ;
474484 }
475485
476486 /**
@@ -643,8 +653,16 @@ class Drawable {
643653 // CPU rendering always occurs at the "native" size, so no need to scale up this._scale
644654 if ( this . skin ) {
645655 this . skin . updateSilhouette ( this . _scale ) ;
656+
657+ if ( this . useNearest ( ) ) {
658+ this . isTouching = this . _isTouchingNearest ;
659+ } else {
660+ this . isTouching = this . _isTouchingLinear ;
661+ }
646662 } else {
647663 log . warn ( `Could not find skin for drawable with id: ${ this . _id } ` ) ;
664+
665+ this . isTouching = this . _isTouchingNever ;
648666 }
649667 }
650668
0 commit comments