Skip to content

Commit 4c76307

Browse files
committed
docs: update the examples with the new synchronous loaders api
1 parent c77ccf8 commit 4c76307

File tree

10 files changed

+148
-123
lines changed

10 files changed

+148
-123
lines changed

docs/.vitepress/theme/styles.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
}
109109

110110
html.dark & {
111-
.cm-scroller {
111+
.cm-scroller,
112+
.sp-tabs-scrollable-container {
112113
color-scheme: dark;
113114
}
114115
}

docs/examples/post-processing/single-pass/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: Single pass (sepia)
33
---
44

5-
::: example-editor
5+
::: example-editor {deps=tweakpane@^4.0.5}
66

77
<<< ./index.ts
88
<<< ./styles.css
9-
<<< @/snippets/default/index.html
9+
<<< @/snippets/render-count/index.html
1010

1111
:::
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
import { useEffectPass, useWebGLCanvas, loadTexture } from "usegl";
2+
import { Pane } from "tweakpane";
23
import "./styles.css";
34

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 });

docs/examples/textures/image/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ title: "Image"
55
::: example-editor
66

77
<<< ./index.ts
8-
<<< ./fragment.glsl
98
<<< ./vertex.glsl
9+
<<< ./fragment.glsl
1010
<<< @/snippets/canvas-full/styles.css
1111
<<< @/snippets/default/index.html
1212

docs/examples/textures/image/index.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@ import "./styles.css";
33
import fragment from "./fragment.glsl?raw";
44
import vertex from "./vertex.glsl?raw";
55

6-
(async () => {
7-
const texture = await loadTexture("https://picsum.photos/id/669/600/400");
6+
const texture = loadTexture("https://picsum.photos/id/669/600/400", {
7+
// the placeholder is optional
8+
placeholder: {
9+
data: new Uint8Array(
10+
[
11+
[255, 255, 255, 255],
12+
[255, 255, 255, 255],
13+
[255, 255, 255, 255],
14+
[255, 255, 255, 255],
15+
[99, 46, 22, 255],
16+
[255, 255, 255, 255],
17+
].flat(),
18+
),
19+
width: 3,
20+
height: 2,
21+
//magFilter: "nearest",
22+
},
23+
});
824

9-
useWebGLCanvas({
10-
canvas: "#glCanvas",
11-
vertex,
12-
fragment,
13-
uniforms: {
14-
uTexture: texture,
15-
},
16-
});
17-
})();
25+
useWebGLCanvas({
26+
canvas: "#glCanvas",
27+
vertex,
28+
fragment,
29+
uniforms: {
30+
uTexture: texture,
31+
},
32+
});

docs/examples/textures/image/vertex.glsl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* This is an example of implementation of the `object-fit: contain/cover` CSS property.
3+
* It would be much simpler to give the canvas the size of the image.
4+
*/
5+
16
attribute vec2 position;
27
attribute vec2 uv;
38
uniform sampler2D uTexture;

docs/examples/textures/video/index.ts

+24-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1-
import { loadVideoTexture, useLoop, useWebGLCanvas } from "usegl";
1+
import { loadVideoTexture, useWebGLCanvas } from "usegl";
22
import "./styles.css";
33

4-
const canvas = document.querySelector("canvas");
5-
6-
(async () => {
7-
const texture = await loadVideoTexture(
8-
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
9-
);
10-
11-
const videoElement = texture.src as HTMLVideoElement;
12-
canvas.style.aspectRatio = `${videoElement.videoWidth} / ${videoElement.videoHeight}`;
13-
14-
const { render } = useWebGLCanvas({
15-
canvas,
16-
fragment: /* glsl */ `
17-
varying vec2 vUv;
18-
uniform sampler2D uTexture;
19-
20-
void main() {
21-
vec3 color = texture(uTexture, vUv).rgb;
22-
23-
// try playing with the colors here
24-
//color = color.rbg;
25-
26-
gl_FragColor = vec4(color, 1.);
27-
}
28-
`,
29-
uniforms: {
30-
uTexture: texture,
31-
},
32-
});
33-
34-
useLoop(render);
35-
})();
4+
const texture = loadVideoTexture(
5+
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
6+
);
7+
8+
useWebGLCanvas({
9+
canvas: "#glCanvas",
10+
fragment: /* glsl */ `
11+
varying vec2 vUv;
12+
uniform sampler2D uTexture;
13+
14+
void main() {
15+
vec3 color = texture(uTexture, vUv).rgb;
16+
17+
// try playing with the colors here
18+
//color = color.rbg;
19+
20+
gl_FragColor = vec4(color, 1.);
21+
}
22+
`,
23+
uniforms: {
24+
uTexture: texture,
25+
},
26+
});

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ import Layout from "../../layouts/Layout.astro";
1010
const mipmaps = useEffectPass({
1111
fragment: mipmapsShader,
1212
uniforms: {
13-
u_threshold: 0.2,
13+
uThreshold: 0.2,
1414
},
1515
});
1616

1717
const horizontalBlur = useEffectPass({
1818
fragment: blurShader,
1919
uniforms: {
20-
u_direction: [1, 0],
20+
uDirection: [1, 0],
2121
},
2222
});
2323

2424
const verticalBlur = useEffectPass({
2525
fragment: blurShader,
2626
uniforms: {
27-
u_direction: [0, 1],
27+
uDirection: [0, 1],
2828
},
2929
});
3030

3131
const combine = useEffectPass({
3232
fragment: combineShader,
3333
uniforms: {
34-
u_image: ({ inputPass }) => inputPass.target!.texture,
35-
u_bloomTexture: () => verticalBlur.target!.texture,
36-
u_mix: 0,
34+
uImage: ({ inputPass }) => inputPass.target!.texture,
35+
uBloomTexture: () => verticalBlur.target!.texture,
36+
uMix: 0,
3737
},
3838
});
3939

@@ -44,7 +44,7 @@ import Layout from "../../layouts/Layout.astro";
4444
combine,
4545
});
4646

47-
bloomEffect.passes.combine.uniforms.u_mix = 1;
47+
bloomEffect.passes.combine.uniforms.uMix = 1;
4848

4949
const vignetteEffect = useEffectPass({
5050
fragment: /* glsl */ `

lib/playground/src/pages/textures/mipmap.astro

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Layout from "../../layouts/Layout.astro";
1313
const { onAfterRender } = useWebGLCanvas({
1414
canvas: "#glCanvas",
1515
fragment: /* glsl */ `
16-
uniform vec2 u_resolution;
16+
uniform vec2 uResolution;
1717
uniform sampler2D uNight;
1818
uniform sampler2D uColor;
1919
uniform float uTime;
@@ -41,7 +41,7 @@ import Layout from "../../layouts/Layout.astro";
4141

4242
vec3 getRayDirection(vec2 uv) {
4343
vec2 p = (2.0 * uv - 1.0);
44-
p.x *= u_resolution.x/u_resolution.y;
44+
p.x *= uResolution.x/uResolution.y;
4545
return normalize(vec3(p, 1.0));
4646
}
4747

@@ -82,7 +82,7 @@ import Layout from "../../layouts/Layout.astro";
8282
}
8383

8484
void main() {
85-
vec2 uv = gl_FragCoord.xy/u_resolution.xy;
85+
vec2 uv = gl_FragCoord.xy/uResolution.xy;
8686
vec3 ro = vec3(0.0, 0.0, -2.0);
8787
vec3 rd = getRayDirection(uv);
8888

0 commit comments

Comments
 (0)