Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add opacity to layers and fix transparent blending #3

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/lib/engines/layerCompositor/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ class LayerTexture {
public url: string,
public texImage2D: TexImage2D,
public offset: Vector2,
public uvScaleFactor = new Vector2(1, -1),
public opacity = 1,
public uvScaleFactor = new Vector2(1, 1),
public uvOffset = new Vector2(0, 1),
) {
// console.log(`Layer: size ( ${texImage2D.size.x}, ${texImage2D.size.y} ) `);

// world space is assumed to be in layer pixel space
const planeToLayer = makeMatrix4Scale(new Vector3(this.texImage2D.size.width, this.texImage2D.size.height, 1.0));
const planeToLayer = makeMatrix4Scale(
new Vector3(
this.texImage2D.size.width * this.uvScaleFactor.width,
this.texImage2D.size.height * this.uvScaleFactor.height,
1.0,
),
);
const layerToImage = makeMatrix4Translation(new Vector3(this.offset.x, this.offset.y, 0.0));
this.planeToImage = makeMatrix4Concatenation(layerToImage, planeToLayer);

Expand Down Expand Up @@ -102,10 +107,11 @@ export class LayerMask extends LayerTexture {
texImage2D: TexImage2D,
offset: Vector2,
public mode: LayerMaskMode = LayerMaskMode.Luminance,
uvScaleFactor = new Vector2(1, -1),
opacity = 1,
uvScaleFactor = new Vector2(1, 1),
uvOffset = new Vector2(0, 1),
) {
super(compositor, url, texImage2D, offset, uvScaleFactor, uvOffset);
super(compositor, url, texImage2D, offset, opacity, uvScaleFactor, uvOffset);
}
}

Expand All @@ -117,10 +123,11 @@ export class Layer extends LayerTexture {
offset: Vector2,
public mask?: LayerMask,
public blendMode: LayerBlendMode = LayerBlendMode.SrcOver,
uvScaleFactor = new Vector2(1, -1),
opacity = 1,
uvScaleFactor = new Vector2(1, 1),
uvOffset = new Vector2(0, 1),
) {
super(compositor, url, texImage2D, offset, uvScaleFactor, uvOffset);
super(compositor, url, texImage2D, offset, opacity, uvScaleFactor, uvOffset);
}

public get isTriviallyBlended(): boolean {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/engines/layerCompositor/LayerCompositor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export class LayerCompositor {

maskMode: 0,
blendMode: 0,
opacity: 1,
};

//console.log(`drawing layer #${index}: ${layer.url} at ${layer.offset.x}, ${layer.offset.y}`);
Expand Down Expand Up @@ -402,6 +403,7 @@ export class LayerCompositor {

maskMode: 0,
blendMode: 0,
opacity: 1,
};

renderBufferGeometry(
Expand Down Expand Up @@ -430,6 +432,7 @@ export class LayerCompositor {

maskMode: 0,
blendMode: layer.blendModeUniformValue,
opacity: layer.opacity,
};

if (mask) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/engines/layerCompositor/blend.frag
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ vec4 compositeColors(vec4 src, vec4 dst) {
}

return vec4(
IDa * Sc + ISa * Dc + Sa * Da * blend,
IDa * Sc * Sa + ISa * Dc * Da + Sa * Da * blend,
Sa + ISa * Da
);
}
14 changes: 8 additions & 6 deletions src/lib/engines/layerCompositor/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ uniform int convertToPremultipliedAlpha;
uniform int maskMode;
uniform int blendMode;

uniform float opacity;

#pragma include "./mask.frag"
#pragma include "./blend.frag"

Expand All @@ -26,6 +28,7 @@ void main() {
}

vec4 outputColor = layerColor;
outputColor *= opacity;

if( maskMode != 0 ){

Expand All @@ -51,13 +54,12 @@ void main() {
}

if( blendMode != 0){
vec4 imageColor = texture2D( imageMap, v_image_uv, mipmapBias );

// Image always comes from premultiplied backbuffer
vec4 imageColor = texture2D( imageMap, v_image_uv, mipmapBias ); // non-premultiplied

outputColor = compositeColors( outputColor, imageColor );
// convert to non-premultiplied
outputColor.rgb = outputColor.a > (0.5 / 255.) ? outputColor.rgb / outputColor.a : vec3(0);
outputColor = compositeColors( outputColor, imageColor ); // output is premultiplied, inputs are not
}

gl_FragColor = outputColor;

}
}
Loading