Skip to content

Commit 95ef25a

Browse files
authored
Add "alphaMask" option for ImageOverlays (#1372)
* Add support for using an overlay layer as a mask * Update dts * Remove alphaTest option * Use #defines * Revert "Use #defines" This reverts commit 1a0ba67.
1 parent 201752c commit 95ef25a

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

src/three/plugins/images/ImageOverlayPlugin.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export class ImageOverlayPlugin {
88
renderer: WebGLRenderer,
99
resolution?: number,
1010
enableTileSplitting?: boolean,
11+
alphaMask?: boolean, // false = fade to the layer below, true = use only alpha to fade all layers underneath
12+
alphaInvert?: boolean, // false = cut inside (keep outside); true = cut outside (keep inside)
1113
} );
1214

1315
addOverlay( overlay: ImageOverlay, order?: number ): void;

src/three/plugins/images/ImageOverlayPlugin.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,11 +1319,11 @@ export class ImageOverlayPlugin {
13191319

13201320
// set the uniform array lengths
13211321
params.layerMaps.length = overlays.length;
1322-
params.layerColor.length = overlays.length;
1322+
params.layerInfo.length = overlays.length;
13231323

13241324
// assign the uniforms
13251325
params.layerMaps.value[ i ] = target !== null ? target.texture : null;
1326-
params.layerColor.value[ i ] = overlay;
1326+
params.layerInfo.value[ i ] = overlay;
13271327

13281328
material.defines.LAYER_COUNT = overlays.length;
13291329
material.needsUpdate = true;
@@ -1420,13 +1420,18 @@ class ImageOverlay {
14201420
color = 0xffffff,
14211421
frame = null,
14221422
preprocessURL = null,
1423+
alphaMask = false,
1424+
alphaInvert = false,
14231425
} = options;
14241426
this.imageSource = null;
14251427

14261428
this.preprocessURL = preprocessURL;
14271429
this.opacity = opacity;
14281430
this.color = new Color( color );
14291431
this.frame = frame !== null ? frame.clone() : null;
1432+
this.alphaMask = alphaMask;
1433+
this.alphaInvert = alphaInvert;
1434+
14301435
this.isReady = false;
14311436
this.isInitialized = false;
14321437

src/three/plugins/images/overlays/wrapOverlaysMaterial.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function wrapOverlaysMaterial( material, previousOnBeforeCompile ) {
1212

1313
const params = {
1414
layerMaps: { value: [] },
15-
layerColor: { value: [] },
15+
layerInfo: { value: [] },
1616
};
1717

1818
material[ OVERLAY_PARAMS ] = params;
@@ -74,13 +74,16 @@ export function wrapOverlaysMaterial( material, previousOnBeforeCompile ) {
7474
.replace( /void main\(/, value => /* glsl */`
7575
7676
#if LAYER_COUNT != 0
77-
struct LayerTint {
77+
struct LayerInfo {
7878
vec3 color;
7979
float opacity;
80+
81+
int alphaMask;
82+
int alphaInvert;
8083
};
8184
8285
uniform sampler2D layerMaps[ LAYER_COUNT ];
83-
uniform LayerTint layerColor[ LAYER_COUNT ];
86+
uniform LayerInfo layerInfo[ LAYER_COUNT ];
8487
#endif
8588
8689
#pragma unroll_loop_start
@@ -126,11 +129,27 @@ export function wrapOverlaysMaterial( material, previousOnBeforeCompile ) {
126129
smoothstep( 1.0 + wDelta, 1.0, layerUV.z );
127130
128131
// apply tint & opacity
129-
tint.rgb *= layerColor[ i ].color;
130-
tint.rgba *= layerColor[ i ].opacity * wOpacity;
132+
tint.rgb *= layerInfo[ i ].color;
133+
tint.rgba *= layerInfo[ i ].opacity * wOpacity;
134+
135+
// invert the alpha
136+
if ( layerInfo[ i ].alphaInvert > 0 ) {
137+
138+
tint.a = 1.0 - tint.a;
139+
140+
}
141+
142+
// apply the alpha across all existing layers if alpha mask is true
143+
if ( layerInfo[ i ].alphaMask > 0 ) {
144+
145+
diffuseColor.a *= tint.a;
146+
147+
} else {
148+
149+
// premultiplied alpha equation
150+
diffuseColor = tint + diffuseColor * ( 1.0 - tint.a );
131151
132-
// premultiplied alpha equation
133-
diffuseColor = tint + diffuseColor * ( 1.0 - tint.a );
152+
}
134153
135154
#endif
136155

0 commit comments

Comments
 (0)