Skip to content

Commit ca3e857

Browse files
authored
yippie -- Cleanup and optimize RenderWebGL.js
1 parent 77fbab1 commit ca3e857

File tree

1 file changed

+50
-129
lines changed

1 file changed

+50
-129
lines changed

src/RenderWebGL.js

Lines changed: 50 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,8 @@ class RenderWebGL extends EventEmitter {
18371837

18381838
// Make a new bufferInfo since this._queryBufferInfo is limited to 480x360
18391839
const bufferInfo = twgl.createFramebufferInfo(
1840-
gl,
1841-
[{format: gl.RGBA}],
1842-
clampedWidth,
1843-
clampedHeight
1840+
gl, [{format: gl.RGBA}],
1841+
clampedWidth, clampedHeight
18441842
);
18451843

18461844
try {
@@ -1850,12 +1848,9 @@ class RenderWebGL extends EventEmitter {
18501848
// and create the projection matrix for the draw.
18511849
gl.viewport(0, 0, clampedWidth, clampedHeight);
18521850
const projection = twgl.m4.ortho(
1853-
scratchBounds.left,
1854-
scratchBounds.right,
1855-
scratchBounds.top,
1856-
scratchBounds.bottom,
1857-
-1,
1858-
1
1851+
scratchBounds.left, scratchBounds.right,
1852+
scratchBounds.top, scratchBounds.bottom,
1853+
-1, 1
18591854
);
18601855

18611856
gl.clearColor(0, 0, 0, 0);
@@ -1877,20 +1872,13 @@ class RenderWebGL extends EventEmitter {
18771872

18781873
const data = new Uint8Array(Math.floor(clampedWidth * clampedHeight * 4));
18791874
gl.readPixels(
1880-
0,
1881-
0,
1882-
clampedWidth,
1883-
clampedHeight,
1884-
gl.RGBA,
1885-
gl.UNSIGNED_BYTE,
1886-
data
1875+
0, 0, clampedWidth, clampedHeight,
1876+
gl.RGBA, gl.UNSIGNED_BYTE, data
18871877
);
18881878
// readPixels can only read into a Uint8Array, but ImageData has to take a Uint8ClampedArray.
18891879
// We can share the same underlying buffer between them to avoid having to copy any data.
18901880
const imageData = new ImageData(
1891-
new Uint8ClampedArray(data.buffer),
1892-
clampedWidth,
1893-
clampedHeight
1881+
new Uint8ClampedArray(data.buffer), clampedWidth, clampedHeight
18941882
);
18951883

18961884
// On high-DPI devices, the canvas' width (in canvas pixels) will be larger than its width in CSS pixels.
@@ -1902,10 +1890,8 @@ class RenderWebGL extends EventEmitter {
19021890

19031891
return {
19041892
imageData,
1905-
x: canvasSpaceBounds.left * ratio,
1906-
y: canvasSpaceBounds.bottom * ratio,
1907-
width: canvasSpaceBounds.width * ratio,
1908-
height: canvasSpaceBounds.height * ratio
1893+
x: canvasSpaceBounds.left * ratio, y: canvasSpaceBounds.bottom * ratio,
1894+
width: canvasSpaceBounds.width * ratio, height: canvasSpaceBounds.height * ratio
19091895
};
19101896
} finally {
19111897
gl.deleteFramebuffer(bufferInfo.framebuffer);
@@ -1942,58 +1928,42 @@ class RenderWebGL extends EventEmitter {
19421928

19431929
const bounds = new Rectangle();
19441930
bounds.initFromBounds(
1945-
scratchX - radius,
1946-
scratchX + radius,
1947-
scratchY - radius,
1948-
scratchY + radius
1931+
scratchX - radius, scratchX + radius,
1932+
scratchY - radius, scratchY + radius
19491933
);
19501934

19511935
const pickX = scratchX - bounds.left;
19521936
const pickY = bounds.top - scratchY;
19531937

19541938
gl.viewport(0, 0, bounds.width, bounds.height);
19551939
const projection = twgl.m4.ortho(
1956-
bounds.left,
1957-
bounds.right,
1958-
bounds.top,
1959-
bounds.bottom,
1960-
-1,
1961-
1
1940+
bounds.left, bounds.right,
1941+
bounds.top, bounds.bottom,
1942+
-1, 1
19621943
);
19631944

19641945
gl.clearColor(...this._backgroundColor4f);
19651946
gl.clear(gl.COLOR_BUFFER_BIT);
19661947
this._drawThese(
1967-
this._drawList,
1968-
ShaderManager.DRAW_MODE.default,
1969-
projection
1948+
this._drawList, ShaderManager.DRAW_MODE.default, projection
19701949
);
19711950

19721951
const data = new Uint8Array(Math.floor(bounds.width * bounds.height * 4));
19731952
gl.readPixels(
1974-
0,
1975-
0,
1976-
bounds.width,
1977-
bounds.height,
1978-
gl.RGBA,
1979-
gl.UNSIGNED_BYTE,
1980-
data
1953+
0, 0, bounds.width, bounds.height,
1954+
gl.RGBA, gl.UNSIGNED_BYTE, data
19811955
);
19821956

19831957
const pixelBase = Math.floor(4 * (pickY * bounds.width + pickX));
19841958
const color = {
1985-
r: data[pixelBase],
1986-
g: data[pixelBase + 1],
1987-
b: data[pixelBase + 2],
1988-
a: data[pixelBase + 3]
1959+
r: data[pixelBase], g: data[pixelBase + 1],
1960+
b: data[pixelBase + 2], a: data[pixelBase + 3]
19891961
};
19901962

19911963
if (this._debugCanvas) {
19921964
this._debugCanvas.width = bounds.width;
19931965
this._debugCanvas.height = bounds.height;
1994-
const ctx = this._debugCanvas.getContext('2d', {
1995-
willReadFrequently: true
1996-
});
1966+
const ctx = this._debugCanvas.getContext('2d', { willReadFrequently: true });
19971967
const imageData = ctx.createImageData(bounds.width, bounds.height);
19981968
imageData.data.set(data);
19991969
ctx.putImageData(imageData, 0, 0);
@@ -2049,9 +2019,7 @@ class RenderWebGL extends EventEmitter {
20492019
if (!this.offscreenTouching) {
20502020
bounds.clamp(this._xLeft, this._xRight, this._yBottom, this._yTop);
20512021
}
2052-
if (bounds.width === 0 || bounds.height === 0) {
2053-
return null;
2054-
}
2022+
if (bounds.width === 0 || bounds.height === 0) return null;
20552023
return bounds;
20562024
}
20572025

@@ -2065,9 +2033,7 @@ class RenderWebGL extends EventEmitter {
20652033
_candidatesTouching (drawableID, candidateIDs) {
20662034
const bounds = this._touchingBounds(drawableID);
20672035
const result = [];
2068-
if (bounds === null) {
2069-
return result;
2070-
}
2036+
if (bounds === null) return result;
20712037
// iterate through the drawables list BACKWARDS - we want the top most item to be the first we check
20722038
for (let index = candidateIDs.length - 1; index >= 0; index--) {
20732039
const id = candidateIDs[index];
@@ -2092,8 +2058,7 @@ class RenderWebGL extends EventEmitter {
20922058

20932059
if (bounds.intersects(candidateBounds)) {
20942060
result.push({
2095-
id,
2096-
drawable,
2061+
id, drawable,
20972062
intersection: Rectangle.intersect(bounds, candidateBounds)
20982063
});
20992064
}
@@ -2111,9 +2076,7 @@ class RenderWebGL extends EventEmitter {
21112076
*/
21122077
_candidatesBounds (candidates) {
21132078
return candidates.reduce((memo, {intersection}) => {
2114-
if (!memo) {
2115-
return intersection;
2116-
}
2079+
if (!memo) return intersection;
21172080
// store the union of the two rectangles in our static rectangle instance
21182081
return Rectangle.union(memo, intersection, __candidatesBounds);
21192082
}, null);
@@ -2223,9 +2186,7 @@ class RenderWebGL extends EventEmitter {
22232186
*/
22242187
return;
22252188
}
2226-
if ('skinId' in properties) {
2227-
this.updateDrawableSkinId(drawableID, properties.skinId);
2228-
}
2189+
if ('skinId' in properties) this.updateDrawableSkinId(drawableID, properties.skinId);
22292190
drawable.updateProperties(properties);
22302191
}
22312192

@@ -2252,17 +2213,11 @@ class RenderWebGL extends EventEmitter {
22522213
const inset = Math.floor(Math.min(aabb.width, aabb.height) / 2);
22532214

22542215
const sx = this._xRight - Math.min(FENCE_WIDTH, inset);
2255-
if (aabb.right + dx < -sx) {
2256-
x = Math.ceil(drawable._position[0] - (sx + aabb.right));
2257-
} else if (aabb.left + dx > sx) {
2258-
x = Math.floor(drawable._position[0] + (sx - aabb.left));
2259-
}
2216+
if (aabb.right + dx < -sx) x = Math.ceil(drawable._position[0] - (sx + aabb.right));
2217+
else if (aabb.left + dx > sx) x = Math.floor(drawable._position[0] + (sx - aabb.left));
22602218
const sy = this._yTop - Math.min(FENCE_WIDTH, inset);
2261-
if (aabb.top + dy < -sy) {
2262-
y = Math.ceil(drawable._position[1] - (sy + aabb.top));
2263-
} else if (aabb.bottom + dy > sy) {
2264-
y = Math.floor(drawable._position[1] + (sy - aabb.bottom));
2265-
}
2219+
if (aabb.top + dy < -sy) y = Math.ceil(drawable._position[1] - (sy + aabb.top));
2220+
else if (aabb.bottom + dy > sy) y = Math.floor(drawable._position[1] + (sy - aabb.bottom));
22662221
return [x, y];
22672222
}
22682223

@@ -2326,15 +2281,11 @@ class RenderWebGL extends EventEmitter {
23262281
penStamp (penSkinID, stampID) {
23272282
this.dirty = true;
23282283
const stampDrawable = this._allDrawables[stampID];
2329-
if (!stampDrawable) {
2330-
return;
2331-
}
2284+
if (!stampDrawable) return;
23322285

23332286
// TW: The bounds will be snapped later
23342287
const bounds = this._unsnappedTouchingBounds(stampID);
2335-
if (!bounds) {
2336-
return;
2337-
}
2288+
if (!bounds) return;
23382289

23392290
this._doExitDrawRegion();
23402291

@@ -2355,17 +2306,13 @@ class RenderWebGL extends EventEmitter {
23552306
gl.viewport(
23562307
this._nativeSize[0] * 0.5 * quality + bounds.left,
23572308
this._nativeSize[1] * 0.5 * quality - bounds.top,
2358-
bounds.width,
2359-
bounds.height
2309+
bounds.width, bounds.height
23602310
);
23612311
const projection = twgl.m4.ortho(
23622312
// TW: We have to convert the snapped "screen-space" back to "stage-space" for rendering.
2363-
bounds.left / quality,
2364-
bounds.right / quality,
2365-
bounds.top / quality,
2366-
bounds.bottom / quality,
2367-
-1,
2368-
1
2313+
bounds.left / quality, bounds.right / quality,
2314+
bounds.top / quality, bounds.bottom / quality,
2315+
-1, 1
23692316
);
23702317

23712318
// Draw the stamped sprite onto the PenSkin's framebuffer.
@@ -2417,29 +2364,20 @@ class RenderWebGL extends EventEmitter {
24172364

24182365
if (!this._pickBufferInfo) {
24192366
this._pickBufferInfo = twgl.createFramebufferInfo(
2420-
gl,
2421-
attachments,
2422-
MAX_TOUCH_SIZE[0],
2423-
MAX_TOUCH_SIZE[1]
2367+
gl, attachments, MAX_TOUCH_SIZE[0], MAX_TOUCH_SIZE[1]
24242368
);
24252369
}
24262370

24272371
/** @todo should we create this on demand to save memory? */
24282372
// A 480x360 32-bpp buffer is 675 KiB.
24292373
if (this._queryBufferInfo) {
24302374
twgl.resizeFramebufferInfo(
2431-
gl,
2432-
this._queryBufferInfo,
2433-
attachments,
2434-
width,
2435-
height
2375+
gl, this._queryBufferInfo,
2376+
attachments, width, height
24362377
);
24372378
} else {
24382379
this._queryBufferInfo = twgl.createFramebufferInfo(
2439-
gl,
2440-
attachments,
2441-
width,
2442-
height
2380+
gl, attachments, width, height
24432381
);
24442382
}
24452383
}
@@ -2473,9 +2411,7 @@ class RenderWebGL extends EventEmitter {
24732411
* state.
24742412
*/
24752413
_doExitDrawRegion () {
2476-
if (this._exitRegion !== null) {
2477-
this._exitRegion();
2478-
}
2414+
if (this._exitRegion !== null) this._exitRegion();
24792415
this._exitRegion = null;
24802416
this._regionId = null;
24812417
}
@@ -2518,7 +2454,6 @@ class RenderWebGL extends EventEmitter {
25182454
const drawable = this._allDrawables[drawableID];
25192455

25202456
const uniforms = {};
2521-
console.log(0, drawable.uniformApplied, !this.renderOffscreen);
25222457
if (!this.renderOffscreen && drawable.uniformApplied) {
25232458
if (drawMode === ShaderManager.DRAW_MODE.default && drawable.skin) {
25242459
// If rotationCenterDirty or skinScaleDirty is dirty, then set _calculateTransform first
@@ -2531,9 +2466,7 @@ class RenderWebGL extends EventEmitter {
25312466

25322467
if (!drawable.inViewport(halfNativeSizeX, halfNativeSizeY)) continue;
25332468
// If unconfirm was not set before
2534-
if (!uniformHasBeenSet) {
2535-
Object.assign(uniforms, drawable.getUniforms());
2536-
}
2469+
if (!uniformHasBeenSet) Object.assign(uniforms, drawable.getUniforms());
25372470
} else {
25382471
Object.assign(uniforms, drawable.getUniforms());
25392472
}
@@ -2567,8 +2500,7 @@ class RenderWebGL extends EventEmitter {
25672500

25682501
let effectBits = drawable.enabledEffects;
25692502
effectBits &= Object.prototype.hasOwnProperty.call(opts, 'effectMask')
2570-
? opts.effectMask
2571-
: effectBits;
2503+
? opts.effectMask : effectBits;
25722504
const newShader = this._shaderManager.getShader(drawMode, effectBits);
25732505

25742506
// Manually perform region check. Do not create functions inside a
@@ -2585,28 +2517,21 @@ class RenderWebGL extends EventEmitter {
25852517
});
25862518
}
25872519

2588-
console.log(2, drawable.uniformApplied, !this.renderOffscreen);
2589-
if (!this.renderOffscreen && drawable.uniformApplied) {
2590-
Object.assign(uniforms, drawable.getUniforms());
2591-
} else {
2520+
if (!this.renderOffscreen && drawable.uniformApplied) Object.assign(uniforms, drawable.getUniforms());
2521+
else {
25922522
drawable.uniformApplied = true;
25932523
Object.assign(
2594-
uniforms,
2595-
drawable.skin.getUniforms(drawableScale),
2596-
drawable.getUniforms()
2524+
uniforms, drawable.skin.getUniforms(drawableScale), drawable.getUniforms()
25972525
);
25982526
}
25992527

26002528
// Apply extra uniforms after the Drawable's, to allow overwriting.
2601-
if (opts.extraUniforms) {
2602-
Object.assign(uniforms, opts.extraUniforms);
2603-
}
2529+
if (opts.extraUniforms) Object.assign(uniforms, opts.extraUniforms);
26042530

26052531
if (uniforms.u_skin) {
26062532
twgl.setTextureParameters(gl, uniforms.u_skin, {
26072533
minMag: drawable.skin.useNearest(drawableScale, drawable)
2608-
? gl.NEAREST
2609-
: gl.LINEAR
2534+
? gl.NEAREST : gl.LINEAR
26102535
});
26112536
}
26122537

@@ -2628,9 +2553,7 @@ class RenderWebGL extends EventEmitter {
26282553

26292554
const [width, height] = drawable.skin.size;
26302555
// No points in the hull if invisible or size is 0.
2631-
if (!drawable.getVisible() || width === 0 || height === 0) {
2632-
return [];
2633-
}
2556+
if (!drawable.getVisible() || width === 0 || height === 0) return [];
26342557

26352558
drawable.updateCPURenderAttributes();
26362559

@@ -2689,9 +2612,7 @@ class RenderWebGL extends EventEmitter {
26892612
}
26902613

26912614
// If we managed to loop all the way through, there are no opaque pixels on this row. Go to the next one
2692-
if (x >= width) {
2693-
continue;
2694-
}
2615+
if (x >= width) continue;
26952616

26962617
// Because leftEndPointIndex is initialized to -1, this is skipped for the first two rows.
26972618
// It runs only when there are enough points in the left hull to make at least one line.

0 commit comments

Comments
 (0)