Move matrix + silhouette updates into method

This commit is contained in:
adroitwhiz 2020-05-19 16:02:12 -04:00
parent d73aeb1ac1
commit e7b1504117
5 changed files with 27 additions and 21 deletions

View file

@ -452,6 +452,8 @@ class Drawable {
/** /**
* Check if the world position touches the skin. * Check if the world position touches the skin.
* The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
* @see updateCPURenderAttributes
* @param {twgl.v3} vec World coordinate vector. * @param {twgl.v3} vec World coordinate vector.
* @return {boolean} True if the world position touches the skin. * @return {boolean} True if the world position touches the skin.
*/ */
@ -632,6 +634,15 @@ class Drawable {
} }
} }
/**
* Update everything necessary to render this drawable on the CPU.
*/
updateCPURenderAttributes () {
this.updateMatrix();
// CPU rendering always occurs at the "native" size, so no need to scale up this._scale
if (this.skin) this.skin.updateSilhouette(this._scale);
}
/** /**
* Respond to an internal change in the current Skin. * Respond to an internal change in the current Skin.
* @private * @private
@ -676,6 +687,8 @@ class Drawable {
/** /**
* Sample a color from a drawable's texture. * Sample a color from a drawable's texture.
* The caller is responsible for ensuring this drawable's inverse matrix & its skin's silhouette are up-to-date.
* @see updateCPURenderAttributes
* @param {twgl.v3} vec The scratch space [x,y] vector * @param {twgl.v3} vec The scratch space [x,y] vector
* @param {Drawable} drawable The drawable to sample the texture from * @param {Drawable} drawable The drawable to sample the texture from
* @param {Uint8ClampedArray} dst The "color4b" representation of the texture at point. * @param {Uint8ClampedArray} dst The "color4b" representation of the texture at point.

View file

@ -985,12 +985,7 @@ class RenderWebGL extends EventEmitter {
const bounds = this.clientSpaceToScratchBounds(centerX, centerY, touchWidth, touchHeight); const bounds = this.clientSpaceToScratchBounds(centerX, centerY, touchWidth, touchHeight);
const worldPos = twgl.v3.create(); const worldPos = twgl.v3.create();
drawable.updateMatrix(); drawable.updateCPURenderAttributes();
if (drawable.skin) {
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${drawableID}`);
}
for (worldPos[1] = bounds.bottom; worldPos[1] <= bounds.top; worldPos[1]++) { for (worldPos[1] = bounds.bottom; worldPos[1] <= bounds.top; worldPos[1]++) {
for (worldPos[0] = bounds.left; worldPos[0] <= bounds.right; worldPos[0]++) { for (worldPos[0] = bounds.left; worldPos[0] <= bounds.right; worldPos[0]++) {
@ -1021,12 +1016,7 @@ class RenderWebGL extends EventEmitter {
const drawable = this._allDrawables[id]; const drawable = this._allDrawables[id];
// default pick list ignores visible and ghosted sprites. // default pick list ignores visible and ghosted sprites.
if (drawable.getVisible() && drawable.getUniforms().u_ghost !== 0) { if (drawable.getVisible() && drawable.getUniforms().u_ghost !== 0) {
drawable.updateMatrix(); drawable.updateCPURenderAttributes();
if (drawable.skin) {
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${id}`);
}
return true; return true;
} }
return false; return false;
@ -1258,8 +1248,8 @@ class RenderWebGL extends EventEmitter {
/** @todo remove this once URL-based skin setting is removed. */ /** @todo remove this once URL-based skin setting is removed. */
if (!drawable.skin || !drawable.skin.getTexture([100, 100])) return null; if (!drawable.skin || !drawable.skin.getTexture([100, 100])) return null;
drawable.updateMatrix();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable)); drawable.updateCPURenderAttributes();
const bounds = drawable.getFastBounds(); const bounds = drawable.getFastBounds();
// Limit queries to the stage size. // Limit queries to the stage size.
@ -1296,8 +1286,7 @@ class RenderWebGL extends EventEmitter {
const drawable = this._allDrawables[id]; const drawable = this._allDrawables[id];
if (drawable.skin && drawable._visible) { if (drawable.skin && drawable._visible) {
// Update the CPU position data // Update the CPU position data
drawable.updateMatrix(); drawable.updateCPURenderAttributes();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const candidateBounds = drawable.getFastBounds(); const candidateBounds = drawable.getFastBounds();
if (bounds.intersects(candidateBounds)) { if (bounds.intersects(candidateBounds)) {
result.push({ result.push({
@ -1775,8 +1764,7 @@ class RenderWebGL extends EventEmitter {
_getConvexHullPointsForDrawable (drawableID) { _getConvexHullPointsForDrawable (drawableID) {
const drawable = this._allDrawables[drawableID]; const drawable = this._allDrawables[drawableID];
drawable.updateMatrix(); drawable.updateCPURenderAttributes();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const [width, height] = drawable.skin.size; const [width, height] = drawable.skin.size;
// No points in the hull if invisible or size is 0. // No points in the hull if invisible or size is 0.

View file

@ -105,7 +105,7 @@ class SVGSkin extends Skin {
return mip; return mip;
} }
updateSilhouette (scale = 1) { updateSilhouette (scale = [100, 100]) {
// Ensure a silhouette exists. // Ensure a silhouette exists.
this.getTexture(scale); this.getTexture(scale);
} }

View file

@ -222,6 +222,9 @@ class Skin extends EventEmitter {
/** /**
* Does this point touch an opaque or translucent point on this skin? * Does this point touch an opaque or translucent point on this skin?
* Nearest Neighbor version * Nearest Neighbor version
* The caller is responsible for ensuring this skin's silhouette is up-to-date.
* @see updateSilhouette
* @see Drawable.updateCPURenderAttributes
* @param {twgl.v3} vec A texture coordinate. * @param {twgl.v3} vec A texture coordinate.
* @return {boolean} Did it touch? * @return {boolean} Did it touch?
*/ */
@ -232,6 +235,9 @@ class Skin extends EventEmitter {
/** /**
* Does this point touch an opaque or translucent point on this skin? * Does this point touch an opaque or translucent point on this skin?
* Linear Interpolation version * Linear Interpolation version
* The caller is responsible for ensuring this skin's silhouette is up-to-date.
* @see updateSilhouette
* @see Drawable.updateCPURenderAttributes
* @param {twgl.v3} vec A texture coordinate. * @param {twgl.v3} vec A texture coordinate.
* @return {boolean} Did it touch? * @return {boolean} Did it touch?
*/ */

View file

@ -42,8 +42,7 @@
if (!(drawable._visible && drawable.skin)) { if (!(drawable._visible && drawable.skin)) {
return; return;
} }
drawable.updateMatrix(); drawable.updateCPURenderAttributes();
drawable.skin.updateSilhouette();
return { id, drawable }; return { id, drawable };
}).reverse().filter(Boolean); }).reverse().filter(Boolean);
const color = new Uint8ClampedArray(3); const color = new Uint8ClampedArray(3);