Skip to content

Commit abb82c7

Browse files
committed
Put back premultiplied alpha settings in WebGL from before
1 parent 2d5b225 commit abb82c7

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

src/core/filterShaders.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,14 @@ 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-
const texSample = p5.getTexture(canvasContent , sampleCoord);
106-
avg += weight * (texSample * p5.vec4(
105+
const texSample = p5.getTexture(canvasContent, sampleCoord);
106+
avg += weight * texSample * p5.vec4(
107107
texSample.a, texSample.a, texSample.a, 1
108-
));
108+
);
109109
total += weight;
110110
}
111111

112112
const blended = avg / total;
113-
114113
return p5.vec4(
115114
blended.r / blended.a,
116115
blended.g / blended.a,

src/core/p5.Renderer3D.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,9 @@ export class Renderer3D extends Renderer {
16691669
//// TEXT SUPPORT METHODS
16701670
//////////////////////////////
16711671

1672+
_beforeDrawText() {}
1673+
_afterDrawText() {}
1674+
16721675
textCanvas() {
16731676
if (!this._textCanvas) {
16741677
this._textCanvas = document.createElement('canvas');

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

src/webgl/text.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ function text(p5, fn) {
766766

767767
// this will have to do for now...
768768
sh.setUniform('uMaterialColor', curFillColor);
769-
769+
this._beforeDrawText();
770770
this.glyphDataCache = this.glyphDataCache || new Set();
771771

772772
try {
@@ -827,6 +827,7 @@ function text(p5, fn) {
827827
this.states.setValue('strokeColor', doStroke);
828828
this.states.setValue('drawMode', drawMode);
829829

830+
this._afterDrawText();
830831
this.pop();
831832
}
832833
};

0 commit comments

Comments
 (0)