-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
280 lines (243 loc) · 9.74 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<html>
<head>
<meta charset="utf-8"/>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<div class="canvases"></div>
<pre class="error"></pre>
<ul class="controls"></ul>
<script type="module">
import {ShaderCanvas} from "../dist/shader-canvas.js";
const defaultShader = `
precision mediump float;
uniform vec2 u_resolution;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
gl_FragColor = vec4(uv, 1.0, 1.0);
}
`;
const canvases = document.querySelector(".canvases");
const error = document.querySelector(".error");
const controls = document.querySelector(".controls");
const shaderCanvas = new ShaderCanvas();
shaderCanvas.render();
canvases.appendChild(shaderCanvas.domElement);
function addControl(name, onclick) {
const a = document.createElement("a");
a.href = "#";
a.textContent = name;
a.onclick = onclick;
const li = document.createElement("li");
li.appendChild(a);
controls.appendChild(li)
}
// Functions that need to run when switching away from a shader:
let cleanupFunctions = [];
function setShaderAndCheckErrs(source) {
cleanupFunctions.forEach(f => f());
cleanupFunctions = []
const errs = shaderCanvas.setShader(source);
if (errs) {
error.textContent = errs.map((err) => err.text).join("\n");
} else {
error.textContent = "";
}
}
addControl("randomize size", (e) => {
const min = 200;
const max = 600;
const w = Math.floor(Math.random() * (max - min) + min)
const h = Math.floor(Math.random() * (max - min) + min)
shaderCanvas.setSize(w, h);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.render();
e.preventDefault();
});
addControl("pretty gradient shader", (e) => {
setShaderAndCheckErrs(defaultShader);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.render();
e.preventDefault();
});
addControl("useful gradient shader", (e) => {
setShaderAndCheckErrs(`
precision mediump float;
uniform vec2 u_resolution;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
gl_FragColor = vec4(uv.x, 0.0, uv.y, 1.0);
}
`);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.render();
e.preventDefault();
});
addControl("animated shader", (e) => {
setShaderAndCheckErrs(`
precision mediump float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float t = sin(u_time * 2.0) * 0.5 + 0.5;
gl_FragColor = vec4(uv, t, 1.0);
}
`);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.setUniform("u_time", 0);
let animReq;
const animate = (timestamp) => {
animReq = window.requestAnimationFrame(animate);
shaderCanvas.setUniform("u_time", timestamp / 1000);
shaderCanvas.render();
};
animReq = window.requestAnimationFrame(animate);
cleanupFunctions.push(() => {
window.cancelAnimationFrame(animReq);
});
e.preventDefault();
});
addControl("interactive shader", (e) => {
setShaderAndCheckErrs(`
precision mediump float;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
gl_FragColor = vec4(uv.x, 0.0, uv.y, 1.0);
if (distance(uv, u_mouse) < 0.1) {
gl_FragColor = vec4(uv.y, 0.0, uv.x, 1.0);
}
}
`);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.setUniform("u_mouse", [0.5, 0.5]);
const mousemove = (e) => {
const newValue = [
e.offsetX / shaderCanvas.width,
1 - (e.offsetY / shaderCanvas.height),
]
shaderCanvas.setUniform("u_mouse", newValue);
shaderCanvas.render();
}
shaderCanvas.domElement.addEventListener("mousemove", mousemove);
cleanupFunctions.push(() => {
shaderCanvas.domElement.removeEventListener("mousemove", mousemove);
});
shaderCanvas.render();
e.preventDefault();
});
addControl("bad shader", (e) => {
// ERROR! no uv declaration
setShaderAndCheckErrs(`
precision mediump float;
void main() {
gl_FragColor = vec4(uv, 1.0, 1.0);
}
`);
e.preventDefault();
});
addControl("texture", (e) => {
setShaderAndCheckErrs(`
precision mediump float;
uniform vec2 u_resolution;
uniform sampler2D u_mainTex;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float aspect = u_resolution.x / u_resolution.y;
// Preserve aspect ratio of the image:
uv.x *= aspect;
// Center:
uv.x += (1.0 - aspect) / 2.0;
// Tile:
uv = fract(uv);
vec3 tex = texture2D(u_mainTex, uv).rgb;
gl_FragColor = vec4(tex, 1.0);
}
`);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
const img = new Image();
img.src = "grace_hopper.jpg";
img.onload = () => {
shaderCanvas.setTexture("u_mainTex", img);
shaderCanvas.render();
};
e.preventDefault();
});
addControl("two textures", (e) => {
setShaderAndCheckErrs(`
precision mediump float;
uniform vec2 u_resolution;
uniform sampler2D u_mainTex;
uniform sampler2D u_normalMap;
const float lightIntensity = 0.35;
const float ambientIntensity = 0.45;
const vec3 lightDirection = vec3(-1.0, 1.0, 1.0); // where it's coming from
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float aspect = u_resolution.x / u_resolution.y;
// Preserve aspect ratio of the image:
uv.x *= aspect;
// Center:
uv.x += (1.0 - aspect) / 2.0;
// Tile:
uv = fract(uv);
vec3 color = texture2D(u_mainTex, uv).rgb;
vec3 normal = texture2D(u_normalMap, uv).xyz;
normal = 2.0 * normal - 1.0; // map to [-1, 1] range
float light = dot(normal, lightDirection * lightIntensity);
light += ambientIntensity;
color *= light;
color = clamp(color, 0.0, 1.0);
gl_FragColor = vec4(color, 1.0);
}
`);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
const mainTex = new Image();
mainTex.src = "grace_hopper.jpg";
mainTex.onload = () => {
shaderCanvas.setTexture("u_mainTex", mainTex);
shaderCanvas.render();
};
const normalMap = new Image();
normalMap.src = "brickwall_normal-map.jpg";
normalMap.onload = () => {
shaderCanvas.setTexture("u_normalMap", normalMap);
shaderCanvas.render();
};
e.preventDefault();
});
addControl("load shader", (e) => {
fetch(new Request("./example.glsl")).then((resp) => {
if (resp.status !== 200) {
throw new Error("bad status: " + resp.status);
}
return resp.text();
}).then((source) => {
shaderCanvas.setShader(source);
shaderCanvas.setUniform("u_resolution", [shaderCanvas.domElement.width, shaderCanvas.domElement.height]);
let animReq;
const animate = (timestamp) => {
animReq = window.requestAnimationFrame(animate);
shaderCanvas.setUniform("u_time", timestamp / 1000);
shaderCanvas.render();
};
animReq = window.requestAnimationFrame(animate);
cleanupFunctions.push(() => {
window.cancelAnimationFrame(animReq);
});
});
e.preventDefault();
});
setShaderAndCheckErrs(defaultShader);
shaderCanvas.setUniform("u_resolution", shaderCanvas.getResolution());
shaderCanvas.render();
</script>
</body>
</html>