Skip to content

Commit a67cd48

Browse files
authored
Merge pull request #485 from adroitwhiz/effect-transform-optimizations
Optimize effect transforms
2 parents ed889bc + 8d1117b commit a67cd48

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

src/Drawable.js

Lines changed: 17 additions & 14 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,7 +693,9 @@ class Drawable {
692693
// drawable.useNearest() ?
693694
drawable.skin._silhouette.colorAtNearest(localPosition, dst);
694695
// : drawable.skin._silhouette.colorAtLinear(localPosition, dst);
695-
return EffectTransform.transformColor(drawable, textColor, textColor);
696+
697+
if (drawable.enabledEffects === 0) return textColor;
698+
return EffectTransform.transformColor(drawable, textColor);
696699
}
697700
}
698701

src/EffectTransform.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,33 @@ const hslToRgb = ([h, s, l]) => {
114114
class EffectTransform {
115115

116116
/**
117-
* Transform a color given the drawables effect uniforms. Will apply
117+
* Transform a color in-place given the drawable's effect uniforms. Will apply
118118
* Ghost and Color and Brightness effects.
119119
* @param {Drawable} drawable The drawable to get uniforms from.
120-
* @param {Uint8ClampedArray} color4b The initial color.
121-
* @param {Uint8ClampedArary} [dst] Working space to save the color in (is returned)
122-
* @param {number} [effectMask] A bitmask for which effects to use. Optional.
120+
* @param {Uint8ClampedArray} inOutColor The color to transform.
123121
* @returns {Uint8ClampedArray} dst filled with the transformed color
124122
*/
125-
static transformColor (drawable, color4b, dst, effectMask) {
126-
dst = dst || new Uint8ClampedArray(4);
127-
effectMask = effectMask || 0xffffffff;
128-
dst.set(color4b);
129-
if (dst[3] === 0) {
130-
return dst;
123+
static transformColor (drawable, inOutColor) {
124+
125+
// If the color is fully transparent, don't bother attempting any transformations.
126+
if (inOutColor[3] === 0) {
127+
return inOutColor;
131128
}
132129

130+
const effects = drawable.enabledEffects;
133131
const uniforms = drawable.getUniforms();
134-
const effects = drawable.getEnabledEffects() & effectMask;
135132

136133
if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) {
137134
// gl_FragColor.a *= u_ghost
138-
dst[3] *= uniforms.u_ghost;
135+
inOutColor[3] *= uniforms.u_ghost;
139136
}
140137

141138
const enableColor = (effects & ShaderManager.EFFECT_INFO.color.mask) !== 0;
142139
const enableBrightness = (effects & ShaderManager.EFFECT_INFO.brightness.mask) !== 0;
143140

144141
if (enableColor || enableBrightness) {
145142
// vec3 hsl = convertRGB2HSL(gl_FragColor.xyz);
146-
const hsl = rgbToHsl(dst);
143+
const hsl = rgbToHsl(inOutColor);
147144

148145
if (enableColor) {
149146
// this code forces grayscale values to be slightly saturated
@@ -173,25 +170,24 @@ class EffectTransform {
173170
hsl[2] = Math.min(1, hsl[2] + uniforms.u_brightness);
174171
}
175172
// gl_FragColor.rgb = convertHSL2RGB(hsl);
176-
dst.set(hslToRgb(hsl));
173+
inOutColor.set(hslToRgb(hsl));
177174
}
178175

179-
return dst;
176+
return inOutColor;
180177
}
181178

182179
/**
183180
* Transform a texture coordinate to one that would be select after applying shader effects.
184181
* @param {Drawable} drawable The drawable whose effects to emulate.
185182
* @param {twgl.v3} vec The texture coordinate to transform.
186-
* @param {?twgl.v3} dst A place to store the output coordinate.
183+
* @param {twgl.v3} dst A place to store the output coordinate.
187184
* @return {twgl.v3} dst - The coordinate after being transform by effects.
188185
*/
189-
static transformPoint (drawable, vec, dst = twgl.v3.create()) {
186+
static transformPoint (drawable, vec, dst) {
190187
twgl.v3.copy(vec, dst);
191188

189+
const effects = drawable.enabledEffects;
192190
const uniforms = drawable.getUniforms();
193-
const effects = drawable.getEnabledEffects();
194-
195191
if ((effects & ShaderManager.EFFECT_INFO.mosaic.mask) !== 0) {
196192
// texcoord0 = fract(u_mosaic * texcoord0);
197193
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)