@@ -112,6 +112,8 @@ class Drawable {
112
112
this . _convexHullDirty = true ;
113
113
114
114
this . _skinWasAltered = this . _skinWasAltered . bind ( this ) ;
115
+
116
+ this . isTouching = this . _isTouchingNever ;
115
117
}
116
118
117
119
/**
@@ -452,25 +454,33 @@ class Drawable {
452
454
}
453
455
454
456
/**
457
+ * @function
458
+ * @name isTouching
455
459
* Check if the world position touches the skin.
456
460
* The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
457
461
* @see updateCPURenderAttributes
458
462
* @param {twgl.v3 } vec World coordinate vector.
459
463
* @return {boolean } True if the world position touches the skin.
460
464
*/
461
- isTouching ( vec ) {
462
- if ( ! this . skin ) {
463
- return false ;
464
- }
465
465
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`.
467
472
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 ) ) ;
474
484
}
475
485
476
486
/**
@@ -643,8 +653,16 @@ class Drawable {
643
653
// CPU rendering always occurs at the "native" size, so no need to scale up this._scale
644
654
if ( this . skin ) {
645
655
this . skin . updateSilhouette ( this . _scale ) ;
656
+
657
+ if ( this . useNearest ( ) ) {
658
+ this . isTouching = this . _isTouchingNearest ;
659
+ } else {
660
+ this . isTouching = this . _isTouchingLinear ;
661
+ }
646
662
} else {
647
663
log . warn ( `Could not find skin for drawable with id: ${ this . _id } ` ) ;
664
+
665
+ this . isTouching = this . _isTouchingNever ;
648
666
}
649
667
}
650
668
0 commit comments