Skip to content

Commit 1f6fa12

Browse files
committed
Move isTouching logic to updateCPURenderAttributes
1 parent f2c87cb commit 1f6fa12

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/Drawable.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class Drawable {
111111
this._convexHullDirty = true;
112112

113113
this._skinWasAltered = this._skinWasAltered.bind(this);
114+
115+
this.isTouching = this._isTouchingNever;
114116
}
115117

116118
/**
@@ -451,25 +453,33 @@ class Drawable {
451453
}
452454

453455
/**
456+
* @function
457+
* @name isTouching
454458
* Check if the world position touches the skin.
455459
* The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
456460
* @see updateCPURenderAttributes
457461
* @param {twgl.v3} vec World coordinate vector.
458462
* @return {boolean} True if the world position touches the skin.
459463
*/
460-
isTouching (vec) {
461-
if (!this.skin) {
462-
return false;
463-
}
464464

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`.
466471

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));
473483
}
474484

475485
/**
@@ -639,8 +649,19 @@ class Drawable {
639649
*/
640650
updateCPURenderAttributes () {
641651
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+
}
644665
}
645666

646667
/**

0 commit comments

Comments
 (0)