Skip to content

Commit 1a37be8

Browse files
committed
feat: add linearToSRGB effect
1 parent 322d9fe commit 1a37be8

File tree

9 files changed

+29
-30
lines changed

9 files changed

+29
-30
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Layout from "../../layouts/Layout.astro";
33
---
44

55
<script>
6-
import { useWebGLCanvas, loadTexture, bloom } from "usegl";
6+
import { useWebGLCanvas, loadTexture, bloom, linearToSRGB } from "usegl";
77
import { fragment } from "../../shaders/bloom";
88
import { incrementRenderCount } from "../../components/renderCount";
99

@@ -21,7 +21,7 @@ import Layout from "../../layouts/Layout.astro";
2121
{ colorSpace: "srgb" }
2222
),
2323
},
24-
postEffects: [bloom({ levels, radius, mix })],
24+
postEffects: [bloom({ levels, radius, mix }), linearToSRGB()],
2525
});
2626

2727
onAfterRender(incrementRenderCount);

lib/src/effects/glsl/combine.frag renamed to lib/src/effects/bloom/glsl/combine.frag

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@ uniform float uMix;
55
in vec2 vUv;
66
out vec4 outColor;
77

8-
vec3 linearToSrgb(vec3 color) {
9-
vec3 a = 12.92 * color;
10-
vec3 b = 1.055 * pow(color, vec3(1.0 / 2.4)) - 0.055;
11-
vec3 cond = step(vec3(0.0031308), color);
12-
return mix(a, b, cond);
13-
}
14-
158
void main() {
169
vec4 baseColor = texture(uImage, vUv);
1710
vec4 bloomColor = texture(uBloomTexture, vUv);
1811

1912
outColor = max(baseColor, mix(baseColor, bloomColor, uMix));
20-
21-
outColor.rgb = linearToSrgb(outColor.rgb);
2213
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

lib/src/effects/bloom.ts renamed to lib/src/effects/bloom/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useCompositeEffectPass } from "../hooks/useCompositeEffectPass";
2-
import { useEffectPass } from "../hooks/useEffectPass";
3-
import type { CompositeEffectPass, EffectPass } from "../types";
1+
import { useCompositeEffectPass } from "../../hooks/useCompositeEffectPass";
2+
import { useEffectPass } from "../../hooks/useEffectPass";
3+
import type { CompositeEffectPass, EffectPass } from "../../types";
44

55
import downsampleFragment from "./glsl/downsample.frag?raw";
66
import sampleVertex from "./glsl/sample.vert?raw";
@@ -13,11 +13,6 @@ export type BloomParams = {
1313
mix?: number;
1414
};
1515

16-
const targetConfig = {
17-
internalFormat: WebGL2RenderingContext.RGBA16F,
18-
type: WebGL2RenderingContext.FLOAT,
19-
};
20-
2116
export function bloom(params: BloomParams = {}) {
2217
const { levels = 8, radius = 0.65, mix = 0.5 } = params;
2318

@@ -29,7 +24,6 @@ export function bloom(params: BloomParams = {}) {
2924
fragment: downsampleFragment,
3025
vertex: sampleVertex,
3126
resolutionScale: 1 / 2 ** level,
32-
target: targetConfig,
3327
uniforms: {
3428
uTexelSizeMultiplier: 0.5,
3529
},
@@ -45,7 +39,6 @@ export function bloom(params: BloomParams = {}) {
4539
fragment: upsampleFragment,
4640
vertex: sampleVertex,
4741
resolutionScale: 1 / 2 ** level,
48-
target: targetConfig,
4942
uniforms: {
5043
uTexelSizeMultiplier: 1,
5144
uCurrentTexture: () => downsamplePasses[level].target!.texture,
@@ -69,13 +62,5 @@ export function bloom(params: BloomParams = {}) {
6962
},
7063
});
7164

72-
const compositePass = useCompositeEffectPass([...downsamplePasses, ...upsamplePasses, combine]);
73-
74-
return {
75-
...compositePass,
76-
initialize: (gl) => {
77-
gl.getExtension("EXT_color_buffer_float");
78-
compositePass.initialize(gl);
79-
},
80-
} as CompositeEffectPass;
65+
return useCompositeEffectPass([...downsamplePasses, ...upsamplePasses, combine]);
8166
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useEffectPass } from "../../hooks/useEffectPass";
2+
import linearToSRGBFrag from "./linearToSRGB.frag";
3+
4+
export function linearToSRGB() {
5+
return useEffectPass({
6+
fragment: linearToSRGBFrag,
7+
});
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
uniform sampler2D uTexture;
2+
varying vec2 vUv;
3+
4+
void main() {
5+
vec4 color = texture(uTexture, vUv);
6+
gl_FragColor = vec4(
7+
mix(
8+
color.rgb * 12.92,
9+
pow(color.rgb, vec3(1.0 / 2.4)) * 1.055 - 0.055,
10+
step(vec3(0.0031308), color.rgb)
11+
),
12+
color.a
13+
);
14+
}

lib/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export { usePingPongFBO } from "./hooks/usePingPongFBO";
2020
export { useTransformFeedback } from "./hooks/useTransformFeedback";
2121

2222
export { bloom } from "./effects/bloom";
23+
export { linearToSRGB } from "./effects/linearToSRGB";

0 commit comments

Comments
 (0)