Skip to content

Commit

Permalink
Add Alpha Control to CompositionBlock's Foreground (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexchuber authored Sep 19, 2024
1 parent 3e61728 commit 1aae21f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/demo/src/configuration/blocks/effects/compositionBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const shaderProgram = injectDisableUniform({
uniform vec2 _scaleUV_;
uniform vec2 _translateUV_;
uniform int _alphaMode_;
uniform float _foregroundAlphaScale_;
`,

mainFunctionName: "_composition_",
Expand All @@ -49,6 +50,7 @@ const shaderProgram = injectDisableUniform({
}
vec4 foreground = texture2D(_foreground_, transformedUV);
foreground.a *= _foregroundAlphaScale_;
if (_alphaMode_ == 0) {
result = foreground;
Expand Down Expand Up @@ -86,6 +88,7 @@ export class CompositionShaderBinding extends ShaderBinding {
private readonly _foregroundLeft: RuntimeData<ConnectionPointType.Float>;
private readonly _foregroundWidth: RuntimeData<ConnectionPointType.Float>;
private readonly _foregroundHeight: RuntimeData<ConnectionPointType.Float>;
private readonly _foregroundAlphaScale: RuntimeData<ConnectionPointType.Float>;
private readonly _alphaMode: number;

/**
Expand All @@ -97,6 +100,7 @@ export class CompositionShaderBinding extends ShaderBinding {
* @param foregroundLeft - the left position of the foreground texture
* @param foregroundWidth - the width of the foreground texture
* @param foregroundHeight - the height of the foreground texture
* @param foregroundAlphaScale - the alpha scale of the foreground texture
* @param alphaMode - the alpha mode to use
*/
constructor(
Expand All @@ -107,6 +111,7 @@ export class CompositionShaderBinding extends ShaderBinding {
foregroundLeft: RuntimeData<ConnectionPointType.Float>,
foregroundWidth: RuntimeData<ConnectionPointType.Float>,
foregroundHeight: RuntimeData<ConnectionPointType.Float>,
foregroundAlphaScale: RuntimeData<ConnectionPointType.Float>,
alphaMode: number
) {
super(parentBlock);
Expand All @@ -116,6 +121,7 @@ export class CompositionShaderBinding extends ShaderBinding {
this._foregroundLeft = foregroundLeft;
this._foregroundWidth = foregroundWidth;
this._foregroundHeight = foregroundHeight;
this._foregroundAlphaScale = foregroundAlphaScale;
this._alphaMode = alphaMode;
}

Expand All @@ -134,6 +140,7 @@ export class CompositionShaderBinding extends ShaderBinding {
const foregroundLeft = this._foregroundLeft.value;
const foregroundWidth = this._foregroundWidth.value;
const foregroundHeight = this._foregroundHeight.value;
const foregroundAlphaScale = this._foregroundAlphaScale.value;
const alphaMode = this._alphaMode;

effect.setInt(this.getRemappedName("alphaMode"), alphaMode);
Expand All @@ -143,6 +150,7 @@ export class CompositionShaderBinding extends ShaderBinding {
if (foreground) {
effect.setFloat2(this.getRemappedName("scaleUV"), foregroundWidth, foregroundHeight);
effect.setFloat2(this.getRemappedName("translateUV"), -1 * foregroundLeft, foregroundTop);
effect.setFloat(this.getRemappedName("foregroundAlphaScale"), foregroundAlphaScale);
}
}
}
Expand Down Expand Up @@ -213,6 +221,15 @@ export class CompositionBlock extends ShaderBlock {
createStrongRef(1.0)
);

/**
* Defines a multiplier applied to the foreground's alpha channel.
*/
public readonly foregroundAlphaScale = this._registerOptionalInput(
"foregroundAlphaScale",
ConnectionPointType.Float,
createStrongRef(1.0)
);

/**
* Defines blend mode of the composition.
*/
Expand Down Expand Up @@ -243,6 +260,7 @@ export class CompositionBlock extends ShaderBlock {
const foregroundLeft = this.foregroundLeft.runtimeData;
const foregroundHeight = this.foregroundHeight.runtimeData;
const foregroundTop = this.foregroundTop.runtimeData;
const foregroundAlphaScale = this.foregroundAlphaScale.runtimeData;
const alphaMode = this.alphaMode;

return new CompositionShaderBinding(
Expand All @@ -253,6 +271,7 @@ export class CompositionBlock extends ShaderBlock {
foregroundLeft,
foregroundWidth,
foregroundHeight,
foregroundAlphaScale,
alphaMode
);
}
Expand Down

0 comments on commit 1aae21f

Please sign in to comment.