@@ -111,6 +111,8 @@ class Drawable {
111
111
this . _convexHullDirty = true ;
112
112
113
113
this . _skinWasAltered = this . _skinWasAltered . bind ( this ) ;
114
+
115
+ this . isTouching = this . _isTouchingNever ;
114
116
}
115
117
116
118
/**
@@ -451,25 +453,33 @@ class Drawable {
451
453
}
452
454
453
455
/**
456
+ * @function
457
+ * @name isTouching
454
458
* Check if the world position touches the skin.
455
459
* The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
456
460
* @see updateCPURenderAttributes
457
461
* @param {twgl.v3 } vec World coordinate vector.
458
462
* @return {boolean } True if the world position touches the skin.
459
463
*/
460
- isTouching ( vec ) {
461
- if ( ! this . skin ) {
462
- return false ;
463
- }
464
464
465
- const localPosition = getLocalPosition ( this , vec ) ;
465
+ // `updateCPURenderAttributes` sets this Drawable instance's `isTouching` method
466
+ // to one of the following three functions:
467
+ // If this drawable has no skin, set it to `_isTouchingNever`.
468
+ // Otherwise, if this drawable uses nearest-neighbor scaling at its current scale, set it to `_isTouchingNearest`.
469
+ // Otherwise, set it to `_isTouchingLinear`.
470
+ // This allows several checks to be moved from the `isTouching` function to `updateCPURenderAttributes`.
466
471
467
- // We're not passing in a scale to useNearest, but that's okay because "touching" queries
468
- // happen at the "native" size anyway.
469
- if ( this . useNearest ( ) ) {
470
- return this . skin . isTouchingNearest ( localPosition ) ;
471
- }
472
- return this . skin . isTouchingLinear ( localPosition ) ;
472
+ // eslint-disable-next-line no-unused-vars
473
+ _isTouchingNever ( vec ) {
474
+ return false ;
475
+ }
476
+
477
+ _isTouchingNearest ( vec ) {
478
+ return this . skin . isTouchingNearest ( getLocalPosition ( this , vec ) ) ;
479
+ }
480
+
481
+ _isTouchingLinear ( vec ) {
482
+ return this . skin . isTouchingLinear ( getLocalPosition ( this , vec ) ) ;
473
483
}
474
484
475
485
/**
@@ -639,8 +649,19 @@ class Drawable {
639
649
*/
640
650
updateCPURenderAttributes ( ) {
641
651
this . updateMatrix ( ) ;
642
- // CPU rendering always occurs at the "native" size, so no need to scale up this._scale
643
- if ( this . skin ) this . skin . updateSilhouette ( this . _scale ) ;
652
+
653
+ if ( this . skin ) {
654
+ // CPU rendering always occurs at the "native" size, so no need to scale up this._scale
655
+ this . skin . updateSilhouette ( this . _scale ) ;
656
+
657
+ if ( this . useNearest ( ) ) {
658
+ this . isTouching = this . _isTouchingNearest ;
659
+ } else {
660
+ this . isTouching = this . _isTouchingLinear ;
661
+ }
662
+ } else {
663
+ this . isTouching = this . _isTouchingNever ;
664
+ }
644
665
}
645
666
646
667
/**
0 commit comments