-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny-graphics.js
1437 lines (1251 loc) · 70 KB
/
tiny-graphics.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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file A file that shows how to organize a complete graphics program.
* It wraps common WebGL commands and math.
* The file tiny-graphics-widgets.js additionally wraps web page interactions. By Garett.
*
* This file will consist of a number of class definitions that we will
* export. To organize the exports, we will both declare each class in
* local scope (const) as well as store them in this JS object:
*
* Organization of this file: Math definitions, then graphics definitions.
*
* Vector and Matrix algebra are not built into JavaScript at first. We will add it now.
*
* You will be able to declare a 3D vector [x,y,z] supporting various common vector operations
* with syntax: vec(x,y), vec3( x,y,z ) or vec4( x,y,z, zero or one ). For general sized vectors, use
* class Vector and declare them with standard Array-supported operations like .of().
*
* For matrices, you will use class Mat4 to generate the 4 by 4 matrices that are common
* in graphics, or for general sized matrices you can use class Matrix.
*
* To get vector algebra that performs well in JavaScript, we based class Vector on consecutive
* buffers (using type Float32Array). Implementations should specialize for common vector
* sizes 3 and 4 since JavaScript engines can better optimize functions when they can predict
* argument count. Implementations should also avoid allocating new array objects since these
* will all have to be garbage collected.
*
* Examples:
* ** For size 3 **
* equals: "vec3( 1,0,0 ).equals( vec3( 1,0,0 ) )" returns true.
* plus: "vec3( 1,0,0 ).plus ( vec3( 1,0,0 ) )" returns the Vector [ 2,0,0 ].
* minus: "vec3( 1,0,0 ).minus ( vec3( 1,0,0 ) )" returns the Vector [ 0,0,0 ].
* mult-pairs: "vec3( 1,2,3 ).mult_pairs( vec3( 3,2,0 ) )" returns the Vector [ 3,4,0 ].
* scale: "vec3( 1,2,3 ).scale( 2 )" overwrites the Vector with [ 2,4,6 ].
* times: "vec3( 1,2,3 ).times( 2 )" returns the Vector [ 2,4,6 ].
* randomized: Returns this Vector plus a random vector of a given maximum length.
* mix: "vec3( 0,2,4 ).mix( vec3( 10,10,10 ), .5 )" returns the Vector [ 5,6,7 ].
* norm: "vec3( 1,2,3 ).norm()" returns the square root of 15.
* normalized: "vec3( 4,4,4 ).normalized()" returns the Vector [ sqrt(3), sqrt(3), sqrt(3) ]
* normalize: "vec3( 4,4,4 ).normalize()" overwrites the Vector with [ sqrt(3), sqrt(3), sqrt(3) ].
* dot: "vec3( 1,2,3 ).dot( vec3( 1,2,3 ) )" returns 15.
* cast: "vec3.cast( [-1,-1,0], [1,-1,0], [-1,1,0] )" converts a list of Array literals into a list of vec3's.
* to4: "vec3( 1,2,3 ).to4( true or false )" returns the homogeneous vec4 [ 1,2,3, 1 or 0 ].
* cross: "vec3( 1,0,0 ).cross( vec3( 0,1,0 ) )" returns the Vector [ 0,0,1 ]. Use only on 3x1 Vecs.
* to_string: "vec3( 1,2,3 ).to_string()" returns "[vec3 1, 2, 3]"
* ** For size 4, same except: **
* to3: "vec4( 4,3,2,1 ).to3()" returns the vec3 [ 4,3,2 ]. Use to truncate vec4 to vec3.
* ** To assign by value **
* copy: "let new_vector = old_vector.copy()" assigns by value so you get a different vector object.
* ** For any size **
* to declare: Vector.of( 1,2,3,4,5,6,7,8,9,10 ) returns a Vector filled with those ten entries.
* ** For multiplication by matrices **
* "any_mat4.times( vec4( 1,2,3,0 ) )" premultiplies the homogeneous Vector [1,2,3]
* by the 4x4 matrix and returns the new vec4. Requires a vec4 as input.
*/
export const tiny = {};
/**
* **Vector** stores vectors of floating point numbers. Puts vector math into JavaScript.
* @Note Vectors should be created with of() due to wierdness with the TypedArray spec.
* @Tip Assign Vectors with .copy() to avoid referring two variables to the same Vector object.
* @type {tiny.Vector}
*/
const Vector = tiny.Vector =
class Vector extends Float32Array {
static create(...arr) {
return new Vector(arr);
}
// cast(): For compact syntax when declaring lists.
static cast(...args) {
return args.map(x => Vector.from(x))
}
copy() {
return new Vector(this)
}
equals(b) {
return this.every((x, i) => x == b[i])
}
plus(b) {
return this.map((x, i) => x + b[i])
}
minus(b) {
return this.map((x, i) => x - b[i])
}
times_pairwise(b) {
return this.map((x, i) => x * b[i])
}
scale_by(s) {
this.forEach((x, i, a) => a[i] *= s)
}
times(s) {
return this.map(x => s * x)
}
randomized(s) {
return this.map(x => x + s * (Math.random() - .5))
}
mix(b, s) {
return this.map((x, i) => (1 - s) * x + s * b[i])
}
norm() {
return Math.sqrt(this.dot(this))
}
normalized() {
return this.times(1 / this.norm())
}
normalize() {
this.scale_by(1 / this.norm())
}
dot(b) {
// Optimize for Vectors of size 2
if (this.length == 2)
return this[0] * b[0] + this[1] * b[1];
return this.reduce((acc, x, i) => {
return acc + x * b[i];
}, 0);
}
// to3() / to4() / cross(): For standardizing the API with Vector3/Vector4,
// so the performance hit of changing between these types can be measured.
to3() {
return vec3(this[0], this[1], this[2]);
}
to4(is_a_point) {
return vec4(this[0], this[1], this[2], +is_a_point);
}
cross(b) {
return vec3(this[1] * b[2] - this[2] * b[1],
this[2] * b[0] - this[0] * b[2],
this[0] * b[1] - this[1] * b[0]);
}
to_string() {
return "[vector " + this.join(", ") + "]"
}
}
const Vector3 = tiny.Vector3 =
class Vector3 extends Float32Array {
// **Vector3** is a specialization of Vector only for size 3, for performance reasons.
static create(x, y, z) {
const v = new Vector3(3);
v[0] = x;
v[1] = y;
v[2] = z;
return v;
}
static cast(...args) {
// cast(): Converts a bunch of arrays into a bunch of vec3's.
return args.map(x => Vector3.from(x));
}
static unsafe(x, y, z) {
// unsafe(): returns vec3s only meant to be consumed immediately. Aliases into
// shared memory, to be overwritten upon next unsafe3 call. Faster.
const shared_memory = vec3(0, 0, 0);
Vector3.unsafe = (x, y, z) => {
shared_memory[0] = x;
shared_memory[1] = y;
shared_memory[2] = z;
return shared_memory;
}
return Vector3.unsafe(x, y, z);
}
copy() {
return Vector3.from(this)
}
// In-fix operations: Use these for more readable math expressions.
equals(b) {
return this[0] == b[0] && this[1] == b[1] && this[2] == b[2]
}
plus(b) {
return vec3(this[0] + b[0], this[1] + b[1], this[2] + b[2])
}
minus(b) {
return vec3(this[0] - b[0], this[1] - b[1], this[2] - b[2])
}
times(s) {
return vec3(this[0] * s, this[1] * s, this[2] * s)
}
times_pairwise(b) {
return vec3(this[0] * b[0], this[1] * b[1], this[2] * b[2])
}
// Pre-fix operations: Use these for better performance (to avoid new allocation).
add_by(b) {
this[0] += b[0];
this[1] += b[1];
this[2] += b[2]
}
subtract_by(b) {
this[0] -= b[0];
this[1] -= b[1];
this[2] -= b[2]
}
scale_by(s) {
this[0] *= s;
this[1] *= s;
this[2] *= s
}
scale_pairwise_by(b) {
this[0] *= b[0];
this[1] *= b[1];
this[2] *= b[2]
}
// Other operations:
randomized(s) {
return vec3(this[0] + s * (Math.random() - .5),
this[1] + s * (Math.random() - .5),
this[2] + s * (Math.random() - .5));
}
mix(b, s) {
return vec3((1 - s) * this[0] + s * b[0],
(1 - s) * this[1] + s * b[1],
(1 - s) * this[2] + s * b[2]);
}
norm() {
return Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2])
}
normalized() {
const d = 1 / this.norm();
return vec3(this[0] * d, this[1] * d, this[2] * d);
}
normalize() {
const d = 1 / this.norm();
this[0] *= d;
this[1] *= d;
this[2] *= d;
}
dot(b) {
return this[0] * b[0] + this[1] * b[1] + this[2] * b[2]
}
cross(b) {
return vec3(this[1] * b[2] - this[2] * b[1],
this[2] * b[0] - this[0] * b[2],
this[0] * b[1] - this[1] * b[0])
}
to4(is_a_point)
// to4(): Convert to a homogeneous vector of 4 values.
{
return vec4(this[0], this[1], this[2], +is_a_point)
}
to_string() {
return "[vec3 " + this.join(", ") + "]"
}
}
const Vector4 = tiny.Vector4 =
class Vector4 extends Float32Array {
// **Vector4** is a specialization of Vector only for size 4, for performance reasons.
// The fourth coordinate value is homogenized (0 for a vector, 1 for a point).
static create(x, y, z, w) {
const v = new Vector4(4);
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = w;
return v;
}
static unsafe(x, y, z, w) {
// **unsafe** Returns vec3s to be used immediately only. Aliases into
// shared memory to be overwritten on next unsafe3 call. Faster.
const shared_memory = vec4(0, 0, 0, 0);
Vec4.unsafe = (x, y, z, w) => {
shared_memory[0] = x;
shared_memory[1] = y;
shared_memory[2] = z;
shared_memory[3] = w;
}
}
copy() {
return Vector4.from(this)
}
// In-fix operations: Use these for more readable math expressions.
equals() {
return this[0] == b[0] && this[1] == b[1] && this[2] == b[2] && this[3] == b[3]
}
plus(b) {
return vec4(this[0] + b[0], this[1] + b[1], this[2] + b[2], this[3] + b[3])
}
minus(b) {
return vec4(this[0] - b[0], this[1] - b[1], this[2] - b[2], this[3] - b[3])
}
times(s) {
return vec4(this[0] * s, this[1] * s, this[2] * s, this[3] * s)
}
times_pairwise(b) {
return vec4(this[0] * b[0], this[1] * b[1], this[2] * b[2], this[3] * b[3])
}
// Pre-fix operations: Use these for better performance (to avoid new allocation).
add_by(b) {
this[0] += b[0];
this[1] += b[1];
this[2] += b[2];
this[3] += b[3]
}
subtract_by(b) {
this[0] -= b[0];
this[1] -= b[1];
this[2] -= b[2];
this[3] -= b[3]
}
scale_by(s) {
this[0] *= s;
this[1] *= s;
this[2] *= s;
this[3] *= s
}
scale_pairwise_by(b) {
this[0] *= b[0];
this[1] *= b[1];
this[2] *= b[2];
this[3] *= b[3]
}
// Other operations:
randomized(s) {
return vec4(this[0] + s * (Math.random() - .5),
this[1] + s * (Math.random() - .5),
this[2] + s * (Math.random() - .5),
this[3] + s * (Math.random() - .5));
}
mix(b, s) {
return vec4((1 - s) * this[0] + s * b[0],
(1 - s) * this[1] + s * b[1],
(1 - s) * this[2] + s * b[2],
(1 - s) * this[3] + s * b[3]);
}
norm() {
// The norms should behave like for Vector3 because of the homogenous format.
return Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2])
}
normalized() {
const d = 1 / this.norm();
return vec4(this[0] * d, this[1] * d, this[2] * d, this[3]);
// (leaves the 4th coord alone)
}
normalize() {
const d = 1 / this.norm();
this[0] *= d;
this[1] *= d;
this[2] *= d;
// (leaves the 4th coord alone)
}
dot(b) {
return this[0] * b[0] + this[1] * b[1] + this[2] * b[2] + this[3] * b[3]
}
to3() {
return vec3(this[0], this[1], this[2])
}
to_string() {
return "[vec4 " + this.join(", ") + "]"
}
}
const vec = tiny.vec = Vector.create;
const vec3 = tiny.vec3 = Vector3.create;
const vec4 = tiny.vec4 = Vector4.create;
const unsafe3 = tiny.unsafe3 = Vector3.unsafe;
const unsafe4 = tiny.unsafe4 = Vector4.unsafe;
// **Color** is an alias for class Vector4. Colors should be made as special 4x1
// vectors expressed as ( red, green, blue, opacity ) each ranging from 0 to 1.
const Color = tiny.Color =
class Color extends Vector4 {
// Create color from RGBA floats
static create_from_float(r, g, b, a) {
const v = new Vector4(4);
v[0] = r;
v[1] = g;
v[2] = b;
v[3] = a;
return v;
}
// Create color from hexadecimal numbers, e.g., #FFFFFF
static create_from_hex(hex, alpha = 1.) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
const v = new Vector4(4);
if (result) {
v[0] = parseInt(result[1], 16) / 255.;
v[1] = parseInt(result[2], 16) / 255.;
v[2] = parseInt(result[3], 16) / 255.;
v[3] = alpha;
}
return v;
}
}
const color = tiny.color = Color.create_from_float;
const hex_color = tiny.hex_color = Color.create_from_hex;
const Matrix = tiny.Matrix =
class Matrix extends Array {
// **Matrix** holds M by N matrices of floats. Enables matrix and vector math.
// Example usage:
// "Matrix( rows )" returns a Matrix with those rows, where rows is an array of float arrays.
// "M.set_identity( m, n )" assigns the m by n identity to Matrix M.
// "M.sub_block( start, end )" where start and end are each a [ row, column ] pair returns a sub-rectangle cut out from M.
// "M.copy()" creates a deep copy of M and returns it so you can modify it without affecting the original.
// "M.equals(b)", "M.plus(b)", and "M.minus(b)" are operations betwen two matrices.
// "M.transposed()" returns a new matrix where all rows of M became columns and vice versa.
// "M.times(b)" (where the post-multiplied b can be a scalar, a Vector4, or another Matrix) returns a
// new Matrix or Vector4 holding the product.
// "M.pre_multiply(b)" overwrites the Matrix M with the product of b * M where b must be another Matrix.
// "M.post_multiply(b)" overwrites the Matrix M with the product of M * b where b can be a Matrix or scalar.
// "Matrix.flatten_2D_to_1D( M )" flattens input (a Matrix or any array of Vectors or float arrays)
// into a row-major 1D array of raw floats.
// "M.to_string()" where M contains the 4x4 identity returns "[[1, 0, 0, 0] [0, 1, 0, 0] [0, 0, 1, 0] [0, 0, 0, 1]]".
constructor(...args) {
super(0);
this.push(...args)
}
static flatten_2D_to_1D(M) {
let index = 0, floats = new Float32Array(M.length && M.length * M[0].length);
for (let i = 0; i < M.length; i++) for (let j = 0; j < M[i].length; j++) floats[index++] = M[i][j];
return floats;
}
set(M) {
this.length = 0;
this.push(...M);
}
set_identity(m, n) {
this.length = 0;
for (let i = 0; i < m; i++) {
this.push(Array(n).fill(0));
if (i < n) this[i][i] = 1;
}
}
sub_block(start, end) {
return Matrix.from(this.slice(start[0], end[0]).map(r => r.slice(start[1], end[1])));
}
copy() {
return this.map(r => [...r])
}
equals(b) {
return this.every((r, i) => r.every((x, j) => x == b[i][j]))
}
plus(b) {
return this.map((r, i) => r.map((x, j) => x + b[i][j]))
}
minus(b) {
return this.map((r, i) => r.map((x, j) => x - b[i][j]))
}
transposed() {
return this.map((r, i) => r.map((x, j) => this[j][i]))
}
times(b, optional_preallocated_result) {
const len = b.length;
if (typeof len === "undefined") return this.map(r => r.map(x => b * x));
// Matrix * scalar case.
const len2 = b[0].length;
if (typeof len2 === "undefined") {
let result = optional_preallocated_result || new Vector4(this.length);
// Matrix * Vector4 case.
for (let r = 0; r < len; r++) result[r] = b.dot(this[r]);
return result;
}
let result = optional_preallocated_result || Matrix.from(new Array(this.length));
for (let r = 0; r < this.length; r++) {
// Matrix * Matrix case.
if (!optional_preallocated_result)
result[r] = new Array(len2);
for (let c = 0, sum = 0; c < len2; c++) {
result[r][c] = 0;
for (let r2 = 0; r2 < len; r2++)
result[r][c] += this[r][r2] * b[r2][c];
}
}
return result;
}
pre_multiply(b) {
const new_value = b.times(this);
this.length = 0;
this.push(...new_value);
return this;
}
post_multiply(b) {
const new_value = this.times(b);
this.length = 0;
this.push(...new_value);
return this;
}
to_string() {
return "[" + this.map((r, i) => "[" + r.join(", ") + "]").join(" ") + "]"
}
}
const Mat4 = tiny.Mat4 =
class Mat4 extends Matrix {
// **Mat4** generates special 4x4 matrices that are useful for graphics.
// All the methods below return a certain 4x4 matrix.
static identity() {
return Matrix.of([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]);
};
static rotation(angle, x, y, z) {
// rotation(): Requires a scalar (angle) and a three-component axis vector.
const normalize = (x, y, z) => {
const n = Math.sqrt(x * x + y * y + z * z);
return [x / n, y / n, z / n]
}
let [i, j, k] = normalize(x, y, z),
[c, s] = [Math.cos(angle), Math.sin(angle)],
omc = 1.0 - c;
return Matrix.of([i * i * omc + c, i * j * omc - k * s, i * k * omc + j * s, 0],
[i * j * omc + k * s, j * j * omc + c, j * k * omc - i * s, 0],
[i * k * omc - j * s, j * k * omc + i * s, k * k * omc + c, 0],
[0, 0, 0, 1]);
}
static scale(x, y, z) {
// scale(): Builds and returns a scale matrix using x,y,z.
return Matrix.of([x, 0, 0, 0],
[0, y, 0, 0],
[0, 0, z, 0],
[0, 0, 0, 1]);
}
static translation(x, y, z) {
// translation(): Builds and returns a translation matrix using x,y,z.
return Matrix.of([1, 0, 0, x],
[0, 1, 0, y],
[0, 0, 1, z],
[0, 0, 0, 1]);
}
static look_at(eye, at, up) {
// look_at(): Produce a traditional graphics camera "lookat" matrix.
// Each input must be a 3x1 Vector.
// Note: look_at() assumes the result will be used for a camera and stores its
// result in inverse space.
// If you want to use look_at to point a non-camera towards something, you can
// do so, but to generate the correct basis you must re-invert its result.
// Compute vectors along the requested coordinate axes. "y" is the "updated" and orthogonalized local y axis.
let z = at.minus(eye).normalized(),
x = z.cross(up).normalized(),
y = x.cross(z).normalized();
// Check for NaN, indicating a degenerate cross product, which
// happens if eye == at, or if at minus eye is parallel to up.
if (!x.every(i => i == i))
throw "Two parallel vectors were given";
z.scale_by(-1); // Enforce right-handed coordinate system.
return Mat4.translation(-x.dot(eye), -y.dot(eye), -z.dot(eye))
.times(Matrix.of(x.to4(0), y.to4(0), z.to4(0), vec4(0, 0, 0, 1)));
}
static orthographic(left, right, bottom, top, near, far) {
// orthographic(): Box-shaped view volume for projection.
return Mat4.scale(vec3(1 / (right - left), 1 / (top - bottom), 1 / (far - near)))
.times(Mat4.translation(vec3(-left - right, -top - bottom, -near - far)))
.times(Mat4.scale(vec3(2, 2, -2)));
}
static perspective(fov_y, aspect, near, far) {
// perspective(): Frustum-shaped view volume for projection.
const f = 1 / Math.tan(fov_y / 2), d = far - near;
return Matrix.of([f / aspect, 0, 0, 0],
[0, f, 0, 0],
[0, 0, -(near + far) / d, -2 * near * far / d],
[0, 0, -1, 0]);
}
static inverse(m) {
// inverse(): A 4x4 inverse. Computing it is slow because of
// the amount of steps; call fewer times when possible.
const result = Mat4.identity(), m00 = m[0][0], m01 = m[0][1], m02 = m[0][2], m03 = m[0][3],
m10 = m[1][0], m11 = m[1][1], m12 = m[1][2], m13 = m[1][3],
m20 = m[2][0], m21 = m[2][1], m22 = m[2][2], m23 = m[2][3],
m30 = m[3][0], m31 = m[3][1], m32 = m[3][2], m33 = m[3][3];
result[0][0] = m12 * m23 * m31 - m13 * m22 * m31 + m13 * m21 * m32 - m11 * m23 * m32 - m12 * m21 * m33 + m11 * m22 * m33;
result[0][1] = m03 * m22 * m31 - m02 * m23 * m31 - m03 * m21 * m32 + m01 * m23 * m32 + m02 * m21 * m33 - m01 * m22 * m33;
result[0][2] = m02 * m13 * m31 - m03 * m12 * m31 + m03 * m11 * m32 - m01 * m13 * m32 - m02 * m11 * m33 + m01 * m12 * m33;
result[0][3] = m03 * m12 * m21 - m02 * m13 * m21 - m03 * m11 * m22 + m01 * m13 * m22 + m02 * m11 * m23 - m01 * m12 * m23;
result[1][0] = m13 * m22 * m30 - m12 * m23 * m30 - m13 * m20 * m32 + m10 * m23 * m32 + m12 * m20 * m33 - m10 * m22 * m33;
result[1][1] = m02 * m23 * m30 - m03 * m22 * m30 + m03 * m20 * m32 - m00 * m23 * m32 - m02 * m20 * m33 + m00 * m22 * m33;
result[1][2] = m03 * m12 * m30 - m02 * m13 * m30 - m03 * m10 * m32 + m00 * m13 * m32 + m02 * m10 * m33 - m00 * m12 * m33;
result[1][3] = m02 * m13 * m20 - m03 * m12 * m20 + m03 * m10 * m22 - m00 * m13 * m22 - m02 * m10 * m23 + m00 * m12 * m23;
result[2][0] = m11 * m23 * m30 - m13 * m21 * m30 + m13 * m20 * m31 - m10 * m23 * m31 - m11 * m20 * m33 + m10 * m21 * m33;
result[2][1] = m03 * m21 * m30 - m01 * m23 * m30 - m03 * m20 * m31 + m00 * m23 * m31 + m01 * m20 * m33 - m00 * m21 * m33;
result[2][2] = m01 * m13 * m30 - m03 * m11 * m30 + m03 * m10 * m31 - m00 * m13 * m31 - m01 * m10 * m33 + m00 * m11 * m33;
result[2][3] = m03 * m11 * m20 - m01 * m13 * m20 - m03 * m10 * m21 + m00 * m13 * m21 + m01 * m10 * m23 - m00 * m11 * m23;
result[3][0] = m12 * m21 * m30 - m11 * m22 * m30 - m12 * m20 * m31 + m10 * m22 * m31 + m11 * m20 * m32 - m10 * m21 * m32;
result[3][1] = m01 * m22 * m30 - m02 * m21 * m30 + m02 * m20 * m31 - m00 * m22 * m31 - m01 * m20 * m32 + m00 * m21 * m32;
result[3][2] = m02 * m11 * m30 - m01 * m12 * m30 - m02 * m10 * m31 + m00 * m12 * m31 + m01 * m10 * m32 - m00 * m11 * m32;
result[3][3] = m01 * m12 * m20 - m02 * m11 * m20 + m02 * m10 * m21 - m00 * m12 * m21 - m01 * m10 * m22 + m00 * m11 * m22;
// Divide by determinant and return.
return result.times(1 / (m00 * result[0][0] + m10 * result[0][1] + m20 * result[0][2] + m30 * result[0][3]));
}
}
const Keyboard_Manager = tiny.Keyboard_Manager =
class Keyboard_Manager {
// **Keyboard_Manager** maintains a running list of which keys are depressed. You can map combinations of
// shortcut keys to trigger callbacks you provide by calling add(). See add()'s arguments. The shortcut
// list is indexed by convenient strings showing each bound shortcut combination. The constructor
// optionally takes "target", which is the desired DOM element for keys to be pressed inside of, and
// "callback_behavior", which will be called for every key action and allows extra behavior on each event
// -- giving an opportunity to customize their bubbling, preventDefault, and more. It defaults to no
// additional behavior besides the callback itself on each assigned key action.
constructor(target = document, callback_behavior = (callback, event) => callback(event)) {
this.saved_controls = {};
this.actively_pressed_keys = new Set();
this.callback_behavior = callback_behavior;
target.addEventListener("keydown", this.key_down_handler.bind(this));
target.addEventListener("keyup", this.key_up_handler.bind(this));
window.addEventListener("focus", () => this.actively_pressed_keys.clear());
// Deal with stuck keys during focus change.
}
key_down_handler(event) {
if (["INPUT", "TEXTAREA"].includes(event.target.tagName))
return;
// Don't interfere with typing.
this.actively_pressed_keys.add(event.key);
// Track the pressed key.
for (let saved of Object.values(this.saved_controls)) {
// Re-check all the keydown handlers.
if (saved.shortcut_combination.every(s => this.actively_pressed_keys.has(s))
&& event.ctrlKey == saved.shortcut_combination.includes("Control")
&& event.shiftKey == saved.shortcut_combination.includes("Shift")
&& event.altKey == saved.shortcut_combination.includes("Alt")
&& event.metaKey == saved.shortcut_combination.includes("Meta"))
// Modifiers must exactly match.
this.callback_behavior(saved.callback, event);
// The keys match, so fire the callback.
}
}
key_up_handler(event) {
const lower_symbols = "qwertyuiopasdfghjklzxcvbnm1234567890-=[]\\;',./",
upper_symbols = "QWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*()_+{}|:\"<>?";
const lifted_key_symbols = [event.key, upper_symbols[lower_symbols.indexOf(event.key)],
lower_symbols[upper_symbols.indexOf(event.key)]];
// Call keyup for any shortcuts
for (let saved of Object.values(this.saved_controls)) // that depended on the released
if (lifted_key_symbols.some(s => saved.shortcut_combination.includes(s))) // key or its shift-key counterparts.
this.callback_behavior(saved.keyup_callback, event); // The keys match, so fire the callback.
lifted_key_symbols.forEach(k => this.actively_pressed_keys.delete(k));
}
add(shortcut_combination, callback = () => {
}, keyup_callback = () => {
}) { // add(): Creates a keyboard operation. The argument shortcut_combination wants an
// array of strings that follow standard KeyboardEvent key names. Both the keyup
// and keydown callbacks for any key combo are optional.
this.saved_controls[shortcut_combination.join('+')] = {shortcut_combination, callback, keyup_callback};
}
}
const Graphics_Card_Object = tiny.Graphics_Card_Object =
class Graphics_Card_Object {
// ** Graphics_Card_Object** Extending this base class allows an object to
// copy itself onto a WebGL context on demand, whenever it is first used for
// a GPU draw command on a context it hasn't seen before.
constructor() {
this.gpu_instances = new Map()
} // Track which GPU contexts this object has copied itself onto.
copy_onto_graphics_card(context, intial_gpu_representation) {
// copy_onto_graphics_card(): Our object might need to register to multiple
// GPU contexts in the case of multiple drawing areas. If this is a new GPU
// context for this object, copy the object to the GPU. Otherwise, this
// object already has been copied over, so get a pointer to the existing
// instance. The instance consists of whatever GPU pointers are associated
// with this object, as returned by the WebGL calls that copied it to the
// GPU. GPU-bound objects should override this function, which builds an
// initial instance, so as to populate it with finished pointers.
const existing_instance = this.gpu_instances.get(context);
// Warn the user if they are avoidably making too many GPU objects. Beginner
// WebGL programs typically only need to call copy_onto_graphics_card once
// per object; doing it more is expensive, so warn them with an "idiot
// alarm". Don't trigger the idiot alarm if the user is correctly re-using
// an existing GPU context and merely overwriting parts of itself.
if (!existing_instance) {
Graphics_Card_Object.idiot_alarm |= 0; // Start a program-wide counter.
if (Graphics_Card_Object.idiot_alarm++ > 200)
throw `Error: You are sending a lot of object definitions to the GPU, probably by mistake!
Many of them are likely duplicates, which you don't want since sending each one is very slow.
To avoid this, from your display() function avoid ever declaring a Shape Shader or Texture (or
subclass of these) with "new", thus causing the definition to be re-created and re-transmitted every
frame. Instead, call these in your scene's constructor and keep the result as a class member,
or otherwise make sure it only happens once. In the off chance that you have a somehow deformable
shape that MUST change every frame, then at least use the special arguments of
copy_onto_graphics_card to limit which buffers get overwritten every frame to only
the necessary ones.`;
}
// Check if this object already exists on that GPU context.
return existing_instance || // If necessary, start a new object associated with the context.
this.gpu_instances.set(context, intial_gpu_representation).get(context);
}
activate(context, ...args) { // activate(): To use, super call it to retrieve a container of GPU
// pointers associated with this object. If none existed one will be created.
// Then do any WebGL calls you need that require GPU pointers.
return this.gpu_instances.get(context) || this.copy_onto_graphics_card(context, ...args)
}
}
const Vertex_Buffer = tiny.Vertex_Buffer =
class Vertex_Buffer extends Graphics_Card_Object {
// **Vertex_Buffer** organizes data related to one 3D shape and copies it into GPU memory. That data
// is broken down per vertex in the shape. To use, make a subclass of it that overrides the
// constructor and fills in the "arrays" property. Within "arrays", you can make several fields that
// you can look up in a vertex; for each field, a whole array will be made here of that data type and
// it will be indexed per vertex. Along with those lists is an additional array "indices" describing
// how vertices are connected to each other into shape primitives. Primitives could includes
// triangles, expressed as triples of vertex indices.
constructor(...array_names) {
// This superclass constructor expects a list of names of arrays that you plan for.
super();
[this.arrays, this.indices] = [{}, []];
// Initialize a blank array member of the Shape with each of the names provided:
for (let name of array_names) this.arrays[name] = [];
}
copy_onto_graphics_card(context, selection_of_arrays = Object.keys(this.arrays), write_to_indices = true) {
// copy_onto_graphics_card(): Called automatically as needed to load this vertex array set onto
// one of your GPU contexts for its first time. Send the completed vertex and index lists to
// their own buffers within any of your existing graphics card contexts. Optional arguments
// allow calling this again to overwrite the GPU buffers related to this shape's arrays, or
// subsets of them as needed (if only some fields of your shape have changed).
// Define what this object should store in each new WebGL Context:
const initial_gpu_representation = {webGL_buffer_pointers: {}};
// Our object might need to register to multiple GPU contexts in the case of
// multiple drawing areas. If this is a new GPU context for this object,
// copy the object to the GPU. Otherwise, this object already has been
// copied over, so get a pointer to the existing instance.
const did_exist = this.gpu_instances.get(context);
const gpu_instance = super.copy_onto_graphics_card(context, initial_gpu_representation);
const gl = context;
const write = did_exist ? (target, data) => gl.bufferSubData(target, 0, data)
: (target, data) => gl.bufferData(target, data, gl.STATIC_DRAW);
for (let name of selection_of_arrays) {
if (!did_exist)
gpu_instance.webGL_buffer_pointers[name] = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, gpu_instance.webGL_buffer_pointers[name]);
write(gl.ARRAY_BUFFER, Matrix.flatten_2D_to_1D(this.arrays[name]));
}
if (this.indices.length && write_to_indices) {
if (!did_exist)
gpu_instance.index_buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gpu_instance.index_buffer);
write(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array(this.indices));
}
return gpu_instance;
}
execute_shaders(gl, gpu_instance, type) // execute_shaders(): Draws this shape's entire vertex buffer.
{ // Draw shapes using indices if they exist. Otherwise, assume the vertices are arranged as triples.
if (this.indices.length) {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gpu_instance.index_buffer);
gl.drawElements(gl[type], this.indices.length, gl.UNSIGNED_INT, 0)
} else gl.drawArrays(gl[type], 0, Object.values(this.arrays)[0].length);
}
draw(webgl_manager, program_state, model_transform, material, type = "TRIANGLES") {
// draw(): To appear onscreen, a shape of any variety goes through this function,
// which executes the shader programs. The shaders draw the right shape due to
// pre-selecting the correct buffer region in the GPU that holds that shape's data.
const gpu_instance = this.activate(webgl_manager.context);
material.shader.activate(webgl_manager.context, gpu_instance.webGL_buffer_pointers, program_state, model_transform, material);
// Run the shaders to draw every triangle now:
this.execute_shaders(webgl_manager.context, gpu_instance, type);
}
}
const Shape = tiny.Shape =
class Shape extends Vertex_Buffer {
// **Shape** extends Vertex_Buffer to give it an awareness that it holds data about 3D space. This class
// is used the same way as Vertex_Buffer, by subclassing it and writing a constructor that fills in the
// "arrays" property with some custom arrays.
// Shape extends Vertex_Buffer's functionality for copying shapes into buffers the graphics card's memory,
// adding the basic assumption that each vertex will have a 3D position and a 3D normal vector as available
// fields to look up. This means there will be at least two arrays for the user to fill in: "positions"
// enumerating all the vertices' locations, and "normals" enumerating all vertices' normal vectors pointing
// away from the surface. Both are of type Vector3.
// By including these, Shape adds to class Vertex_Buffer the ability to compound shapes in together into a
// single performance-friendly Vertex_Buffer, placing this shape into a larger one at a custom transforms by
// adjusting positions and normals with a call to insert_transformed_copy_into(). Compared to Vertex_Buffer
// we also gain the ability via flat-shading to compute normals from scratch for a shape that has none, and
// the ability to eliminate inter-triangle sharing of vertices for any data we want to abruptly vary as we
// cross over a triangle edge (such as texture images).
// Like in class Vertex_Buffer we have an array "indices" to fill in as well, a list of index triples
// defining which three vertices belong to each triangle. Call new on a Shape and fill its arrays (probably
// in an overridden constructor).
// IMPORTANT: To use this class you must define all fields for every single vertex by filling in the arrays
// of each field, so this includes positions, normals, any more fields a specific Shape subclass decides to
// include per vertex, such as texture coordinates. Be warned that leaving any empty elements in the lists
// will result in an out of bounds GPU warning (and nothing drawn) whenever the "indices" list contains
// references to that position in the lists.
static insert_transformed_copy_into(recipient, args, points_transform = Mat4.identity()) {
// insert_transformed_copy_into(): For building compound shapes. A copy of this shape is made
// and inserted into any recipient shape you pass in. Compound shapes help reduce draw calls
// and speed up performane.
// Here if you try to bypass making a temporary shape and instead directly insert new data into
// the recipient, you'll run into trouble when the recursion tree stops at different depths.
const temp_shape = new this(...args);
recipient.indices.push(...temp_shape.indices.map(i => i + recipient.arrays.position.length));
// Copy each array from temp_shape into the recipient shape:
for (let a in temp_shape.arrays) {
// Apply points_transform to all points added during this call:
if (a == "position" || a == "tangents")
recipient.arrays[a].push(...temp_shape.arrays[a].map(p => points_transform.times(p.to4(1)).to3()));
// Do the same for normals, but use the inverse transpose matrix as math requires:
else if (a == "normal")
recipient.arrays[a].push(...temp_shape.arrays[a].map(n =>
Mat4.inverse(points_transform.transposed()).times(n.to4(1)).to3()));
// All other arrays get copied in unmodified:
else recipient.arrays[a].push(...temp_shape.arrays[a]);
}
}
make_flat_shaded_version() {
// make_flat_shaded_version(): Auto-generate a new class that re-uses any
// Shape's points, but with new normals generated from flat shading.
return class extends this.constructor {
constructor(...args) {
super(...args);
this.duplicate_the_shared_vertices();
this.flat_shade();
}
}
}
duplicate_the_shared_vertices() {
// (Internal helper function)
// Prepare an indexed shape for flat shading if it is not ready -- that is, if there are any
// edges where the same vertices are indexed by both the adjacent triangles, and those two
// triangles are not co-planar. The two would therefore fight over assigning different normal
// vectors to the shared vertices.
const arrays = {};
for (let arr in this.arrays) arrays[arr] = [];
for (let index of this.indices)
for (let arr in this.arrays)
arrays[arr].push(this.arrays[arr][index]);
// Make re-arranged versions of each data field, with
Object.assign(this.arrays, arrays);
// copied values every time an index was formerly re-used.
this.indices = this.indices.map((x, i) => i);
// Without shared vertices, we can use sequential numbering.
}
flat_shade() {
// (Internal helper function)
// Automatically assign the correct normals to each triangular element to achieve flat shading.
// Affect all recently added triangles (those past "offset" in the list). Assumes that no
// vertices are shared across seams. First, iterate through the index or position triples:
for (let counter = 0; counter < (this.indices ? this.indices.length : this.arrays.position.length); counter += 3) {
const indices = this.indices.length ? [this.indices[counter], this.indices[counter + 1], this.indices[counter + 2]]
: [counter, counter + 1, counter + 2];
const [p1, p2, p3] = indices.map(i => this.arrays.position[i]);
// Cross the two edge vectors of this triangle together to get its normal:
const n1 = p1.minus(p2).cross(p3.minus(p1)).normalized();
// Flip the normal if adding it to the triangle brings it closer to the origin:
if (n1.times(.1).plus(p1).norm() < p1.norm()) n1.scale_by(-1);
// Propagate this normal to the 3 vertices:
for (let i of indices) this.arrays.normal[i] = Vector3.from(n1);
}
}
normalize_positions(keep_aspect_ratios = true) {
let p_arr = this.arrays.position;
const average_position = p_arr.reduce((acc, p) => acc.plus(p.times(1 / p_arr.length)),
vec3(0, 0, 0));
p_arr = p_arr.map(p => p.minus(average_position)); // Center the point cloud on the origin.
const average_lengths = p_arr.reduce((acc, p) =>
acc.plus(p.map(x => Math.abs(x)).times(1 / p_arr.length)), vec3(0, 0, 0));
if (keep_aspect_ratios) {
// Divide each axis by its average distance from the origin.
this.arrays.position = p_arr.map(p => p.map((x, i) => x / average_lengths[i]));
} else
this.arrays.position = p_arr.map(p => p.times(1 / average_lengths.norm()));
}
}
const Light = tiny.Light =
class Light {
// **Light** stores the properties of one light in a scene. Contains a coordinate and a
// color (each are 4x1 Vectors) as well as one size scalar.
// The coordinate is homogeneous, and so is either a point or a vector. Use w=0 for a
// vector (directional) light, and w=1 for a point light / spotlight.
// For spotlights, a light also needs a "size" factor for how quickly the brightness
// should attenuate (reduce) as distance from the spotlight increases.
constructor(position, color, size) {
Object.assign(this, {position, color, attenuation: 1 / size});
}
}
const Graphics_Addresses = tiny.Graphics_Addresses =
class Graphics_Addresses {
// **Graphics_Addresses** is used internally in Shaders for organizing communication with the GPU.
// Once we've compiled the Shader, we can query some things about the compiled program, such as
// the memory addresses it will use for uniform variables, and the types and indices of its per-
// vertex attributes. We'll need those for building vertex buffers.
constructor(program, gl) {
const num_uniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < num_uniforms; ++i) {
// Retrieve the GPU addresses of each uniform variable in the shader
// based on their names, and store these pointers for later.
let u = gl.getActiveUniform(program, i).name.split('[')[0];
this[u] = gl.getUniformLocation(program, u);
}
this.shader_attributes = {};
// Assume per-vertex attributes will each be a set of 1 to 4 floats:
const type_to_size_mapping = {0x1406: 1, 0x8B50: 2, 0x8B51: 3, 0x8B52: 4};
const numAttribs = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i = 0; i < numAttribs; i++) {
// https://github.com/greggman/twgl.js/blob/master/dist/twgl-full.js for another example:
const attribInfo = gl.getActiveAttrib(program, i);
// Pointers to all shader attribute variables:
this.shader_attributes[attribInfo.name] = {
index: gl.getAttribLocation(program, attribInfo.name),