Skip to content

Commit

Permalink
Use un-multiplied alpha in shader hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Dec 18, 2024
1 parent 2ada04a commit ddb3a33
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/webgl/shaders/phong.frag
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ void main(void) {
inputs.texCoord = vTexCoord;
inputs.ambientLight = vAmbientColor;
inputs.color = isTexture
// Textures come in with premultiplied alpha. To apply tint and still have
// premultiplied alpha output, we need to multiply the RGB channels by the
// tint RGB, and all channels by the tint alpha.
? TEXTURE(uSampler, vTexCoord) * vec4(uTint.rgb/255., 1.) * (uTint.a/255.)
// Colors come in with unmultiplied alpha, so we need to multiply the RGB
// channels by alpha to convert it to premultiplied alpha.
: vec4(vColor.rgb * vColor.a, vColor.a);
? TEXTURE(uSampler, vTexCoord) * uTint/255.
: vColor;
if (isTexture) {
// Textures come in with premultiplied alpha. Temporarily unpremultiply it
// so hooks users don't have to think about premultiplied alpha.
inputs.color.rgb /= inputs.color.a;
}
inputs.shininess = uShininess;
inputs.metalness = uMetallic;
inputs.ambientMaterial = uHasSetAmbient ? uAmbientMatColor.rgb : inputs.color.rgb;
Expand All @@ -79,5 +79,6 @@ void main(void) {
c.specular = specular;
c.emissive = inputs.emissiveMaterial;
OUT_COLOR = HOOK_getFinalColor(HOOK_combineColors(c));
OUT_COLOR.rgb *= OUT_COLOR.a; // Premultiply alpha before rendering
HOOK_afterFragment();
}
54 changes: 54 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,58 @@ visualSuite('WebGL', function() {
screenshot();
})
})

visualSuite('Opacity', function() {
visualTest('Basic colors have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(255);
p5.fill(255, 0, 0, 100);
p5.circle(0, 0, 50);
screenshot();
});

visualTest('Colors have opacity applied correctly when lights are used', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(255);
p5.ambientLight(255);
p5.fill(255, 0, 0, 100);
p5.circle(0, 0, 50);
screenshot();
});

visualTest('Colors in shader hooks have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const myShader = p5.baseMaterialShader().modify({
'Inputs getPixelInputs': `(Inputs inputs) {
inputs.color = vec4(1., 0., 0., 100./255.);
return inputs;
}`
})
p5.background(255);
p5.shader(myShader);
p5.circle(0, 0, 50);
screenshot();
});

visualTest('Colors in textures have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = p5.createFramebuffer();
tex.draw(() => p5.background(255, 0, 0, 100));
p5.background(255);
p5.texture(tex);
p5.circle(0, 0, 50);
screenshot();
});

visualTest('Colors in tinted textures have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = p5.createFramebuffer();
tex.draw(() => p5.background(255, 0, 0, 255));
p5.background(255);
p5.texture(tex);
p5.tint(255, 100);
p5.circle(0, 0, 50);
screenshot();
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}

0 comments on commit ddb3a33

Please sign in to comment.