Skip to content

Commit 3c953fe

Browse files
authored
Merge branch 'dev-2.0' into fix-7917-dev2
2 parents c5eb586 + f557d24 commit 3c953fe

13 files changed

Lines changed: 91 additions & 23 deletions

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test/**/*.js": "eslint",
2020
"utils/**/*.{js,mjs}": "eslint"
2121
},
22-
"version": "2.2.0-rc.2",
22+
"version": "2.2.0-rc.3",
2323
"dependencies": {
2424
"@davepagurek/bezier-path": "^0.0.2",
2525
"@japont/unicode-range": "^1.0.0",

src/core/filterShaders.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,20 @@ export function makeFilterShader(renderer, operation, p5) {
102102
const sampleCoord = uv + p5.vec2(sample, sample) / inputs.canvasSize * direction;
103103
const weight = quadWeight(sample, (numSamples - 1.0) * 0.5 * spacing);
104104

105-
avg += weight * p5.getTexture(canvasContent, sampleCoord);
105+
const texSample = p5.getTexture(canvasContent, sampleCoord);
106+
avg += weight * texSample * p5.vec4(
107+
texSample.a, texSample.a, texSample.a, 1
108+
);
106109
total += weight;
107110
}
108111

109-
return avg / total;
112+
const blended = avg / total;
113+
return p5.vec4(
114+
blended.r / blended.a,
115+
blended.g / blended.a,
116+
blended.b / blended.a,
117+
blended.a
118+
);
110119
});
111120
}, { p5 });
112121

src/core/p5.Renderer3D.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,9 @@ export class Renderer3D extends Renderer {
16901690
//// TEXT SUPPORT METHODS
16911691
//////////////////////////////
16921692

1693+
_beforeDrawText() {}
1694+
_afterDrawText() {}
1695+
16931696
textCanvas() {
16941697
if (!this._textCanvas) {
16951698
this._textCanvas = document.createElement('canvas');

src/strands/strands_for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ export class StrandsFor {
297297
const scopeEndBlock = CFG.createBasicBlock(cfg, BlockType.SCOPE_END);
298298
CFG.addEdge(cfg, updateBlock, scopeEndBlock);
299299

300-
// Loop back to break check
301-
CFG.addEdge(cfg, scopeEndBlock, breakCheckBlock);
300+
// Connect end of for loop to the merge agter the loop
301+
CFG.addEdge(cfg, scopeEndBlock, mergeBlock);
302302

303303
// Break condition exits to merge
304304
CFG.addEdge(cfg, breakCheckBlock, mergeBlock);

src/webgl/p5.Camera.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,8 +1602,7 @@ class Camera {
16021602
);
16031603
// If the camera is active, make uPMatrix reflect changes in projMatrix.
16041604
if (this._isActive()) {
1605-
this._renderer.states.setValue('uPMatrix', this._renderer.states.uPMatrix.clone());
1606-
this._renderer.states.uPMatrix.mat4 = this.projMatrix.mat4.slice();
1605+
this._renderer.states.setValue('uPMatrix', this.projMatrix.clone());
16071606
}
16081607
}
16091608

src/webgl/p5.RendererGL.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ class RendererGL extends Renderer3D {
312312
}
313313
}
314314

315+
//////////////////////////////////////////////
316+
// Text
317+
//////////////////////////////////////////////
318+
319+
_beforeDrawText() {
320+
this.GL.pixelStorei(this.GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
321+
}
322+
_afterDrawText() {
323+
this.GL.pixelStorei(this.GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
324+
}
325+
315326
//////////////////////////////////////////////
316327
// Setting
317328
//////////////////////////////////////////////
@@ -413,9 +424,10 @@ class RendererGL extends Renderer3D {
413424
gl.enable(gl.DEPTH_TEST);
414425
gl.depthFunc(gl.LEQUAL);
415426
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
416-
// Make sure all images are loaded into the canvas non-premultiplied so that
417-
// they can be handled consistently in shaders.
418-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
427+
// Make sure all images are loaded into the canvas premultiplied so that
428+
// they match the way we render colors. This will make framebuffer textures
429+
// be encoded the same way as textures from everything else.
430+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
419431
this._viewport = this.drawingContext.getParameter(
420432
this.drawingContext.VIEWPORT
421433
);

src/webgl/shaders/light_texture.frag

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ void main(void) {
1414
}
1515
else {
1616
vec4 baseColor = isTexture
17-
// Textures come in with non-premultiplied alpha. Apply tint.
18-
? TEXTURE(uSampler, vVertTexCoord) * (uTint/255.)
19-
// Colors come in with non-premultiplied alpha.
20-
: vColor;
21-
// Convert to premultiplied alpha for consistent output
22-
baseColor.rgb *= baseColor.a;
17+
// Textures come in with premultiplied alpha. To apply tint and still have
18+
// premultiplied alpha output, we need to multiply the RGB channels by the
19+
// tint RGB, and all channels by the tint alpha.
20+
? TEXTURE(uSampler, vVertTexCoord) * vec4(uTint.rgb/255., 1.) * (uTint.a/255.)
21+
// Colors come in with unmultiplied alpha, so we need to multiply the RGB
22+
// channels by alpha to convert it to premultiplied alpha.
23+
: vec4(vColor.rgb * vColor.a, vColor.a);
2324
OUT_COLOR = vec4(baseColor.rgb * vDiffuseColor + vSpecularColor, baseColor.a);
2425
}
2526
}

src/webgl/shaders/phong.frag

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ void main(void) {
4747
inputs.texCoord = vTexCoord;
4848
inputs.ambientLight = uAmbientColor;
4949
inputs.color = isTexture
50-
? TEXTURE(uSampler, vTexCoord) * (uTint/255.)
50+
? TEXTURE(uSampler, vTexCoord) * (vec4(uTint.rgb/255., 1.) * uTint.a/255.)
5151
: vColor;
52+
if (isTexture && inputs.color.a > 0.0) {
53+
// Textures come in with premultiplied alpha. Temporarily unpremultiply it
54+
// so hooks users don't have to think about premultiplied alpha.
55+
inputs.color.rgb /= inputs.color.a;
56+
}
5257
inputs.shininess = uShininess;
5358
inputs.metalness = uMetallic;
5459
inputs.ambientMaterial = uHasSetAmbient ? uAmbientMatColor.rgb : inputs.color.rgb;

src/webgl/shaders/webgl2Compatibility.glsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ out vec4 outColor;
2626
#endif
2727

2828
#ifdef FRAGMENT_SHADER
29-
#define getTexture TEXTURE
29+
vec4 getTexture(in sampler2D content, vec2 coord) {
30+
vec4 color = TEXTURE(content, coord);
31+
if (color.a > 0.) color.rgb /= color.a;
32+
return color;
33+
}
3034
#endif

0 commit comments

Comments
 (0)