Skip to content

Commit d16a4a1

Browse files
authored
Merge pull request #684 from LLK/revert-676-revert-674-revert-673-revert-660-revert-489
Revert "Put back in "Merge pull request #489 from adroitwhiz/touching-white-fixes""
2 parents 5902c4a + 0ae9cdd commit d16a4a1

File tree

8 files changed

+31
-126
lines changed

8 files changed

+31
-126
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"eslint": "^4.6.1",
3535
"eslint-config-scratch": "^5.0.0",
3636
"gh-pages": "^1.0.0",
37-
"jsdoc": "^3.6.0",
37+
"jsdoc": "^3.5.5",
3838
"json": "^9.0.4",
3939
"playwright-chromium": "^1.0.1",
4040
"scratch-vm": "0.2.0-prerelease.20200622143012",

src/Drawable.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ const getLocalPosition = (drawable, vec) => {
3737
// localPosition matches that transformation.
3838
localPosition[0] = 0.5 - (((v0 * m[0]) + (v1 * m[4]) + m[12]) / d);
3939
localPosition[1] = (((v0 * m[1]) + (v1 * m[5]) + m[13]) / d) + 0.5;
40-
// Fix floating point issues near 0.
41-
// TODO: Check if this can be removed after render pull 479 is merged
42-
if (Math.abs(localPosition[0] < 1e-8)) localPosition[0] = 0;
43-
if (Math.abs(localPosition[1] < 1e-8)) localPosition[1] = 0;
4440
// Apply texture effect transform if the localPosition is within the drawable's space,
4541
// and any effects are currently active.
4642
if (drawable.enabledEffects !== 0 &&
@@ -732,7 +728,6 @@ class Drawable {
732728
dst[3] = 0;
733729
return dst;
734730
}
735-
736731
const textColor =
737732
// commenting out to only use nearest for now
738733
// drawable.useNearest() ?

src/RenderWebGL.js

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,9 @@ class RenderWebGL extends EventEmitter {
188188
/** @type {function} */
189189
this._exitRegion = null;
190190

191-
/** @type {object} */
192-
this._backgroundDrawRegionId = {
193-
enter: () => this._enterDrawBackground(),
194-
exit: () => this._exitDrawBackground()
195-
};
196-
197191
/** @type {Array.<snapshotCallback>} */
198192
this._snapshotCallbacks = [];
199193

200-
/** @type {Array<number>} */
201-
// Don't set this directly-- use setBackgroundColor so it stays in sync with _backgroundColor3b
202-
this._backgroundColor4f = [0, 0, 0, 1];
203-
204-
/** @type {Uint8ClampedArray} */
205-
// Don't set this directly-- use setBackgroundColor so it stays in sync with _backgroundColor4f
206-
this._backgroundColor3b = new Uint8ClampedArray(3);
207-
208194
this._createGeometry();
209195

210196
this.on(RenderConstants.Events.NativeSizeChanged, this.onNativeSizeChanged);
@@ -264,14 +250,7 @@ class RenderWebGL extends EventEmitter {
264250
* @param {number} blue The blue component for the background.
265251
*/
266252
setBackgroundColor (red, green, blue) {
267-
this._backgroundColor4f[0] = red;
268-
this._backgroundColor4f[1] = green;
269-
this._backgroundColor4f[2] = blue;
270-
271-
this._backgroundColor3b[0] = red * 255;
272-
this._backgroundColor3b[1] = green * 255;
273-
this._backgroundColor3b[2] = blue * 255;
274-
253+
this._backgroundColor = [red, green, blue, 1];
275254
}
276255

277256
/**
@@ -650,7 +629,7 @@ class RenderWebGL extends EventEmitter {
650629

651630
twgl.bindFramebufferInfo(gl, null);
652631
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
653-
gl.clearColor.apply(gl, this._backgroundColor4f);
632+
gl.clearColor.apply(gl, this._backgroundColor);
654633
gl.clear(gl.COLOR_BUFFER_BIT);
655634

656635
this._drawThese(this._drawList, ShaderManager.DRAW_MODE.default, this._projection);
@@ -766,22 +745,12 @@ class RenderWebGL extends EventEmitter {
766745
*/
767746
isTouchingColor (drawableID, color3b, mask3b) {
768747
const candidates = this._candidatesTouching(drawableID, this._visibleDrawList);
769-
770-
let bounds;
771-
if (colorMatches(color3b, this._backgroundColor3b, 0)) {
772-
// If the color we're checking for is the background color, don't confine the check to
773-
// candidate drawables' bounds--since the background spans the entire stage, we must check
774-
// everything that lies inside the drawable.
775-
bounds = this._touchingBounds(drawableID);
776-
// e.g. empty costume, or off the stage
777-
if (bounds === null) return false;
778-
} else if (candidates.length === 0) {
779-
// If not checking for the background color, we can return early if there are no candidate drawables.
748+
if (candidates.length === 0) {
780749
return false;
781-
} else {
782-
bounds = this._candidatesBounds(candidates);
783750
}
784751

752+
const bounds = this._candidatesBounds(candidates);
753+
785754
const maxPixelsForCPU = this._getMaxPixelsForCPU();
786755

787756
const debugCanvasContext = this._debugCanvas && this._debugCanvas.getContext('2d');
@@ -842,19 +811,6 @@ class RenderWebGL extends EventEmitter {
842811
}
843812
}
844813

845-
_enterDrawBackground () {
846-
const gl = this.gl;
847-
const currentShader = this._shaderManager.getShader(ShaderManager.DRAW_MODE.background, 0);
848-
gl.disable(gl.BLEND);
849-
gl.useProgram(currentShader.program);
850-
twgl.setBuffersAndAttributes(gl, currentShader, this._bufferInfo);
851-
}
852-
853-
_exitDrawBackground () {
854-
const gl = this.gl;
855-
gl.enable(gl.BLEND);
856-
}
857-
858814
_isTouchingColorGpuStart (drawableID, candidateIDs, bounds, color3b, mask3b) {
859815
this._doExitDrawRegion();
860816

@@ -866,8 +822,15 @@ class RenderWebGL extends EventEmitter {
866822
gl.viewport(0, 0, bounds.width, bounds.height);
867823
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1);
868824

869-
// Clear the query buffer to fully transparent. This will be the color of pixels that fail the stencil test.
870-
gl.clearColor(0, 0, 0, 0);
825+
let fillBackgroundColor = this._backgroundColor;
826+
827+
// When using masking such that the background fill color will showing through, ensure we don't
828+
// fill using the same color that we are trying to detect!
829+
if (color3b[0] > 196 && color3b[1] > 196 && color3b[2] > 196) {
830+
fillBackgroundColor = [0, 0, 0, 255];
831+
}
832+
833+
gl.clearColor.apply(gl, fillBackgroundColor);
871834
gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
872835

873836
let extraUniforms;
@@ -879,9 +842,6 @@ class RenderWebGL extends EventEmitter {
879842
}
880843

881844
try {
882-
// Using the stencil buffer, mask out the drawing to either the drawable's alpha channel
883-
// or pixels of the drawable which match the mask color, depending on whether a mask color is given.
884-
// Masked-out pixels will not be checked.
885845
gl.enable(gl.STENCIL_TEST);
886846
gl.stencilFunc(gl.ALWAYS, 1, 1);
887847
gl.stencilOp(gl.KEEP, gl.KEEP, gl.REPLACE);
@@ -902,25 +862,12 @@ class RenderWebGL extends EventEmitter {
902862
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
903863
gl.colorMask(true, true, true, true);
904864

905-
// Draw the background as a quad. Drawing a background with gl.clear will not mask to the stenciled area.
906-
this.enterDrawRegion(this._backgroundDrawRegionId);
907-
908-
const uniforms = {
909-
u_backgroundColor: this._backgroundColor4f
910-
};
911-
912-
const currentShader = this._shaderManager.getShader(ShaderManager.DRAW_MODE.background, 0);
913-
twgl.setUniforms(currentShader, uniforms);
914-
twgl.drawBufferInfo(gl, this._bufferInfo, gl.TRIANGLES);
915-
916-
// Draw the candidate drawables on top of the background.
917865
this._drawThese(candidateIDs, ShaderManager.DRAW_MODE.default, projection,
918866
{idFilterFunc: testID => testID !== drawableID}
919867
);
920868
} finally {
921869
gl.colorMask(true, true, true, true);
922870
gl.disable(gl.STENCIL_TEST);
923-
this._doExitDrawRegion();
924871
}
925872
}
926873

@@ -939,8 +886,7 @@ class RenderWebGL extends EventEmitter {
939886
}
940887

941888
for (let pixelBase = 0; pixelBase < pixels.length; pixelBase += 4) {
942-
// Transparent pixels are masked (either by the drawable's alpha channel or color mask).
943-
if (pixels[pixelBase + 3] !== 0 && colorMatches(color3b, pixels, pixelBase)) {
889+
if (colorMatches(color3b, pixels, pixelBase)) {
944890
return true;
945891
}
946892
}
@@ -1375,7 +1321,7 @@ class RenderWebGL extends EventEmitter {
13751321
gl.viewport(0, 0, bounds.width, bounds.height);
13761322
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1);
13771323

1378-
gl.clearColor.apply(gl, this._backgroundColor4f);
1324+
gl.clearColor.apply(gl, this._backgroundColor);
13791325
gl.clear(gl.COLOR_BUFFER_BIT);
13801326
this._drawThese(this._drawList, ShaderManager.DRAW_MODE.default, projection);
13811327

@@ -1465,13 +1411,6 @@ class RenderWebGL extends EventEmitter {
14651411
// Update the CPU position data
14661412
drawable.updateCPURenderAttributes();
14671413
const candidateBounds = drawable.getFastBounds();
1468-
1469-
// Push bounds out to integers. If a drawable extends out into half a pixel, that half-pixel still
1470-
// needs to be tested. Plus, in some areas we construct another rectangle from the union of these,
1471-
// and iterate over its pixels (width * height). Turns out that doesn't work so well when the
1472-
// width/height aren't integers.
1473-
candidateBounds.snapToInt();
1474-
14751414
if (bounds.intersects(candidateBounds)) {
14761415
result.push({
14771416
id,

src/ShaderManager.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ ShaderManager.DRAW_MODE = {
176176
/**
177177
* Draw a line with caps.
178178
*/
179-
line: 'line',
180-
181-
/**
182-
* Draw the background in a certain color. Must sometimes be used instead of gl.clear.
183-
*/
184-
background: 'background'
179+
line: 'line'
185180
};
186181

187182
module.exports = ShaderManager;

src/Silhouette.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
*/
1111
let __SilhouetteUpdateCanvas;
1212

13-
// Optimized Math.min and Math.max for integers;
14-
// taken from https://web.archive.org/web/20190716181049/http://guihaire.com/code/?p=549
15-
const intMin = (i, j) => j ^ ((i ^ j) & ((i - j) >> 31));
16-
const intMax = (i, j) => i ^ ((i ^ j) & ((i - j) >> 31));
17-
1813
/**
1914
* Internal helper function (in hopes that compiler can inline). Get a pixel
2015
* from silhouette data, or 0 if outside it's bounds.
@@ -45,18 +40,13 @@ const __cornerWork = [
4540
/**
4641
* Get the color from a given silhouette at an x/y local texture position.
4742
* Multiply color values by alpha for proper blending.
48-
* @param {Silhouette} $0 The silhouette to sample.
49-
* @param {number} x X position of texture [0, width).
50-
* @param {number} y Y position of texture [0, height).
43+
* @param {Silhouette} The silhouette to sample.
44+
* @param {number} x X position of texture (0-1).
45+
* @param {number} y Y position of texture (0-1).
5146
* @param {Uint8ClampedArray} dst A color 4b space.
5247
* @return {Uint8ClampedArray} The dst vector.
5348
*/
5449
const getColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => {
55-
// Clamp coords to edge, matching GL_CLAMP_TO_EDGE.
56-
// (See github.com/LLK/scratch-render/blob/954cfff02b08069a082cbedd415c1fecd9b1e4fb/src/BitmapSkin.js#L88)
57-
x = intMax(0, intMin(x, width - 1));
58-
y = intMax(0, intMin(y, height - 1));
59-
6050
// 0 if outside bounds, otherwise read from data.
6151
if (x >= width || y >= height || x < 0 || y < 0) {
6252
return dst.fill(0);
@@ -74,17 +64,17 @@ const getColor4b = ({_width: width, _height: height, _colorData: data}, x, y, ds
7464
/**
7565
* Get the color from a given silhouette at an x/y local texture position.
7666
* Do not multiply color values by alpha, as it has already been done.
77-
* @param {Silhouette} $0 The silhouette to sample.
78-
* @param {number} x X position of texture [0, width).
79-
* @param {number} y Y position of texture [0, height).
67+
* @param {Silhouette} The silhouette to sample.
68+
* @param {number} x X position of texture (0-1).
69+
* @param {number} y Y position of texture (0-1).
8070
* @param {Uint8ClampedArray} dst A color 4b space.
8171
* @return {Uint8ClampedArray} The dst vector.
8272
*/
8373
const getPremultipliedColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => {
84-
// Clamp coords to edge, matching GL_CLAMP_TO_EDGE.
85-
x = intMax(0, intMin(x, width - 1));
86-
y = intMax(0, intMin(y, height - 1));
87-
74+
// 0 if outside bounds, otherwise read from data.
75+
if (x >= width || y >= height || x < 0 || y < 0) {
76+
return dst.fill(0);
77+
}
8878
const offset = ((y * width) + x) * 4;
8979
dst[0] = data[offset];
9080
dst[1] = data[offset + 1];

src/shaders/sprite.frag

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ uniform float u_lineThickness;
3939
uniform float u_lineLength;
4040
#endif // DRAW_MODE_line
4141

42-
#ifdef DRAW_MODE_background
43-
uniform vec4 u_backgroundColor;
44-
#endif // DRAW_MODE_background
45-
4642
uniform sampler2D u_skin;
4743

48-
#ifndef DRAW_MODE_background
4944
varying vec2 v_texCoord;
50-
#endif
5145

5246
// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations.
5347
// Smaller values can cause problems on some mobile devices.
@@ -115,7 +109,7 @@ const vec2 kCenter = vec2(0.5, 0.5);
115109

116110
void main()
117111
{
118-
#if !(defined(DRAW_MODE_line) || defined(DRAW_MODE_background))
112+
#ifndef DRAW_MODE_line
119113
vec2 texcoord0 = v_texCoord;
120114

121115
#ifdef ENABLE_mosaic
@@ -221,9 +215,7 @@ void main()
221215
gl_FragColor.rgb /= gl_FragColor.a + epsilon;
222216
#endif
223217

224-
#endif // !(defined(DRAW_MODE_line) || defined(DRAW_MODE_background))
225-
226-
#ifdef DRAW_MODE_line
218+
#else // DRAW_MODE_line
227219
// Maaaaagic antialiased-line-with-round-caps shader.
228220

229221
// "along-the-lineness". This increases parallel to the line.
@@ -242,8 +234,4 @@ void main()
242234
// the closer we are to the line, invert it.
243235
gl_FragColor = u_lineColor * clamp(1.0 - line, 0.0, 1.0);
244236
#endif // DRAW_MODE_line
245-
246-
#ifdef DRAW_MODE_background
247-
gl_FragColor = u_backgroundColor;
248-
#endif
249237
}

src/shaders/sprite.vert

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ uniform vec4 u_penPoints;
1414
const float epsilon = 1e-3;
1515
#endif
1616

17-
#if !(defined(DRAW_MODE_line) || defined(DRAW_MODE_background))
17+
#ifndef DRAW_MODE_line
1818
uniform mat4 u_projectionMatrix;
1919
uniform mat4 u_modelMatrix;
2020
attribute vec2 a_texCoord;
@@ -66,8 +66,6 @@ void main() {
6666
// 4. Apply view transform
6767
position *= 2.0 / u_stageSize;
6868
gl_Position = vec4(position, 0, 1);
69-
#elif defined(DRAW_MODE_background)
70-
gl_Position = vec4(a_position * 2.0, 0, 1);
7169
#else
7270
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1);
7371
v_texCoord = a_texCoord;
-54.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)