Skip to content

Commit

Permalink
fix: fix black fringes: V-Ray PNGs are actually premultipled alpha ag…
Browse files Browse the repository at this point in the history
…ainst PNG spec.
  • Loading branch information
bhouston committed Oct 27, 2020
1 parent 8e77d45 commit e74490d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/lib/engines/layerCompositor/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class Layer {
public offset: Vector2,
public uvScaleFactor = new Vector2(1, -1),
public uvOffset = new Vector2(0, 1),
public premultipliedAlpha = true,
) {
// console.log(`Layer: size ( ${texImage2D.size.x}, ${texImage2D.size.y} ) `);

Expand Down
6 changes: 3 additions & 3 deletions src/lib/engines/layerCompositor/LayerCompositor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class LayerCompositor {
);
const uvToTexture = makeMatrix3Concatenation(uvTranslation, uvScale);

canvasFramebuffer.clearState = this.clearState; // new ClearState(new Vector3(1, 1, 1), 0.0);
canvasFramebuffer.clearState = new ClearState(new Vector3(0, 0, 0), 0.0);
canvasFramebuffer.clear();

const offscreenColorAttachment = this.offscreenColorAttachment;
Expand Down Expand Up @@ -280,10 +280,10 @@ export class LayerCompositor {
layerMap: layer.texImage2D,
uvToTexture: layer.uvToTexture,
mipmapBias: 0,
premultipliedAlpha: layer.premultipliedAlpha ? 1 : 0,
premultipliedAlpha: 0,
};

const blendState = blendModeToBlendState(Blending.Over, layer.premultipliedAlpha);
const blendState = blendModeToBlendState(Blending.Over, true);

// console.log(`drawing layer #${index}: ${layer.url} at ${layer.offset.x}, ${layer.offset.y}`);
renderBufferGeometry(offscreenFramebuffer, this.#program, uniforms, this.#bufferGeometry, undefined, blendState);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/engines/layerCompositor/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ varying vec3 v_viewPosition;
varying vec3 v_viewNormal;
varying vec2 v_uv;

#pragma include <math/math>
#pragma include <color/spaces/srgb>

void main() {
vec3 outputColor = vec3(0.);
vec2 texelUv = ( uvToTexture * vec3( v_uv, 1.0 ) ).xy;

vec4 layerColor = texture2D( layerMap, texelUv, mipmapBias );
outputColor += sRGBToLinear( layerColor.rgb );

gl_FragColor.rgb = linearTosRGB( outputColor );
// premultiply alpha in output as the source PNG is not premultiplied
if( premultipliedAlpha == 1 ) {
gl_FragColor.rgb *= layerColor.a;
layerColor.rgb *= layerColor.a;
}
gl_FragColor.a = layerColor.a;

gl_FragColor = layerColor;

}

0 comments on commit e74490d

Please sign in to comment.