Skip to content

Commit d7624bd

Browse files
committed
refactor: use vite-plugin-glsl to load shaders
1 parent abbc7ba commit d7624bd

18 files changed

+137
-125
lines changed

lib/astro.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from "astro/config";
22
import { resolve, dirname } from "node:path";
33
import { fileURLToPath } from "node:url";
4+
import glsl from "vite-plugin-glsl";
45

56
const __dirname = dirname(fileURLToPath(import.meta.url));
67

@@ -14,6 +15,7 @@ export default defineConfig({
1415
usegl: resolve(__dirname, "./src/index.ts"),
1516
},
1617
},
18+
plugins: [glsl({ minify: true })],
1719
},
1820
devToolbar: {
1921
enabled: false,

lib/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"eslint-config-unjs": "0.5.0",
4242
"prettier": "3.6.2",
4343
"tsdown": "0.14.1",
44-
"typescript": "5.8.3"
44+
"typescript": "5.8.3",
45+
"vite-plugin-glsl": "1.5.1"
4546
},
4647
"changelog": {
4748
"excludeAuthors": [

lib/playground/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"include": ["../src", "src"],
44
"compilerOptions": {
55
"strict": true,
6+
"types": ["vite-plugin-glsl/ext"],
67
"paths": {
78
"usegl": ["../src/index.ts"]
89
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
uniform sampler2D uImage;
2+
uniform sampler2D uBloomTexture;
3+
uniform float uMix;
4+
5+
in vec2 vUv;
6+
out vec4 outColor;
7+
8+
void main() {
9+
vec4 baseColor = texture(uImage, vUv);
10+
vec4 bloomColor = texture(uBloomTexture, vUv);
11+
12+
outColor = max(baseColor, mix(baseColor, bloomColor, uMix));
13+
}

lib/src/effects/bloom/glsl/combine.frag.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
uniform sampler2D uInputTexture;
2+
3+
in vec2 vUv;
4+
in vec2 vTexelSize;
5+
out vec4 fragColor;
6+
7+
void main() {
8+
vec2 offsets[13] = vec2[](
9+
vec2(-1.0, 1.0), vec2( 1.0, 1.0),
10+
vec2(-1.0, -1.0), vec2( 1.0, -1.0),
11+
vec2(-2.0, 2.0), vec2( 0.0, 2.0), vec2( 2.0, 2.0),
12+
vec2(-2.0, 0.0), vec2( 0.0, 0.0), vec2( 2.0, 0.0),
13+
vec2(-2.0, -2.0), vec2( 0.0, -2.0), vec2( 2.0, -2.0)
14+
);
15+
16+
float weights[13] = float[](
17+
// 4 corners
18+
0.125, 0.125,
19+
0.125, 0.125,
20+
// 9 center
21+
0.0555555, 0.0555555, 0.0555555,
22+
0.0555555, 0.0555555, 0.0555555,
23+
0.0555555, 0.0555555, 0.0555555
24+
);
25+
26+
vec4 color = vec4(0.0);
27+
28+
for (int i = 0; i < 13; i++) {
29+
vec2 sampleUv = vUv + offsets[i] * vTexelSize;
30+
color += weights[i] * texture(uInputTexture, sampleUv);
31+
}
32+
33+
fragColor = color;
34+
}

lib/src/effects/bloom/glsl/downsample.frag.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
uniform sampler2D uInputTexture;
2+
uniform float uTexelSizeMultiplier;
3+
4+
in vec2 aPosition;
5+
out vec2 vTexelSize;
6+
out vec2 vUv;
7+
8+
void main() {
9+
vTexelSize = uTexelSizeMultiplier / vec2(textureSize(uInputTexture, 0).xy);
10+
vUv = aPosition.xy * 0.5 + 0.5;
11+
gl_Position = vec4(aPosition.xy, 1.0, 1.0);
12+
}

lib/src/effects/bloom/glsl/sample.vert.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
uniform sampler2D uCurrentTexture; // Texture at the current (higher) resolution
2+
uniform sampler2D uPreviousTexture; // Texture at the lower (blurrier) resolution
3+
uniform float uRadius; // Interpolation factor
4+
5+
in vec2 vTexelSize;
6+
in vec2 vUv;
7+
out vec4 fragColor;
8+
9+
float clampToBorder(const in vec2 uv) {
10+
return (uv.s >= 0.0 && uv.s <= 1.0 && uv.t >= 0.0 && uv.t <= 1.0) ? 1.0 : 0.5;
11+
}
12+
13+
void main() {
14+
vec2 offsets[9] = vec2[](
15+
vec2(-1.0, 1.0), vec2( 0.0, 1.0), vec2( 1.0, 1.0),
16+
vec2(-1.0, 0.0), vec2( 0.0, 0.0), vec2( 1.0, 0.0),
17+
vec2(-1.0, -1.0), vec2( 0.0, -1.0), vec2( 1.0, -1.0)
18+
);
19+
float weights[9] = float[](
20+
0.0625, 0.125, 0.0625,
21+
0.125, 0.25, 0.125,
22+
0.0625, 0.125, 0.0625
23+
);
24+
25+
vec4 prevColor = vec4(0.0);
26+
for (int i = 0; i < 9; i++) {
27+
vec2 sampleUv = vUv + offsets[i] * vTexelSize;
28+
prevColor += weights[i] * clampToBorder(sampleUv) * texture(uPreviousTexture, sampleUv);
29+
}
30+
31+
vec4 currColor = texture(uCurrentTexture, vUv);
32+
33+
fragColor = max(currColor, mix(currColor, prevColor, uRadius));
34+
}

0 commit comments

Comments
 (0)