-
Notifications
You must be signed in to change notification settings - Fork 2
/
cube_camera.html
758 lines (653 loc) · 22 KB
/
cube_camera.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebGL Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>
const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl2', {antialias: true});
if (!gl) { alert('Failed to initialize WebGL'); }
(window.onresize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* account for e.g. high-retina macbook screens */
if (window.devicePixelRatio > 1) {
canvas.style.width = `${canvas.width}px`;
canvas.style.height = `${canvas.height}px`;
canvas.width *= window.devicePixelRatio;
canvas.height *= window.devicePixelRatio;
}
gl.viewport(
0,
0,
canvas.width,
canvas.height
);
})();
let shaders;
/* compile shaders */
{
const vs_geo = `
attribute vec3 a_pos;
attribute vec4 a_color;
uniform mat4 u_matrix;
varying vec4 v_color;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_color = a_color;
}`;
const fs_geo = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = vec4(v_color.xyz*0.35, 1.0);
}`;
const vs_grid = `#version 300 es
in vec3 a_pos;
in vec2 a_uv;
uniform mat4 u_matrix;
out vec2 v_uv;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_uv = (a_uv - 0.5)*abs(a_pos.xy);
}`;
const fs_grid = `#version 300 es
precision mediump float;
in vec2 v_uv;
/* https://iquilezles.org/articles/filterableprocedurals/ */
const float N = 30.0; // grid ratio
float gridTextureGradBox(in vec2 p, in vec2 ddx, in vec2 ddy) {
// filter kernel
vec2 w = max(abs(ddx), abs(ddy)) + 0.01;
// analytic (box) filtering
vec2 a = p + 0.5*w;
vec2 b = p - 0.5*w;
vec2 i = (floor(a)+min(fract(a)*N,1.0)-
floor(b)-min(fract(b)*N,1.0))/(N*w);
//pattern
return (1.0-i.x)*(1.0-i.y);
}
out vec4 frag_color;
void main() {
// vec2 d = 1.0 - step(0.01, fract(v_uv*10.0));
// gl_FragColor = vec4(max(d.x, d.y));
vec2 uv = (v_uv - 0.5) + 0.5/N;
float grid = gridTextureGradBox(uv, dFdx(uv), dFdy(uv));
frag_color = vec4(1.0 - grid);
frag_color *= 0.25;
frag_color *= 0.3 + 0.6*smoothstep(0.0, 0.1, 1.0 - length(v_uv)/5.5);
}`;
function createProgram(gl, vertexSource, fragmentSource) {
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader));
}
return shader;
}
const program = gl.createProgram();
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(program));
}
const wrapper = {program};
const numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i = 0; i < numAttributes; i++) {
const attribute = gl.getActiveAttrib(program, i);
wrapper[attribute.name] = gl.getAttribLocation(program, attribute.name);
}
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < numUniforms; i++) {
const uniform = gl.getActiveUniform(program, i);
wrapper[uniform.name] = gl.getUniformLocation(program, uniform.name);
}
return wrapper;
}
shaders = {
geo: createProgram(gl, vs_geo , fs_geo ),
grid: createProgram(gl, vs_grid, fs_grid),
}
}
const buf = {
geo_v_pos: gl.createBuffer(),
geo_v_color: gl.createBuffer(),
geo_i: gl.createBuffer(),
grid_v_pos: gl.createBuffer(),
grid_v_uv: gl.createBuffer(),
grid_i: gl.createBuffer(),
};
let input = {
pitch: Math.PI*0.25,
yaw: -Math.PI*0.25,
dampedEvent: { button: 0, movementX: 0, movementY: 0 },
cam_pivot_x: 0,
cam_pivot_y: 0,
cam_pivot_z: 0,
zoom: 10,
scroll: 0,
lmb_down: false,
rmb_down: false,
}
let wheelTimeout;
window.onwheel = e => {
if (input.mouse_down) return;
input.scroll = Math.sign(e.deltaY);
clearTimeout(wheelTimeout);
wheelTimeout = setTimeout(() => input.scroll = 0, 100)
};
window.onmousedown = ev => {
ev.preventDefault();
if (ev.button == 0) input.lmb_down = true;
if (ev.button == 2) input.rmb_down = true;
};
window.onmousemove = ev => {
input.dampedEvent.button = ev.button;
input.dampedEvent.movementX = ev.movementX;
input.dampedEvent.movementY = ev.movementY;
};
window.oncontextmenu = ev => ev.preventDefault();
window.onmouseup = ev => {
ev.preventDefault();
if (ev.button == 0) input.lmb_down = false;
if (ev.button == 2) input.rmb_down = false;
};
requestAnimationFrame(function frame(timestamp) {
requestAnimationFrame(frame);
/* we could apply these changes directly in the event handlers, but the result feels "low fps"
* because we don't get input events at 60fps. so instead, we apply the changes every frame
* and simply "damp them" so that they get weaker every frame.
*
* (this should probably use delta time rather than simply *= 0.8) */
{
const ev = input.dampedEvent;
/* based on the assumption that if you're zoomed in more,
* you're doing finer-detailed work and want more precise movements. */
const zoom_fudge = Math.sqrt(input.zoom/10.0)*2.0;
if (input.lmb_down) {
input.pitch -= ev.movementX * 0.0035 * zoom_fudge;
input.yaw -= ev.movementY * 0.0035 * zoom_fudge;
}
if (input.rmb_down) {
const unit = [0, -ev.movementX*0.0075*zoom_fudge, ev.movementY*0.0075*zoom_fudge, 1];
{
const view = mat4_create();
const scratch = mat4_create();
mat4_from_z_rotation(scratch, input.pitch);
mat4_mul(view, view, scratch);
mat4_from_y_rotation(scratch, input.yaw);
mat4_mul(view, view, scratch);
mat4_transform_vec4(unit, unit, view);
}
input.cam_pivot_x += unit[0];
input.cam_pivot_y += unit[1];
input.cam_pivot_z += unit[2];
}
ev.button *= 0.8;
ev.movementX *= 0.8;
ev.movementY *= 0.8;
}
input.zoom += 0.025*input.scroll*input.zoom;
const ORTHO = false;
const mvp = mat4_create();
const scratch = mat4_create();
{
const model = mat4_create();
{
// mat4_from_z_rotation(model, input.pitch);
// mat4_from_y_rotation(scratch, input.yaw);
// mat4_mul(model, scratch, model);
mat4_from_scaling(scratch, 0.5)
mat4_mul(model, scratch, model);
}
if (ORTHO) {
const ar = window.innerWidth / window.innerHeight;
const projection = mat4_ortho(mat4_create(), -ar, ar, -1, 1, -2, 2);
mat4_mul(mvp, projection, model);
} else {
const FIELD_OF_VIEW = 70 / 180 * Math.PI;
const ar = window.innerWidth / window.innerHeight;
const projection = mat4_perspective(mat4_create(), FIELD_OF_VIEW, ar, 0.01, 100.0);
const view = mat4_create();
{
const eye = [input.zoom, 0, 0, 1];
{
mat4_from_z_rotation(scratch, input.pitch);
mat4_mul(view, view, scratch);
mat4_from_y_rotation(scratch, input.yaw);
mat4_mul(view, view, scratch);
mat4_transform_vec4(eye, eye, view);
}
eye[0] += input.cam_pivot_x;
eye[1] += input.cam_pivot_y;
eye[2] += input.cam_pivot_z;
mat4_target_to(
view,
eye,
[input.cam_pivot_x, input.cam_pivot_y, input.cam_pivot_z],
[ 0, 0, 1]
);
mat4_invert(view, view);
}
mat4_mul(mvp, projection, view);
mat4_mul(mvp, mvp, model);
}
}
const geo_idx = [];
const geo_pos = [];
const geo_color = [];
{
const corners = [
-1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, /* Top face */
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, /* Bottom face */
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, /* Front face */
-1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, /* Back face */
1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, /* Right face */
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, /* Left face */
];
for (let corner_i = 0; corner_i < corners.length; corner_i += 3)
corners[corner_i+2] += 1.0;
for (let corner_i = 0; corner_i < corners.length; corner_i += 12) {
geo_color.push(
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
255, 0, 255, 255
)
const vbuf_i = geo_pos.length / 3;
geo_pos.push(corners[corner_i + 0], corners[corner_i + 1], corners[corner_i + 2]);
geo_pos.push(corners[corner_i + 3], corners[corner_i + 4], corners[corner_i + 5]);
geo_pos.push(corners[corner_i + 6], corners[corner_i + 7], corners[corner_i + 8]);
geo_pos.push(corners[corner_i + 9], corners[corner_i + 10], corners[corner_i + 11]);
geo_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 3,
vbuf_i + 0
);
}
}
const grid_idx = [];
const grid_pos = [];
const grid_uv = [];
{
const corners = [
-999, -999, -0, -999, 999, -0, 999, 999, -0, 999, -999, -0, /* Bottom face */
];
for (let corner_i = 0; corner_i < corners.length; corner_i += 3)
corners[corner_i+2] -= 0.025;
for (let corner_i = 0; corner_i < corners.length; corner_i += 12) {
grid_uv.push(
255, 0,
0, 0,
0, 255,
255, 255,
);
const vbuf_i = grid_pos.length / 3;
grid_pos.push(corners[corner_i + 0], corners[corner_i + 1], corners[corner_i + 2]);
grid_pos.push(corners[corner_i + 3], corners[corner_i + 4], corners[corner_i + 5]);
grid_pos.push(corners[corner_i + 6], corners[corner_i + 7], corners[corner_i + 8]);
grid_pos.push(corners[corner_i + 9], corners[corner_i + 10], corners[corner_i + 11]);
grid_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 3,
vbuf_i + 0
);
}
}
{
/* set up premultiplied alpha */
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
/* clear all */
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
/* geo pass */
{
gl.useProgram(shaders.geo.program);
gl.enableVertexAttribArray(shaders.geo.a_pos);
gl.enableVertexAttribArray(shaders.geo.a_color);
gl.uniformMatrix4fv(shaders.geo.u_matrix, false, mvp);
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, buf.geo_v_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, buf.geo_v_color);
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(geo_color), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf.geo_i);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geo_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, geo_idx.length, gl.UNSIGNED_SHORT, 0);
}
/* geo pass */
{
gl.useProgram(shaders.grid.program);
gl.enableVertexAttribArray(shaders.grid.a_pos);
gl.enableVertexAttribArray(shaders.grid.a_color);
gl.uniformMatrix4fv(shaders.grid.u_matrix, false, mvp);
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, buf.grid_v_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(grid_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.grid.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, buf.grid_v_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(grid_uv), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.grid.a_uv, 2, gl.UNSIGNED_BYTE, true, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf.grid_i);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(grid_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, grid_idx.length, gl.UNSIGNED_SHORT, 0);
}
}
})
function mat4_create() {
let out = new Float32Array(16);
out[0] = 1;
out[5] = 1;
out[10] = 1;
out[15] = 1;
return out;
}
function mat4_ortho(out, left, right, bottom, top, near, far) {
let lr = 1 / (left - right);
let bt = 1 / (bottom - top);
let nf = 1 / (near - far);
out[0] = -2 * lr;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = -2 * bt;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 2 * nf;
out[11] = 0;
out[12] = (left + right) * lr;
out[13] = (top + bottom) * bt;
out[14] = (far + near) * nf;
out[15] = 1;
return out;
}
function mat4_invert(out, a) {
let a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
let a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
let a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
let a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
let b00 = a00 * a11 - a01 * a10;
let b01 = a00 * a12 - a02 * a10;
let b02 = a00 * a13 - a03 * a10;
let b03 = a01 * a12 - a02 * a11;
let b04 = a01 * a13 - a03 * a11;
let b05 = a02 * a13 - a03 * a12;
let b06 = a20 * a31 - a21 * a30;
let b07 = a20 * a32 - a22 * a30;
let b08 = a20 * a33 - a23 * a30;
let b09 = a21 * a32 - a22 * a31;
let b10 = a21 * a33 - a23 * a31;
let b11 = a22 * a33 - a23 * a32;
/* Calculate the determinant */
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
return out;
}
function mat4_perspective(out, fovy, aspect, near, far) {
let f = 1.0 / Math.tan(fovy / 2),
nf;
out[0] = f / aspect;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = f;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[11] = -1;
out[12] = 0;
out[13] = 0;
out[15] = 0;
if (far != null && far !== Infinity) {
nf = 1 / (near - far);
out[10] = (far + near) * nf;
out[14] = 2 * far * near * nf;
} else {
out[10] = -1;
out[14] = -2 * near;
}
return out;
}
function mat4_from_x_rotation(out, rad) {
let s = Math.sin(rad);
let c = Math.cos(rad);
/* Perform axis-specific matrix multiplication */
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = c;
out[6] = s;
out[7] = 0;
out[8] = 0;
out[9] = -s;
out[10] = c;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
function mat4_from_y_rotation(out, rad) {
let s = Math.sin(rad);
let c = Math.cos(rad);
/* Perform axis-specific matrix multiplication */
out[0] = c;
out[1] = 0;
out[2] = -s;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = s;
out[9] = 0;
out[10] = c;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
function mat4_from_z_rotation(out, rad) {
let s = Math.sin(rad);
let c = Math.cos(rad);
/* Perform axis-specific matrix multiplication */
out[0] = c;
out[1] = s;
out[2] = 0;
out[3] = 0;
out[4] = -s;
out[5] = c;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
function mat4_from_scaling(out, f) {
out[0] = f;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = f;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = f;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
function mat4_from_translation(out, v) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = v[0];
out[13] = v[1];
out[14] = v[2];
out[15] = 1;
return out;
}
function mat4_transform_vec4(out, a, m) {
let x = a[0], y = a[1], z = a[2], w = a[3];
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return out;
}
function mat4_mul(out, a, b) {
let a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
let a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
let a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
let a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
/* Cache only the current line of the second matrix */
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[4];
b1 = b[5];
b2 = b[6];
b3 = b[7];
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[8];
b1 = b[9];
b2 = b[10];
b3 = b[11];
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[12];
b1 = b[13];
b2 = b[14];
b3 = b[15];
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
return out;
}
function mat4_target_to(out, eye, target, up=VEC3_UP) {
let eyex = eye[0], eyey = eye[1], eyez = eye[2],
upx = up[0], upy = up[1], upz = up[2];
let z0 = eyex - target[0],
z1 = eyey - target[1],
z2 = eyez - target[2];
let len = z0 * z0 + z1 * z1 + z2 * z2;
if (len > 0) {
len = 1 / Math.sqrt(len);
z0 *= len;
z1 *= len;
z2 *= len;
}
let x0 = upy * z2 - upz * z1,
x1 = upz * z0 - upx * z2,
x2 = upx * z1 - upy * z0;
len = x0 * x0 + x1 * x1 + x2 * x2;
if (len > 0) {
len = 1 / Math.sqrt(len);
x0 *= len;
x1 *= len;
x2 *= len;
}
out[0] = x0;
out[1] = x1;
out[2] = x2;
out[3] = 0;
out[4] = z1 * x2 - z2 * x1;
out[5] = z2 * x0 - z0 * x2;
out[6] = z0 * x1 - z1 * x0;
out[7] = 0;
out[8] = z0;
out[9] = z1;
out[10] = z2;
out[11] = 0;
out[12] = eyex;
out[13] = eyey;
out[14] = eyez;
out[15] = 1;
return out;
}
</script>
</body>
</html>