Skip to content

Fix SVGSkin silhouettes #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/RenderWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ class RenderWebGL extends EventEmitter {

drawable.updateMatrix();
if (drawable.skin) {
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${drawableID}`);
}
Expand Down Expand Up @@ -1008,7 +1008,7 @@ class RenderWebGL extends EventEmitter {
if (drawable.getVisible() && drawable.getUniforms().u_ghost !== 0) {
drawable.updateMatrix();
if (drawable.skin) {
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
} else {
log.warn(`Could not find skin for drawable with id: ${id}`);
}
Expand Down Expand Up @@ -1243,7 +1243,7 @@ class RenderWebGL extends EventEmitter {
if (!drawable.skin || !drawable.skin.getTexture([100, 100])) return null;

drawable.updateMatrix();
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const bounds = drawable.getFastBounds();

// Limit queries to the stage size.
Expand Down Expand Up @@ -1281,7 +1281,7 @@ class RenderWebGL extends EventEmitter {
if (drawable.skin && drawable._visible) {
// Update the CPU position data
drawable.updateMatrix();
drawable.skin.updateSilhouette();
drawable.skin.updateSilhouette(this._getDrawableScreenSpaceScale(drawable));
const candidateBounds = drawable.getFastBounds();
if (bounds.intersects(candidateBounds)) {
result.push({
Expand Down Expand Up @@ -1659,6 +1659,18 @@ class RenderWebGL extends EventEmitter {
this._regionId = null;
}

/**
* Get the screen-space scale of a drawable, as percentages of the drawable's "normal" size.
* @param {Drawable} drawable The drawable whose screen-space scale we're fetching.
* @returns {Array<number>} The screen-space X and Y dimensions of the drawable's scale, as percentages.
*/
_getDrawableScreenSpaceScale (drawable) {
return [
drawable.scale[0] * this._gl.canvas.width / this._nativeSize[0],
drawable.scale[1] * this._gl.canvas.height / this._nativeSize[1]
];
}

/**
* Draw a set of Drawables, by drawable ID
* @param {Array<int>} drawables The Drawable IDs to draw, possibly this._drawList.
Expand Down Expand Up @@ -1691,10 +1703,7 @@ class RenderWebGL extends EventEmitter {
if (!drawable.getVisible() && !opts.ignoreVisibility) continue;

// Combine drawable scale with the native vs. backing pixel ratio
const drawableScale = [
drawable.scale[0] * this._gl.canvas.width / this._nativeSize[0],
drawable.scale[1] * this._gl.canvas.height / this._nativeSize[1]
];
const drawableScale = this._getDrawableScreenSpaceScale(drawable);

// If the skin or texture isn't ready yet, skip it.
if (!drawable.skin || !drawable.skin.getTexture(drawableScale)) continue;
Expand Down
5 changes: 5 additions & 0 deletions src/SVGSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class SVGSkin extends Skin {
return mip;
}

updateSilhouette (scale = 1) {
// Ensure a silhouette exists.
this.getTexture(scale);
}

/**
* @param {Array<number>} scale - The scaling factors to be used, each in the [0,100] range.
* @return {WebGLTexture} The GL texture representation of this skin when drawing at the given scale.
Expand Down