-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-gl.html
376 lines (300 loc) · 8.26 KB
/
hello-gl.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello WebGL</title>
<meta name="description" content="An example zero-dependency GLSL shader">
<style>
:root {
--code-bg: ghostwhite;
}
@media (prefers-color-scheme: dark) {
:root {
--code-bg: #333;
}
}
body {
margin-block: 4rem;
margin-inline: 3rem;
line-height: 1.5;
}
@media (width < 40em) {
body {
margin-block: 1rem;
margin-inline: 0rem;
}
}
@media (prefers-color-scheme: dark) {
body {
background-color: #222;
color: ghostwhite;
}
}
pre {
span {
font-weight: bold;
}
span.muted {
opacity: 0.7;
}
span.blue {
color: rgb(0 50% 100%);
}
span.vs {
color: black;
}
span.fs {
color: blue;
}
}
@media (prefers-color-scheme: dark) {
pre span.fs {
color: rgb(57, 228, 51);
}
pre span.vs {
color: rgb(255, 154, 38)
}
}
.dot {
background-color: rgb(0 50% 100%);
color: white;
padding: 0.5ch;
}
canvas {
display: block;
margin-inline: 1rem;
width: calc(min(36rem, 100%) - 2rem);
aspect-ratio: 2/1;
}
header, p, blockquote, h3, iframe {
max-width: 34rem;
margin-inline: 1rem;
}
pre {
max-width: 33rem;
overflow-x: auto;
}
p, pre, canvas {
margin-block: 2.4rem;
}
pre {
padding: 1.5rem;
background-color: var(--code-bg);
}
@media (width < 40em) {
pre {
padding-inline: 1rem;
}
}
@media (width > 40em) {
pre {
border-radius: 8px;
}
}
:not(pre) > code {
padding: 1ch;
background-color: var(--code-bg);
}
blockquote {
opacity: 0.5;
border-inline-start: 2px solid #888;
margin-inline: 2px;
}
#bonus > pre {
max-width: 100%;
font-size: smaller;
padding: 0 1rem 2ch 1rem;
background: transparent;
}
iframe {
margin-block-end: 3rem;
}
@media (width < 350px) {
iframe {
margin-inline: 0;
}
}
</style>
<script type="module">
const canvas = document.getElementById("c");
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const gl = canvas.getContext("webgl");
gl.clearColor(0, 0.5, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, `
void main() {
gl_Position = vec4(0., 0., 0., 1.);
gl_PointSize = 100.;
}`);
gl.compileShader(vs);
const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, `
void main() {
gl_FragColor = vec4(1., 1., 1., 1.);
}`);
gl.compileShader(fs);
const p = gl.createProgram();
gl.attachShader(p, vs);
gl.attachShader(p, fs);
gl.linkProgram(p);
gl.detachShader(p, vs);
gl.detachShader(p, fs);
gl.deleteShader(vs);
gl.deleteShader(fs);
const log = gl.getProgramInfoLog(p);
if (log) console.error(log);
gl.useProgram(p);
gl.drawArrays(gl.POINTS, 0, 1);
gl.useProgram(null);
gl.deleteProgram(p);
// Show the dynamic DPR
document.getElementById("dpr").innerText = `${devicePixelRatio}`;
</script>
</head>
<body>
<header>
<h2>Hello WebGL</h2>
<em>Look Ma, no libraries.</em>
</header>
<canvas id="c"></canvas>
<p>Create a canvas.</p>
<pre><code><canvas id="c"></canvas>
</code></pre>
<p>
By default, a canvas has a width of 300px and a height of 150px. That is tiny,
you might want to increase it a bit.
</p>
<p>
For example, this page asks the canvas HTML element to fill its container, and
also automatically compute the corresponding height so as to maintain a 2:1
aspect ratio (that is, be twice as wide as it is high).
</p>
<pre><code>canvas {
width: 100%;
aspect-ratio: 2/1;
}
</code></pre>
<p>
Setup things so that the following JavaScript code runs after your document has
loaded. For example, this page uses a <code><script type="module></code>
block. Simply doing that guarantees that our code will run after the DOM has
loaded.
</p>
<pre><code><script type="module">
<span class="muted">
// Rest of our code will go here...
</span>
</script>
</code></pre>
<p>
Get hold of the canvas, and ask for it for its DOM width and height. The canvas
is still living in delusion that its width and height are at their default value
(300, 150), so we also need to tell the canvas, hey buddy, wake up, this is the
space you're occupying on the page (DOM).
</p>
<p>
But that's not all. If you're viewing the page on a high density display, your
screen will be showing multiple pixels for each CSS pixel. For example, I'm
currently writing this on a device that has a <code>devicePixelRatio</code> of
2. You're currently reading this on device that has a DPR of <span
id="dpr"></span>.
</p>
<p>
There are many ways of, and caveats to, using the DPR. To keep things minimal,
let’s just multiply by it so that our example does not look blurry.
</p>
<pre><code>const canvas = document.getElementById("c");
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
</code></pre>
<p>So far, we've not done anything WebGL specific.</p>
<p>Now let's get hands our on the WebGL context, and paint it blue-ish.</p>
<pre><code>const gl = canvas.getContext("webgl");
gl.clearColor(<span class="blue">0, 0.5, 1, 1</span>);
gl.clear(gl.COLOR_BUFFER_BIT);
</code></pre>
<p>
Now onto the shaders. We need two, a vertex shader, which defines the area we're
going to draw in, and a fragment shader, which defines the color each pixel in
this area will have.
</p>
<p>For now, let's create shaders that draw a point, and color it white.</p>
<pre><code>const vs = gl.createShader(gl.<span class="vs">VERTEX_SHADER</span>);
gl.shaderSource(vs, <span class="vs">`
void main() {
gl_Position = vec4(0., 0., 0., 1.);
gl_PointSize = 100.;
}`</span>);
gl.compileShader(vs);
const fs = gl.createShader(gl.<span class="fs">FRAGMENT_SHADER</span>);
gl.shaderSource(fs, <span class="fs">`
void main() {
gl_FragColor = vec4(1., 1., 1., 1.);
}`</span>);
gl.compileShader(fs);
</code></pre>
<p>And tell the WebGL context of our canvas to use them.</p>
<pre><code>const p = gl.createProgram();
gl.attachShader(p, vs);
gl.attachShader(p, fs);
gl.linkProgram(p);
gl.detachShader(p, vs);
gl.detachShader(p, fs);
gl.deleteShader(vs);
gl.deleteShader(fs);
</code></pre>
<p>Let us give WebGL a chance to speak, if it has something to say.</p>
<pre><code>const log = gl.getProgramInfoLog(p);
if (log) console.error(log);
</code></pre>
<blockquote>
<p>
A common mistake is to omit the dots when specifying numbers in GLSL. GLSL is
not JavaScript, and those dots are mandatory – for GLSL <code>1</code> and
<code>1.0</code> mean different things.
</p>
<p><i>Tip:<code>1.</code> is a shorthand for <code>1.0</code></i></p>
</blockquote>
<p>Everything's in place, let us ask GLSL to draw.</p>
<pre><code>gl.useProgram(p);
gl.drawArrays(gl.POINTS, 0, 1);
</code></pre>
<p>
Since we are not animating, we won't need our program subsequently, so we can
also clean it up.
</p>
<pre><code>gl.useProgram(null);
gl.deleteProgram(p);
</code></pre>
<p>
That's it, that's all it took to create that WebGL canvas you saw at the top of
this page. <span class="dot">Our lovely white dot on a blue ocean of
nothingness.</span>
</p>
<section id="bonus">
<h3>Bonus</h3>
<p>
Here is a 499 character standalone HTML file (shown below in an iframe) that
shows an slowly pulsing WebGL box.
</p>
<pre><code><canvas id=c><script>g=document.getElementById(`c`).getContext(`webgl`);a=35633
S=c=>(g.shaderSource(s=g.createShader(a--),
`precision highp float;uniform float t;void main(){gl_`+c+`0.,1.);}`),g.compileShader(s),s)
g.attachShader(p=g.createProgram(),S(`PointSize=100.;gl_Position=vec4(0.,0.,`))
g.attachShader(p,S(`FragColor=vec4(gl_FragCoord.x,sin(t),`));g.linkProgram(p);g.useProgram(p)
d=n=>(g.uniform1f(g.getUniformLocation(p,`t`),n/a),g.drawArrays(0,0,1),requestAnimationFrame(d))
d()</script>
</code></pre>
<iframe srcdoc="<canvas id=c><script>g=document.getElementById(`c`).getContext(`webgl`);a=35633
S=c=>(g.shaderSource(s=g.createShader(a--),
`precision highp float;uniform float t;void main(){gl_`+c+`0.,1.);}`),g.compileShader(s),s)
g.attachShader(p=g.createProgram(),S(`PointSize=100.;gl_Position=vec4(0.,0.,`))
g.attachShader(p,S(`FragColor=vec4(gl_FragCoord.x,sin(t),`));g.linkProgram(p);g.useProgram(p)
d=n=>(g.uniform1f(g.getUniformLocation(p,`t`),n/a),g.drawArrays(0,0,1),requestAnimationFrame(d))
d()</script>" loading="lazy" width="315" height="170"></iframe>
</section>
</body>
</html>