Skip to content

Commit dd2d54e

Browse files
committed
Optimize effect transforms
1 parent 478698c commit dd2d54e

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/Drawable.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ const getLocalPosition = (drawable, vec) => {
3636
// localPosition matches that transformation.
3737
localPosition[0] = 0.5 - (((v0 * m[0]) + (v1 * m[4]) + m[12]) / d);
3838
localPosition[1] = (((v0 * m[1]) + (v1 * m[5]) + m[13]) / d) + 0.5;
39-
// Apply texture effect transform if the localPosition is within the drawable's space.
40-
if ((localPosition[0] >= 0 && localPosition[0] < 1) && (localPosition[1] >= 0 && localPosition[1] < 1)) {
39+
// Apply texture effect transform if the localPosition is within the drawable's space,
40+
// and any effects are currently active.
41+
if (drawable.enabledEffects !== 0 &&
42+
(localPosition[0] >= 0 && localPosition[0] < 1) &&
43+
(localPosition[1] >= 0 && localPosition[1] < 1)) {
44+
4145
EffectTransform.transformPoint(drawable, localPosition, localPosition);
4246
}
4347
return localPosition;
@@ -96,7 +100,11 @@ class Drawable {
96100
this._inverseMatrix = twgl.m4.identity();
97101
this._inverseTransformDirty = true;
98102
this._visible = true;
99-
this._effectBits = 0;
103+
104+
/** A bitmask identifying which effects are currently in use.
105+
* @readonly
106+
* @type {int} */
107+
this.enabledEffects = 0;
100108

101109
/** @todo move convex hull functionality, maybe bounds functionality overall, to Skin classes */
102110
this._convexHullPoints = null;
@@ -159,13 +167,6 @@ class Drawable {
159167
return [this._scale[0], this._scale[1]];
160168
}
161169

162-
/**
163-
* @returns {int} A bitmask identifying which effects are currently in use.
164-
*/
165-
getEnabledEffects () {
166-
return this._effectBits;
167-
}
168-
169170
/**
170171
* @returns {object.<string, *>} the shader uniforms to be used when rendering this Drawable.
171172
*/
@@ -242,9 +243,9 @@ class Drawable {
242243
updateEffect (effectName, rawValue) {
243244
const effectInfo = ShaderManager.EFFECT_INFO[effectName];
244245
if (rawValue) {
245-
this._effectBits |= effectInfo.mask;
246+
this.enabledEffects |= effectInfo.mask;
246247
} else {
247-
this._effectBits &= ~effectInfo.mask;
248+
this.enabledEffects &= ~effectInfo.mask;
248249
}
249250
const converter = effectInfo.converter;
250251
this._uniforms[effectInfo.uniformName] = converter(rawValue);
@@ -481,7 +482,7 @@ class Drawable {
481482
}
482483

483484
// If the effect bits for mosaic, pixelate, whirl, or fisheye are set, use linear
484-
if ((this._effectBits & (
485+
if ((this.enabledEffects & (
485486
ShaderManager.EFFECT_INFO.fisheye.mask |
486487
ShaderManager.EFFECT_INFO.whirl.mask |
487488
ShaderManager.EFFECT_INFO.pixelate.mask |
@@ -692,6 +693,8 @@ class Drawable {
692693
// drawable.useNearest() ?
693694
drawable.skin._silhouette.colorAtNearest(localPosition, dst);
694695
// : drawable.skin._silhouette.colorAtLinear(localPosition, dst);
696+
697+
if (drawable.enabledEffects === 0) return textColor;
695698
return EffectTransform.transformColor(drawable, textColor, textColor);
696699
}
697700
}

src/EffectTransform.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,17 @@ class EffectTransform {
123123
* @returns {Uint8ClampedArray} dst filled with the transformed color
124124
*/
125125
static transformColor (drawable, color4b, dst, effectMask) {
126-
dst = dst || new Uint8ClampedArray(4);
127-
effectMask = effectMask || 0xffffffff;
126+
if (typeof effectMask === 'undefined') effectMask = 0xffffffff;
127+
if (typeof dst === 'undefined') dst = new Uint8ClampedArray(4);
128+
128129
dst.set(color4b);
130+
// If the color is fully transparent, don't bother attempting any transformations.
129131
if (dst[3] === 0) {
130132
return dst;
131133
}
132134

135+
const effects = drawable.enabledEffects & effectMask;
133136
const uniforms = drawable.getUniforms();
134-
const effects = drawable.getEnabledEffects() & effectMask;
135137

136138
if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) {
137139
// gl_FragColor.a *= u_ghost
@@ -186,12 +188,12 @@ class EffectTransform {
186188
* @param {?twgl.v3} dst A place to store the output coordinate.
187189
* @return {twgl.v3} dst - The coordinate after being transform by effects.
188190
*/
189-
static transformPoint (drawable, vec, dst = twgl.v3.create()) {
191+
static transformPoint (drawable, vec, dst) {
192+
if (typeof dst === 'undefined') dst = twgl.v3.create();
190193
twgl.v3.copy(vec, dst);
191194

195+
const effects = drawable.enabledEffects;
192196
const uniforms = drawable.getUniforms();
193-
const effects = drawable.getEnabledEffects();
194-
195197
if ((effects & ShaderManager.EFFECT_INFO.mosaic.mask) !== 0) {
196198
// texcoord0 = fract(u_mosaic * texcoord0);
197199
dst[0] = uniforms.u_mosaic * dst[0] % 1;

src/RenderWebGL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ class RenderWebGL extends EventEmitter {
17011701

17021702
const uniforms = {};
17031703

1704-
let effectBits = drawable.getEnabledEffects();
1704+
let effectBits = drawable.enabledEffects;
17051705
effectBits &= opts.hasOwnProperty('effectMask') ? opts.effectMask : effectBits;
17061706
const newShader = this._shaderManager.getShader(drawMode, effectBits);
17071707

0 commit comments

Comments
 (0)