-
Notifications
You must be signed in to change notification settings - Fork 9
/
27.fbo.lighting.initial.js
536 lines (461 loc) · 18 KB
/
27.fbo.lighting.initial.js
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import { mat4, vec3 } from 'gl-matrix';
const createWebGLProgram = (gl, vs, fs) => {
const program = gl.createProgram();
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vs);
gl.compileShader(vertexShader);
gl.attachShader(program, vertexShader);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fs);
gl.compileShader(fragmentShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log(gl.getShaderInfoLog(vertexShader));
console.log(gl.getShaderInfoLog(fragmentShader));
}
return program;
};
// returns WebGL program and a draw() function for the GEOMETRY
const createGeometryProgram = () => {
const vertexShaderSource = `#version 300 es
#pragma vscode_glsllint_stage: vert
uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProjection;
layout(location=0) in vec4 aPosition;
layout(location=1) in vec3 aNormal;
layout(location=2) in vec4 aOffset;
out vec3 vLocalPosition;
out vec3 vNormal;
void main()
{
vec4 localPosition = uModel * (aPosition + aOffset.xzyw);
gl_Position = uProjection * uView * localPosition;
vLocalPosition = localPosition.xyz;
vNormal = aNormal;
}
`;
const fragmentShaderSource = `#version 300 es
#pragma vscode_glsllint_stage: frag
precision mediump float;
uniform vec3 uDirectionalLight;
uniform vec3[4] uPointLights;
in vec3 vLocalPosition;
in vec3 vNormal;
out vec4 fragColor;
void main()
{
vec4 baseColor = vec4(.2, .4, .8, 1.0);
vec4 lightColor = vec4(1.4, .4, 1.0, 1.0);
vec3 normal = normalize(vNormal);
float diffuseBrightness = 0.4 * max(0.0, dot(uDirectionalLight, normal));
float spotBrightness = 0.0;
for (int i = 0; i < 4; i++) {
vec3 offset = uPointLights[i] - vLocalPosition;
vec3 direction = normalize(offset);
float distance = length(offset);
float cos = max(0.0, dot(direction, normal));
spotBrightness += 1.2 * cos / (distance * distance);
}
fragColor = diffuseBrightness * baseColor
+ spotBrightness * lightColor;
fragColor.a = 1.0;
}
`;
const icosphere = new Float32Array([
// position (vec3) normal (vec3)
0.000,-1.000, 0.000, 0.102,-0.944, 0.315,
0.425,-0.851, 0.309, 0.102,-0.944, 0.315,
-0.162,-0.851, 0.500, 0.102,-0.944, 0.315,
0.724,-0.447, 0.526, 0.700,-0.662, 0.268,
0.425,-0.851, 0.309, 0.700,-0.662, 0.268,
0.851,-0.526, 0.000, 0.700,-0.662, 0.268,
0.000,-1.000, 0.000,-0.268,-0.944, 0.195,
-0.162,-0.851, 0.500,-0.268,-0.944, 0.195,
-0.526,-0.851, 0.000,-0.268,-0.944, 0.195,
0.000,-1.000, 0.000,-0.268,-0.944,-0.195,
-0.526,-0.851, 0.000,-0.268,-0.944,-0.195,
-0.162,-0.851,-0.500,-0.268,-0.944,-0.195,
0.000,-1.000, 0.000, 0.102,-0.944,-0.315,
-0.162,-0.851,-0.500, 0.102,-0.944,-0.315,
0.425,-0.851,-0.309, 0.102,-0.944,-0.315,
0.724,-0.447, 0.526, 0.905,-0.330, 0.268,
0.851,-0.526, 0.000, 0.905,-0.330, 0.268,
0.951, 0.000, 0.309, 0.905,-0.330, 0.268,
-0.276,-0.447, 0.851, 0.025,-0.330, 0.944,
0.263,-0.526, 0.809, 0.025,-0.330, 0.944,
0.000, 0.000, 1.000, 0.025,-0.330, 0.944,
-0.894,-0.447, 0.000,-0.890,-0.330, 0.315,
-0.688,-0.526, 0.500,-0.890,-0.330, 0.315,
-0.951, 0.000, 0.309,-0.890,-0.330, 0.315,
-0.276,-0.447,-0.851,-0.575,-0.330,-0.749,
-0.688,-0.526,-0.500,-0.575,-0.330,-0.749,
-0.588, 0.000,-0.809,-0.575,-0.330,-0.749,
0.724,-0.447,-0.526, 0.535,-0.330,-0.778,
0.263,-0.526,-0.809, 0.535,-0.330,-0.778,
0.588, 0.000,-0.809, 0.535,-0.330,-0.778,
0.724,-0.447, 0.526, 0.803,-0.126, 0.583,
0.951, 0.000, 0.309, 0.803,-0.126, 0.583,
0.588, 0.000, 0.809, 0.803,-0.126, 0.583,
-0.276,-0.447, 0.851,-0.307,-0.126, 0.944,
0.000, 0.000, 1.000,-0.307,-0.126, 0.944,
-0.588, 0.000, 0.809,-0.307,-0.126, 0.944,
-0.894,-0.447, 0.000,-0.992,-0.126, 0.000,
-0.951, 0.000, 0.309,-0.992,-0.126, 0.000,
-0.951, 0.000,-0.309,-0.992,-0.126, 0.000,
-0.276,-0.447,-0.851,-0.307,-0.126,-0.944,
-0.588, 0.000,-0.809,-0.307,-0.126,-0.944,
0.000, 0.000,-1.000,-0.307,-0.126,-0.944,
0.724,-0.447,-0.526, 0.803,-0.126,-0.583,
0.588, 0.000,-0.809, 0.803,-0.126,-0.583,
0.951, 0.000,-0.309, 0.803,-0.126,-0.583,
0.276, 0.447, 0.851, 0.409, 0.662, 0.628,
0.688, 0.526, 0.500, 0.409, 0.662, 0.628,
0.162, 0.851, 0.500, 0.409, 0.662, 0.628,
-0.724, 0.447, 0.526,-0.471, 0.662, 0.583,
-0.263, 0.526, 0.809,-0.471, 0.662, 0.583,
-0.425, 0.851, 0.309,-0.471, 0.662, 0.583,
-0.724, 0.447,-0.526,-0.700, 0.662,-0.268,
-0.851, 0.526, 0.000,-0.700, 0.662,-0.268,
-0.425, 0.851,-0.309,-0.700, 0.662,-0.268,
0.276, 0.447,-0.851, 0.038, 0.662,-0.749,
-0.263, 0.526,-0.809, 0.038, 0.662,-0.749,
0.162, 0.851,-0.500, 0.038, 0.662,-0.749,
0.894, 0.447, 0.000, 0.724, 0.662,-0.195,
0.688, 0.526,-0.500, 0.724, 0.662,-0.195,
0.526, 0.851, 0.000, 0.724, 0.662,-0.195,
0.526, 0.851, 0.000, 0.268, 0.944,-0.195,
0.162, 0.851,-0.500, 0.268, 0.944,-0.195,
0.000, 1.000, 0.000, 0.268, 0.944,-0.195,
0.526, 0.851, 0.000, 0.491, 0.795,-0.357,
0.688, 0.526,-0.500, 0.491, 0.795,-0.357,
0.162, 0.851,-0.500, 0.491, 0.795,-0.357,
0.688, 0.526,-0.500, 0.409, 0.662,-0.628,
0.276, 0.447,-0.851, 0.409, 0.662,-0.628,
0.162, 0.851,-0.500, 0.409, 0.662,-0.628,
0.162, 0.851,-0.500,-0.102, 0.944,-0.315,
-0.425, 0.851,-0.309,-0.102, 0.944,-0.315,
0.000, 1.000, 0.000,-0.102, 0.944,-0.315,
0.162, 0.851,-0.500,-0.188, 0.795,-0.577,
-0.263, 0.526,-0.809,-0.188, 0.795,-0.577,
-0.425, 0.851,-0.309,-0.188, 0.795,-0.577,
-0.263, 0.526,-0.809,-0.471, 0.662,-0.583,
-0.724, 0.447,-0.526,-0.471, 0.662,-0.583,
-0.425, 0.851,-0.309,-0.471, 0.662,-0.583,
-0.425, 0.851,-0.309,-0.331, 0.944, 0.000,
-0.425, 0.851, 0.309,-0.331, 0.944, 0.000,
0.000, 1.000, 0.000,-0.331, 0.944, 0.000,
-0.425, 0.851,-0.309,-0.607, 0.795, 0.000,
-0.851, 0.526, 0.000,-0.607, 0.795, 0.000,
-0.425, 0.851, 0.309,-0.607, 0.795, 0.000,
-0.851, 0.526, 0.000,-0.700, 0.662, 0.268,
-0.724, 0.447, 0.526,-0.700, 0.662, 0.268,
-0.425, 0.851, 0.309,-0.700, 0.662, 0.268,
-0.425, 0.851, 0.309,-0.102, 0.944, 0.315,
0.162, 0.851, 0.500,-0.102, 0.944, 0.315,
0.000, 1.000, 0.000,-0.102, 0.944, 0.315,
-0.425, 0.851, 0.309,-0.188, 0.795, 0.577,
-0.263, 0.526, 0.809,-0.188, 0.795, 0.577,
0.162, 0.851, 0.500,-0.188, 0.795, 0.577,
-0.263, 0.526, 0.809, 0.038, 0.662, 0.749,
0.276, 0.447, 0.851, 0.038, 0.662, 0.749,
0.162, 0.851, 0.500, 0.038, 0.662, 0.749,
0.162, 0.851, 0.500, 0.268, 0.944, 0.195,
0.526, 0.851, 0.000, 0.268, 0.944, 0.195,
0.000, 1.000, 0.000, 0.268, 0.944, 0.195,
0.162, 0.851, 0.500, 0.491, 0.795, 0.357,
0.688, 0.526, 0.500, 0.491, 0.795, 0.357,
0.526, 0.851, 0.000, 0.491, 0.795, 0.357,
0.688, 0.526, 0.500, 0.724, 0.662, 0.195,
0.894, 0.447, 0.000, 0.724, 0.662, 0.195,
0.526, 0.851, 0.000, 0.724, 0.662, 0.195,
0.951, 0.000,-0.309, 0.890, 0.330,-0.315,
0.688, 0.526,-0.500, 0.890, 0.330,-0.315,
0.894, 0.447, 0.000, 0.890, 0.330,-0.315,
0.951, 0.000,-0.309, 0.795, 0.188,-0.577,
0.588, 0.000,-0.809, 0.795, 0.188,-0.577,
0.688, 0.526,-0.500, 0.795, 0.188,-0.577,
0.588, 0.000,-0.809, 0.575, 0.330,-0.749,
0.276, 0.447,-0.851, 0.575, 0.330,-0.749,
0.688, 0.526,-0.500, 0.575, 0.330,-0.749,
0.000, 0.000,-1.000,-0.025, 0.330,-0.944,
-0.263, 0.526,-0.809,-0.025, 0.330,-0.944,
0.276, 0.447,-0.851,-0.025, 0.330,-0.944,
0.000, 0.000,-1.000,-0.303, 0.188,-0.934,
-0.588, 0.000,-0.809,-0.303, 0.188,-0.934,
-0.263, 0.526,-0.809,-0.303, 0.188,-0.934,
-0.588, 0.000,-0.809,-0.535, 0.330,-0.778,
-0.724, 0.447,-0.526,-0.535, 0.330,-0.778,
-0.263, 0.526,-0.809,-0.535, 0.330,-0.778,
-0.951, 0.000,-0.309,-0.905, 0.330,-0.268,
-0.851, 0.526, 0.000,-0.905, 0.330,-0.268,
-0.724, 0.447,-0.526,-0.905, 0.330,-0.268,
-0.951, 0.000,-0.309,-0.982, 0.188, 0.000,
-0.951, 0.000, 0.309,-0.982, 0.188, 0.000,
-0.851, 0.526, 0.000,-0.982, 0.188, 0.000,
-0.951, 0.000, 0.309,-0.905, 0.330, 0.268,
-0.724, 0.447, 0.526,-0.905, 0.330, 0.268,
-0.851, 0.526, 0.000,-0.905, 0.330, 0.268,
-0.588, 0.000, 0.809,-0.535, 0.330, 0.778,
-0.263, 0.526, 0.809,-0.535, 0.330, 0.778,
-0.724, 0.447, 0.526,-0.535, 0.330, 0.778,
-0.588, 0.000, 0.809,-0.303, 0.188, 0.934,
0.000, 0.000, 1.000,-0.303, 0.188, 0.934,
-0.263, 0.526, 0.809,-0.303, 0.188, 0.934,
0.000, 0.000, 1.000,-0.025, 0.330, 0.944,
0.276, 0.447, 0.851,-0.025, 0.330, 0.944,
-0.263, 0.526, 0.809,-0.025, 0.330, 0.944,
0.588, 0.000, 0.809, 0.575, 0.330, 0.749,
0.688, 0.526, 0.500, 0.575, 0.330, 0.749,
0.276, 0.447, 0.851, 0.575, 0.330, 0.749,
0.588, 0.000, 0.809, 0.795, 0.188, 0.577,
0.951, 0.000, 0.309, 0.795, 0.188, 0.577,
0.688, 0.526, 0.500, 0.795, 0.188, 0.577,
0.951, 0.000, 0.309, 0.890, 0.330, 0.315,
0.894, 0.447, 0.000, 0.890, 0.330, 0.315,
0.688, 0.526, 0.500, 0.890, 0.330, 0.315,
0.588, 0.000,-0.809, 0.307, 0.126,-0.944,
0.000, 0.000,-1.000, 0.307, 0.126,-0.944,
0.276, 0.447,-0.851, 0.307, 0.126,-0.944,
0.588, 0.000,-0.809, 0.303,-0.188,-0.934,
0.263,-0.526,-0.809, 0.303,-0.188,-0.934,
0.000, 0.000,-1.000, 0.303,-0.188,-0.934,
0.263,-0.526,-0.809, 0.025,-0.330,-0.944,
-0.276,-0.447,-0.851, 0.025,-0.330,-0.944,
0.000, 0.000,-1.000, 0.025,-0.330,-0.944,
-0.588, 0.000,-0.809,-0.803, 0.126,-0.583,
-0.951, 0.000,-0.309,-0.803, 0.126,-0.583,
-0.724, 0.447,-0.526,-0.803, 0.126,-0.583,
-0.588, 0.000,-0.809,-0.795,-0.188,-0.577,
-0.688,-0.526,-0.500,-0.795,-0.188,-0.577,
-0.951, 0.000,-0.309,-0.795,-0.188,-0.577,
-0.688,-0.526,-0.500,-0.890,-0.330,-0.315,
-0.894,-0.447, 0.000,-0.890,-0.330,-0.315,
-0.951, 0.000,-0.309,-0.890,-0.330,-0.315,
-0.951, 0.000, 0.309,-0.803, 0.126, 0.583,
-0.588, 0.000, 0.809,-0.803, 0.126, 0.583,
-0.724, 0.447, 0.526,-0.803, 0.126, 0.583,
-0.951, 0.000, 0.309,-0.795,-0.188, 0.577,
-0.688,-0.526, 0.500,-0.795,-0.188, 0.577,
-0.588, 0.000, 0.809,-0.795,-0.188, 0.577,
-0.688,-0.526, 0.500,-0.575,-0.330, 0.749,
-0.276,-0.447, 0.851,-0.575,-0.330, 0.749,
-0.588, 0.000, 0.809,-0.575,-0.330, 0.749,
0.000, 0.000, 1.000, 0.307, 0.126, 0.944,
0.588, 0.000, 0.809, 0.307, 0.126, 0.944,
0.276, 0.447, 0.851, 0.307, 0.126, 0.944,
0.000, 0.000, 1.000, 0.303,-0.188, 0.934,
0.263,-0.526, 0.809, 0.303,-0.188, 0.934,
0.588, 0.000, 0.809, 0.303,-0.188, 0.934,
0.263,-0.526, 0.809, 0.535,-0.330, 0.778,
0.724,-0.447, 0.526, 0.535,-0.330, 0.778,
0.588, 0.000, 0.809, 0.535,-0.330, 0.778,
0.951, 0.000, 0.309, 0.992, 0.126, 0.000,
0.951, 0.000,-0.309, 0.992, 0.126, 0.000,
0.894, 0.447, 0.000, 0.992, 0.126, 0.000,
0.951, 0.000, 0.309, 0.982,-0.188, 0.000,
0.851,-0.526, 0.000, 0.982,-0.188, 0.000,
0.951, 0.000,-0.309, 0.982,-0.188, 0.000,
0.851,-0.526, 0.000, 0.905,-0.330,-0.268,
0.724,-0.447,-0.526, 0.905,-0.330,-0.268,
0.951, 0.000,-0.309, 0.905,-0.330,-0.268,
0.425,-0.851,-0.309, 0.471,-0.662,-0.583,
0.263,-0.526,-0.809, 0.471,-0.662,-0.583,
0.724,-0.447,-0.526, 0.471,-0.662,-0.583,
0.425,-0.851,-0.309, 0.188,-0.795,-0.577,
-0.162,-0.851,-0.500, 0.188,-0.795,-0.577,
0.263,-0.526,-0.809, 0.188,-0.795,-0.577,
-0.162,-0.851,-0.500,-0.038,-0.662,-0.749,
-0.276,-0.447,-0.851,-0.038,-0.662,-0.749,
0.263,-0.526,-0.809,-0.038,-0.662,-0.749,
-0.162,-0.851,-0.500,-0.409,-0.662,-0.628,
-0.688,-0.526,-0.500,-0.409,-0.662,-0.628,
-0.276,-0.447,-0.851,-0.409,-0.662,-0.628,
-0.162,-0.851,-0.500,-0.491,-0.795,-0.357,
-0.526,-0.851, 0.000,-0.491,-0.795,-0.357,
-0.688,-0.526,-0.500,-0.491,-0.795,-0.357,
-0.526,-0.851, 0.000,-0.724,-0.662,-0.195,
-0.894,-0.447, 0.000,-0.724,-0.662,-0.195,
-0.688,-0.526,-0.500,-0.724,-0.662,-0.195,
-0.526,-0.851, 0.000,-0.724,-0.662, 0.195,
-0.688,-0.526, 0.500,-0.724,-0.662, 0.195,
-0.894,-0.447, 0.000,-0.724,-0.662, 0.195,
-0.526,-0.851, 0.000,-0.491,-0.795, 0.357,
-0.162,-0.851, 0.500,-0.491,-0.795, 0.357,
-0.688,-0.526, 0.500,-0.491,-0.795, 0.357,
-0.162,-0.851, 0.500,-0.409,-0.662, 0.628,
-0.276,-0.447, 0.851,-0.409,-0.662, 0.628,
-0.688,-0.526, 0.500,-0.409,-0.662, 0.628,
0.851,-0.526, 0.000, 0.700,-0.662,-0.268,
0.425,-0.851,-0.309, 0.700,-0.662,-0.268,
0.724,-0.447,-0.526, 0.700,-0.662,-0.268,
0.851,-0.526, 0.000, 0.607,-0.795, 0.000,
0.425,-0.851, 0.309, 0.607,-0.795, 0.000,
0.425,-0.851,-0.309, 0.607,-0.795, 0.000,
0.425,-0.851, 0.309, 0.331,-0.944, 0.000,
0.000,-1.000, 0.000, 0.331,-0.944, 0.000,
0.425,-0.851,-0.309, 0.331,-0.944, 0.000,
-0.162,-0.851, 0.500,-0.038,-0.662, 0.749,
0.263,-0.526, 0.809,-0.038,-0.662, 0.749,
-0.276,-0.447, 0.851,-0.038,-0.662, 0.749,
-0.162,-0.851, 0.500, 0.188,-0.795, 0.577,
0.425,-0.851, 0.309, 0.188,-0.795, 0.577,
0.263,-0.526, 0.809, 0.188,-0.795, 0.577,
0.425,-0.851, 0.309, 0.471,-0.662, 0.583,
0.724,-0.447, 0.526, 0.471,-0.662, 0.583,
0.263,-0.526, 0.809, 0.471,-0.662, 0.583,
]);
const vertexCount = icosphere.length / 6;
const instanceCount = 400;
const offsets = new Float32Array(instanceCount * 2);
for (let i = 0; i < instanceCount; i += 1) {
const index = i * 2;
const row = (i % 20) - 10;
const col = Math.floor(i / 20 - 10);
offsets[index] = 2 * row;
offsets[index+1] = 2 * col;
}
const program = createWebGLProgram(gl, vertexShaderSource, fragmentShaderSource);
gl.enable(gl.CULL_FACE);
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, icosphere, gl.STATIC_DRAW);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 24, 0);
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 24, 12);
gl.enableVertexAttribArray(0);
gl.enableVertexAttribArray(1);
const offsetsBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, offsetsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW);
gl.vertexAttribPointer(2, 2, gl.FLOAT, false, 0, 0);
gl.vertexAttribDivisor(2, 1);
gl.enableVertexAttribArray(2);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindVertexArray(null);
const draw = () => {
gl.useProgram(program);
gl.bindVertexArray(vao);
gl.enable(gl.DEPTH_TEST);
gl.drawArraysInstanced(gl.TRIANGLES, 0, vertexCount, instanceCount);
gl.disable(gl.DEPTH_TEST);
}
return { program, draw };
};
// returns WebGL program and a draw() function for the QUAD
const createQuadProgram = () => {
const vertexShaderSource =
`#version 300 es
#pragma vscode_glsllint_stage: vert
layout(location=0) in vec4 aPosition;
layout(location=1) in vec2 aTexCoord;
out vec2 vTexCoord;
void main()
{
gl_Position = aPosition;
vTexCoord = aTexCoord;
}
`;
const fragmentShaderSource =
`#version 300 es
#pragma vscode_glsllint_stage: frag
precision mediump float;
uniform sampler2D sampler;
in vec2 vTexCoord;
out vec4 fragColor;
void main()
{
fragColor = texture(sampler, vTexCoord);
}
`;
const program = createWebGLProgram(gl, vertexShaderSource, fragmentShaderSource);
const quadData = new Float32Array([
// Pos (xy) // UV coordinate
-1, 1, 0,1,
-1,-1, 0,0,
1, 1, 1,1,
1,-1, 1,0,
]);
const vertexCount = 4;
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, quadData, gl.STATIC_DRAW);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 16, 8);
gl.enableVertexAttribArray(0);
gl.enableVertexAttribArray(1);
gl.bindVertexArray(null);
const draw = () => {
gl.useProgram(program);
gl.bindVertexArray(vao);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertexCount);
gl.bindVertexArray(null);
};
return { program, draw };
};
// MVP matrices are required by the geometry program
const initializeMVPMatrices = () => {
gl.useProgram(geometry.program);
const uModel = mat4.create();
const uModelLocation = gl.getUniformLocation(geometry.program, 'uModel');
mat4.scale(uModel, uModel, new Float32Array([.2, .2, .2]));
gl.uniformMatrix4fv(uModelLocation, false, uModel);
const uView = mat4.create();
const uViewLocation = gl.getUniformLocation(geometry.program, 'uView');
mat4.lookAt(uView, new Float32Array([1.5,.3,1.8]), new Float32Array([ 0, -1, 0 ]), new Float32Array([ 0, 1, 0 ]));
gl.uniformMatrix4fv(uViewLocation, false, uView);
const uProjection = mat4.create();
const uProjectionLocation = gl.getUniformLocation(geometry.program, 'uProjection');
mat4.perspective(uProjection, 1, 1, .1, 20);
gl.uniformMatrix4fv(uProjectionLocation, false, uProjection);
gl.useProgram(null);
};
// returns an update() function for the light uniforms
// (used by the fragment shader that makes the lighting calculations)
const createLights = (program) => {
// Static directional Light
gl.useProgram(program);
gl.uniform3f(gl.getUniformLocation(program, 'uDirectionalLight'), -1, 1, 1);
gl.useProgram(null);
// Dynamic point lights
const lightData = new Float32Array([
1.3, 1.5, -2.6,
-2.6, 1.5, 1.3,
2.6, 1.5, -2.6,
1.3, 1.5, 1.8,
]);
const lights = [
new Float32Array(lightData.buffer, 0, 3),
new Float32Array(lightData.buffer, 12, 3),
new Float32Array(lightData.buffer, 24, 3),
new Float32Array(lightData.buffer, 36, 3),
];
const uPointLights = gl.getUniformLocation(program, 'uPointLights');
const update = () => {
gl.useProgram(program);
for (let i = 0; i < 4; i++) {
vec3.rotateY(lights[i], lights[i], vec3.fromValues(0,1,0), 0.025 + (i * .005));
}
gl.uniform3fv(uPointLights, lightData);
};
return { update };
};
const canvas = document.querySelector('canvas');
const gl = canvas.getContext('webgl2');
const geometry = createGeometryProgram();
const quad = createQuadProgram();
// MVP matrix uniforms are used by the geometry program to orient the camera
initializeMVPMatrices(geometry.program);
// Lights are fragment shader uniforms used for the lighting calculations
const lights = createLights(geometry.program);
const animate = () => {
lights.update();
geometry.draw();
quad.draw();
requestAnimationFrame(animate);
};
animate();