Skip to content

Commit a88f2be

Browse files
committed
Extract quad-shader lib
1 parent 081f368 commit a88f2be

13 files changed

+127
-46
lines changed

Diff for: .github/workflows/ci.yml

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
name: Deploy static content to Pages
44

55
on:
6-
# Runs on pushes targeting the default branch
6+
# Runs on pushes
77
push:
8-
branches: main
98

109
# Allows you to run this workflow manually from the Actions tab
1110
workflow_dispatch:
@@ -35,11 +34,21 @@ jobs:
3534
- name: Build
3635
# Here we set an empty base, which makes vite produce links that are relative
3736
# to the assets, as opposed to expecting all assets to be served from `/`.
38-
run: npm ci && npm run tsc && npm run build -- --base ''
39-
- name: Upload artifact
37+
run: npm ci && npm run build -- --base ''
38+
- name: Upload pages artifact
4039
uses: actions/upload-pages-artifact@v3
4140
with:
42-
path: './dist' # vite's build output
41+
path: './dist/pages' # vite's outDir as specified in package.json
42+
43+
# build the .tgz in ./out and upload as artifact
44+
- name: Build library
45+
run: mkdir -p ./out && npm pack --pack-destination ./out
46+
- name: Upload lib artifact
47+
uses: actions/upload-pages-artifact@v3
48+
with:
49+
path: './out'
50+
4351
- name: Deploy to GitHub Pages
52+
if: github.ref_name == github.event.repository.default_branch
4453
id: deployment
4554
uses: actions/deploy-pages@v4

Diff for: HACKING.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Hacking
2+
3+
This explains how to develop locally, build the app & package the lib.
4+
5+
## Development
6+
7+
Best way to develop is with vite hot-reloading:
8+
9+
```sh
10+
npm run dev
11+
```
12+
13+
## Build microsite
14+
15+
Run the production build:
16+
17+
```sh
18+
npm run build
19+
```
20+
21+
## Package the library
22+
23+
```
24+
npm prepare # or npm pack to see the .tgz
25+
```
26+
27+
or alternatively, publish it:
28+
29+
```
30+
npm publish
31+
```

Diff for: README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<img width=300 height=300 src="https://github.com/user-attachments/assets/52f41567-effd-49a3-90b1-544e19d3e3bc" />
44
</p>
55

6-
# WebGL Shader Template
6+
# Quad Shader
77

88
This serves as a boilerplate for creating WebGL-based applications using
99
[Vite](https://vitejs.dev/) and [TypeScript](https://www.typescriptlang.org/).
1010
You can view the template built and deployed at
11-
[https://nmattia.github.io/webgl-starter-template/](https://nmattia.github.io/webgl-shader-template/).
11+
[https://nmattia.github.io/quad-shader/](https://nmattia.github.io/quad-shader/).
1212

1313
Whether you're just getting started with WebGL or looking for a quick way to
1414
bootstrap your shader projects, this template should serve as a solid
@@ -58,21 +58,19 @@ enabled for your repository (Settings -> Pages -> Source -> GitHub Actions)
5858
### Project Structure
5959

6060
```
61-
webgl-shader-template/
61+
quad-shader/
62+
├── frag.glsl # Fragment shader (where animation is defined)
6263
├── index.html # Vite entry point & main page
6364
├── index.ts # Demo app/microsite entrypoint
64-
├── frag.glsl # Fragment shader (where animation is defined)
65-
├── public # Static assets
6665
└── src
67-
├── lib.ts # TypeScript module setting up WebGL
68-
└── vert.glsl # Vertex shader (setting up the vertices)
66+
└── index.ts # TypeScript module setting up WebGL
6967
```
7068

7169
### Customizing the Shader
7270

73-
This template includes a basic vertex shader (`src/vert.glsl`) setting up
74-
4 vertices that the fragment shader draws on. The fragment shader (`frag.glsl`)
75-
includes an animation that you can remove or tweak to see how your changes affect
71+
This template includes a basic vertex shader setting up a quad (4 vertices) that
72+
the fragment shader draws on. The fragment shader (`frag.glsl`) includes an
73+
animation that you can remove or tweak to see how your changes affect
7674
what is shown on the screen.
7775

7876
### Prettier

Diff for: index.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/png" href="favicon.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>WebGL Shader Template</title>
7+
<title>quad-shader: Render fragment shader in a quad</title>
88
<link rel="stylesheet" href="./style.css" />
99
<link rel="preconnect" href="https://fonts.googleapis.com" />
1010
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
@@ -14,7 +14,7 @@
1414
/>
1515
</head>
1616
<body>
17-
<h1>WebGL Shader Template</h1>
17+
<h1>Easy 2D shader setup with quad-shader</h1>
1818
<p>
1919
This is a template for getting started with
2020
<a
@@ -24,10 +24,10 @@ <h1>WebGL Shader Template</h1>
2424
>WebGL</a
2525
>. The code is on GitHub at
2626
<a
27-
href="https://github.com/nmattia/webgl-shader-template"
27+
href="https://github.com/nmattia/quad-shader"
2828
target="_blank"
2929
rel="noopener noreferrer"
30-
>nmattia/webgl-shader-template</a
30+
>nmattia/quad-shader</a
3131
>.
3232
</p>
3333
<div id="canvas-container">
@@ -51,7 +51,7 @@ <h1>WebGL Shader Template</h1>
5151
This template has <strong>no dependencies</strong> and uses the WebGL API
5252
directly. See more in the GitHub
5353
<a
54-
href="https://github.com/nmattia/webgl-shader-template"
54+
href="https://github.com/nmattia/quad-shader"
5555
target="_blank"
5656
rel="noopener noreferrer"
5757
>repository</a

Diff for: index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This is the app's entrypoint. It picks the canvas element and attaches sets up
22
// WebGL via the lib.
3-
import { attach } from "./src/lib";
3+
import { attach } from "./src";
44

55
// Read the .glsl file contents
66
import fragShaderSrc from "./frag.glsl?raw";

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
{
2-
"name": "webgl-shader-template",
3-
"private": true,
2+
"name": "quad-shader",
43
"version": "0.0.0",
54
"type": "module",
5+
"files": [
6+
"README.md",
7+
"LICENSE",
8+
"dist/lib/quad-shader.js",
9+
"dist/lib/quad-shader.cjs",
10+
"dist/types/index.d.ts"
11+
],
12+
"exports": {
13+
".": {
14+
"types": "./dist/types/index.d.ts",
15+
"default": "./dist/lib/quad-shader.js"
16+
}
17+
},
18+
"//scripts": {
19+
"dev, build & preview": "build the webapp (e.g. for GitHub Pages deployment)",
20+
"types & prepare": "build the lib, to be used by 'npm publish' or 'npm pack'"
21+
},
622
"scripts": {
723
"dev": "vite",
8-
"build": "tsc && vite build",
9-
"preview": "vite preview",
10-
"tsc": "tsc",
11-
"format": "prettier ./src ./index.ts ./index.html ./style.css ./package.json -w"
24+
"build": "tsc --project tsconfig.json && vite build --config /dev/null --outDir ./dist/pages",
25+
"preview": "npm run build && vite preview --outDir ./dist/pages",
26+
"prepare": "npm run types && vite build --outDir ./dist/lib",
27+
"types": "tsc --project ./tsconfig.lib.json",
28+
"format": "prettier ./src ./index.ts ./index.html ./style.css ./package.json vite.config.ts -w"
1229
},
1330
"devDependencies": {
1431
"prettier": "^3.3.3",

Diff for: src/lib.ts renamed to src/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66
// uAspectRatio (ratio width/height of the canvas element). For more uniforms, use
77
// the `WebGLRenderingContext` returned by `attach` or specify `beforeRender`.
88

9-
// Read the .glsl file contents
10-
import vertShaderSrc from "./vert.glsl?raw";
9+
const vertShaderSrc = `
10+
attribute vec2 aVertexPosition;
1111
12-
type Opts = {
12+
uniform float uAspectRatio;
13+
varying vec2 vPosition;
14+
15+
void main() {
16+
// gl_Position is the ouput, which we simply return
17+
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
18+
19+
// We pre-scale the data passed to the fragment shader so that the
20+
// fragment shader doesn't have to care about the aspect ratio
21+
vPosition = gl_Position.xy * vec2(uAspectRatio, 1.0);
22+
}
23+
`;
24+
25+
export type Opts = {
1326
beforeRender?: (gl: WebGLRenderingContext, state: State) => void;
1427
};
1528

@@ -219,6 +232,7 @@ function resizeIfDimChanged(gl: WebGLRenderingContext, state: State): boolean {
219232
const pxWidth = clientWidth * window.devicePixelRatio;
220233
const pxHeight = clientHeight * window.devicePixelRatio;
221234

235+
// NOTE: the CSS MUST bound the canvas size otherwise this will grow forever
222236
state.canvas.width = pxWidth;
223237
state.canvas.height = pxHeight;
224238

Diff for: src/vert.glsl

-13
This file was deleted.

Diff for: tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"noUnusedParameters": true,
2121
"noFallthroughCasesInSwitch": true
2222
},
23-
"include": ["src", "index.ts"]
23+
"include": ["src", "index.ts", "types.d.ts"]
2424
}

Diff for: tsconfig.lib.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"declaration": true,
6+
"emitDeclarationOnly": true,
7+
"declarationDir": "dist/types"
8+
},
9+
"include": ["src/index.ts" ]
10+
}

Diff for: src/types.d.ts renamed to types.d.ts

File renamed without changes.

Diff for: vite.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { dirname, resolve } from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
import { defineConfig } from "vite";
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
7+
export default defineConfig({
8+
build: {
9+
copyPublicDir: false /* in the lib we don't care about any assets */,
10+
lib: {
11+
entry: resolve(__dirname, "src/index.ts"),
12+
formats: ["es", "cjs"] /* target modern browsers & node */,
13+
},
14+
},
15+
});

0 commit comments

Comments
 (0)