|
1 | 1 | import { useEffectPass, useWebGLCanvas, loadTexture } from "usegl";
|
| 2 | +import { Pane } from "tweakpane"; |
2 | 3 | import "./styles.css";
|
3 | 4 |
|
4 |
| -(async () => { |
5 |
| - const sepiaEffect = useEffectPass({ |
6 |
| - fragment: /* glsl */ ` |
7 |
| - uniform sampler2D uTexture; // output of the render pass |
8 |
| - in vec2 vUv; |
9 |
| - out vec4 fragColor; |
10 |
| -
|
11 |
| - #define SEPIA_COLOR vec3(1.2, 1.0, 0.7) |
12 |
| - #define STRENGTH .75 |
13 |
| -
|
14 |
| - vec3 sepia(vec3 color) { |
15 |
| - float grayScale = dot(color, vec3(0.299, 0.587, 0.114)); |
16 |
| - return grayScale * SEPIA_COLOR; |
17 |
| - } |
18 |
| -
|
19 |
| - void main() { |
20 |
| - vec3 color = texture(uTexture, vUv).rgb; |
21 |
| - color = mix(color, sepia(color), STRENGTH); |
22 |
| - fragColor = vec4(color, 1.); |
23 |
| - } |
24 |
| - `, |
25 |
| - }); |
26 |
| - |
27 |
| - useWebGLCanvas({ |
28 |
| - canvas: "#glCanvas", |
29 |
| - fragment: /* glsl */ ` |
30 |
| - in vec2 vUv; |
31 |
| - uniform sampler2D uPicture; |
32 |
| - out vec4 fragColor; |
33 |
| -
|
34 |
| - void main() { |
35 |
| - fragColor = texture(uPicture, vUv); |
36 |
| - } |
37 |
| - `, |
38 |
| - uniforms: { |
39 |
| - uPicture: await loadTexture("https://picsum.photos/id/323/600/400"), |
40 |
| - }, |
41 |
| - postEffects: [sepiaEffect], |
42 |
| - }); |
43 |
| -})(); |
| 5 | +const image = loadTexture("https://picsum.photos/id/323/600/400"); |
| 6 | + |
| 7 | +const sepiaEffect = useEffectPass({ |
| 8 | + fragment: /* glsl */ ` |
| 9 | + uniform sampler2D uTexture; // output of the render pass |
| 10 | + uniform float uStrength; |
| 11 | + in vec2 vUv; |
| 12 | + out vec4 fragColor; |
| 13 | +
|
| 14 | + #define SEPIA_COLOR vec3(1.2, 1.0, 0.7) |
| 15 | +
|
| 16 | + vec3 sepia(vec3 color) { |
| 17 | + float grayScale = dot(color, vec3(0.299, 0.587, 0.114)); |
| 18 | + return grayScale * SEPIA_COLOR; |
| 19 | + } |
| 20 | +
|
| 21 | + void main() { |
| 22 | + vec3 color = texture(uTexture, vUv).rgb; |
| 23 | + color = mix(color, sepia(color), uStrength); |
| 24 | + fragColor = vec4(color, 1.); |
| 25 | + } |
| 26 | + `, |
| 27 | + uniforms: { |
| 28 | + uStrength: 0.75, |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +const { onAfterRender } = useWebGLCanvas({ |
| 33 | + canvas: "#glCanvas", |
| 34 | + fragment: /* glsl */ ` |
| 35 | + in vec2 vUv; |
| 36 | + uniform sampler2D uPicture; |
| 37 | + out vec4 fragColor; |
| 38 | +
|
| 39 | + void main() { |
| 40 | + fragColor = texture(uPicture, vUv); |
| 41 | + } |
| 42 | + `, |
| 43 | + uniforms: { |
| 44 | + uPicture: image, |
| 45 | + }, |
| 46 | + postEffects: [sepiaEffect], |
| 47 | +}); |
| 48 | + |
| 49 | +const renderCount = document.querySelector("#renderCount"); |
| 50 | +onAfterRender(() => { |
| 51 | + renderCount.textContent = `${Number(renderCount.textContent) + 1}`; |
| 52 | +}); |
| 53 | + |
| 54 | +// You can update the uniforms of an effect pass |
| 55 | +const pane = new Pane({ title: "Uniforms" }); |
| 56 | +pane.addBinding(sepiaEffect.uniforms, "uStrength", { min: 0, max: 1 }); |
0 commit comments