Skip to content

Commit abc7e35

Browse files
committed
Set transparent if canvas uses alpha
1 parent fecd30e commit abc7e35

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/materials/EffectMaterial.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IUniform, Texture, Uniform } from "three";
1+
import { IUniform, NormalBlending, Texture, Uniform } from "three";
22
import { EffectShaderSection } from "../enums/EffectShaderSection.js";
33
import { EffectShaderSection as Section } from "../enums/EffectShaderSection.js";
44
import { ShaderDataTracker } from "../utils/ShaderDataTracker.js";
@@ -29,6 +29,7 @@ export class EffectMaterial extends FullscreenMaterial {
2929

3030
super({
3131
name: "EffectMaterial",
32+
blending: NormalBlending,
3233
defines: {
3334
COLOR_SPACE_CONVERSION: true
3435
},

src/passes/EffectPass.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event as Event3, Material, Texture } from "three";
1+
import { Event as Event3, Material, Texture, WebGLRenderer } from "three";
22
import { Pass } from "../core/Pass.js";
33
import { Effect } from "../effects/Effect.js";
44
import { GBuffer } from "../enums/GBuffer.js";
@@ -76,6 +76,19 @@ export class EffectPass extends Pass<EffectMaterial> {
7676

7777
}
7878

79+
override get renderer(): WebGLRenderer | null {
80+
81+
return super.renderer;
82+
83+
}
84+
85+
override set renderer(value: WebGLRenderer | null) {
86+
87+
super.renderer = value;
88+
this.updateMaterialTransparency();
89+
90+
}
91+
7992
override get subpasses(): readonly Pass<Material | null>[] {
8093

8194
return super.subpasses;
@@ -188,6 +201,7 @@ export class EffectPass extends Pass<EffectMaterial> {
188201

189202
// Get the material for the current effect combination.
190203
this.fullscreenMaterial = this.effectMaterialManager.getMaterial(this.effects);
204+
this.updateMaterialTransparency();
191205

192206
// Pick up new materials.
193207
for(const material of this.effectMaterialManager.materials) {
@@ -198,6 +212,24 @@ export class EffectPass extends Pass<EffectMaterial> {
198212

199213
}
200214

215+
/**
216+
* Updates the fullscreen material to use alpha blending if needed.
217+
*/
218+
219+
private updateMaterialTransparency(): void {
220+
221+
if(this.renderer === null) {
222+
223+
return;
224+
225+
}
226+
227+
const alpha = this.renderer.getContext().getContextAttributes()?.alpha ?? false;
228+
const outputBuffer = this.output.defaultBuffer?.value ?? null;
229+
this.fullscreenMaterial.transparent = (alpha && outputBuffer === null);
230+
231+
}
232+
201233
/**
202234
* Updates the G-Buffer struct uniform.
203235
*/
@@ -340,6 +372,12 @@ export class EffectPass extends Pass<EffectMaterial> {
340372

341373
}
342374

375+
protected override onOutputChange(): void {
376+
377+
this.updateMaterialTransparency();
378+
379+
}
380+
343381
override checkRequirements(): void {
344382

345383
for(const effect of this.effects) {

0 commit comments

Comments
 (0)