Skip to content

Commit 1e212cb

Browse files
committed
fix: fix broken render targets on effects
1 parent 1a37be8 commit 1e212cb

File tree

11 files changed

+45
-36
lines changed

11 files changed

+45
-36
lines changed

lib/playground/src/pages/post-processing/bloom.astro

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import Layout from "../../layouts/Layout.astro";
1414
const { onAfterRender } = useWebGLCanvas({
1515
canvas: "#glCanvas",
1616
fragment: fragment,
17-
uniforms: {
18-
uTexture: loadTexture(
19-
"https://st.depositphotos.com/2300153/2648/i/950/depositphotos_26489675-stock-photo-dark-room-with-window.jpg",
20-
// "https://st.depositphotos.com/8521256/56706/v/600/depositphotos_567060820-stock-video-white-picture-of-rectangle-on.jpg",
21-
{ colorSpace: "srgb" }
22-
),
23-
},
17+
// uniforms: {
18+
// uTexture: loadTexture(
19+
// "https://st.depositphotos.com/2300153/2648/i/950/depositphotos_26489675-stock-photo-dark-room-with-window.jpg",
20+
// // "https://st.depositphotos.com/8521256/56706/v/600/depositphotos_567060820-stock-video-white-picture-of-rectangle-on.jpg",
21+
// { colorSpace: "srgb" }
22+
// ),
23+
// },
2424
postEffects: [bloom({ levels, radius, mix }), linearToSRGB()],
2525
});
2626

@@ -30,10 +30,3 @@ import Layout from "../../layouts/Layout.astro";
3030
<Layout title="Bloom">
3131
<canvas id="glCanvas"></canvas>
3232
</Layout>
33-
34-
<style>
35-
canvas {
36-
width: 600px;
37-
height: 600px;
38-
}
39-
</style>

lib/src/effects/bloom/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import { useCompositeEffectPass } from "../../hooks/useCompositeEffectPass";
22
import { useEffectPass } from "../../hooks/useEffectPass";
3-
import type { CompositeEffectPass, EffectPass } from "../../types";
3+
import type { EffectPass } from "../../types";
44

55
import downsampleFragment from "./glsl/downsample.frag?raw";
66
import sampleVertex from "./glsl/sample.vert?raw";
77
import upsampleFragment from "./glsl/upsample.frag?raw";
88
import combineFragment from "./glsl/combine.frag?raw";
9+
import { RenderTargetParams } from "../../core/renderTarget";
910

1011
export type BloomParams = {
1112
levels?: number;
1213
radius?: number;
1314
mix?: number;
1415
};
1516

17+
const floatTargetConfig: RenderTargetParams = {
18+
internalFormat: WebGL2RenderingContext.RGBA16F,
19+
type: WebGL2RenderingContext.HALF_FLOAT,
20+
minFilter: "linear",
21+
magFilter: "linear",
22+
};
23+
1624
export function bloom(params: BloomParams = {}) {
1725
const { levels = 8, radius = 0.65, mix = 0.5 } = params;
1826

@@ -22,6 +30,7 @@ export function bloom(params: BloomParams = {}) {
2230
for (let level = 1; level <= levels; level++) {
2331
const pass = useEffectPass({
2432
fragment: downsampleFragment,
33+
target: floatTargetConfig,
2534
vertex: sampleVertex,
2635
resolutionScale: 1 / 2 ** level,
2736
uniforms: {
@@ -37,6 +46,7 @@ export function bloom(params: BloomParams = {}) {
3746
for (let level = levels - 1; level >= 1; level--) {
3847
const pass = useEffectPass({
3948
fragment: upsampleFragment,
49+
target: floatTargetConfig,
4050
vertex: sampleVertex,
4151
resolutionScale: 1 / 2 ** level,
4252
uniforms: {
@@ -55,6 +65,7 @@ export function bloom(params: BloomParams = {}) {
5565
// --- Combine original + bloom ---
5666
const combine = useEffectPass({
5767
fragment: combineFragment,
68+
target: floatTargetConfig,
5869
uniforms: {
5970
uImage: ({ inputPass }) => inputPass.target!.texture,
6071
uBloomTexture: ({ previousPass }) => previousPass.target!.texture,

lib/src/hooks/useCompositeEffectPass.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createRenderTarget } from "../core/renderTarget";
21
import { useLifeCycleCallback } from "../internal/useLifeCycleCallback";
32
import type {
43
CompositeEffectPass,
@@ -24,13 +23,9 @@ export function useCompositeEffectPass<P extends EffectPass<any>[]>(
2423
}
2524

2625
function initialize(gl: WebGL2RenderingContext) {
27-
for (const [index, pass] of passes.entries()) {
26+
for (const pass of passes) {
2827
pass.initialize(gl);
2928

30-
if (index < passes.length - 1 && pass.target == undefined) {
31-
pass.setTarget(createRenderTarget(gl));
32-
}
33-
3429
pass.onUpdated((newUniforms, oldUniforms) => {
3530
for (const callback of onUpdatedCallbacks) {
3631
callback(newUniforms, oldUniforms);
@@ -50,7 +45,9 @@ export function useCompositeEffectPass<P extends EffectPass<any>[]>(
5045
}
5146

5247
return {
53-
target: outputPass.target,
48+
get target() {
49+
return outputPass.target;
50+
},
5451
passes,
5552
onBeforeRender,
5653
onAfterRender,

lib/src/hooks/useCompositor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export function useCompositor(
1414
renderPass: RenderPass<any>,
1515
effects: Array<EffectPass<any> | CompositeEffectPass<EffectPass<any>[]>>,
1616
) {
17+
// add the ability to render to floating-point buffers
18+
gl.getExtension("EXT_color_buffer_float");
19+
1720
if (effects.length > 0 && renderPass.target === null) {
1821
renderPass.setTarget(createRenderTarget(gl));
1922
}
@@ -23,8 +26,8 @@ export function useCompositor(
2326
for (const [index, effect] of effects.entries()) {
2427
effect.initialize(gl);
2528

26-
if (index < effects.length - 1 && effect.target == undefined) {
27-
effect.setTarget(createRenderTarget(gl));
29+
if (index === effects.length - 1 && effect.target !== null) {
30+
effect.setTarget(null);
2831
}
2932

3033
if (isCompositeEffectPass(effect)) {

lib/src/hooks/useEffectPass.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ type EffectPassOptions<U extends EffectUniforms> = Omit<QuadPassOptions<U>, "tar
1515
target?: RenderTargetParams | null;
1616
};
1717

18+
const effectTargetConfig: RenderTargetParams = {
19+
minFilter: "nearest",
20+
magFilter: "nearest",
21+
};
22+
1823
export function useEffectPass<U extends EffectUniforms>(
1924
options: EffectPassOptions<U>,
2025
): EffectPass<U> {
21-
const { target, resolutionScale = 1 } = options;
26+
const { target = effectTargetConfig, resolutionScale = 1 } = options;
2227

2328
const renderPass = useQuadRenderPass(undefined, { ...options, target: null });
2429

lib/src/hooks/usePingPongFBO.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function usePingPongFBO<U extends Uniforms>(
2121
gl: WebGL2RenderingContext,
2222
{ uniforms = {} as U, dataTexture, fragment }: PingPongFBOOptions<U>,
2323
) {
24-
// enable the extension for float textures
24+
// add the ability to render to 32-bit floating-point buffers
2525
gl.getExtension("EXT_color_buffer_float");
2626

2727
const { initialData, name: dataTextureName = "tData" } = dataTexture;
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)