-
Notifications
You must be signed in to change notification settings - Fork 0
/
rayphoton.js
2254 lines (2022 loc) · 95.8 KB
/
rayphoton.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
let debug = false;
let warnings = false;
const DEG_TO_RAD = Math.PI / 180;
const EPSILON = 1e-6;
const LITTLE_SPACE = 1e-3; // let's leave room between things, e.g., don't put them right on the floor. Used automatically in object constructors, not primitives: e.g., use Ball instead of Sphere
const MAX_TRACE_DIST = 20;
const MAX_DEPTH = 5;
const SUB_SAMPLE = 6; // split each pixel into virtual SUB_SAMPLE × SUB_SAMPLE grid, then average results.
const LIGHT_PATHS_PER_SOURCE_PER_RAY = 1; // normally keep this around 1 since it is redundant with subsampling. Shoot this many paths towards light sources for direct lighting; in practice it doesn't offer real benefits, just slows down
const SUPER_SAMPLE_BASE = 3; // to get approximate picture at first, but total rendering time is around SSB / (SSB - 1) times longer than if we just started at 1:1 size
const SUPER_SAMPLE_LEVELS = 10; // so first draw at scale of SSB ** SSL : 1, then SSB ** (SSL - 1) : 1, ..., 1 : 1
const NUM_PHOTONS_DIFFUSE = 20000;
const NUM_PHOTONS_CAUSTIC = 50000;
const CAUSTIC_RADIUS = 0.15;
const DIFFUSE_RADIUS = 0.40;
const CAUSTIC_AREA = Math.PI * CAUSTIC_RADIUS ** 2;
const DIFFUSE_AREA = Math.PI * DIFFUSE_RADIUS ** 2;
const STANDARD_LAMP_AREA = 100; // magic number to scale brightness of lamps. 40-watt spotlight of radius 1 is nice.
// --------------------------------
// colours
// --------------------------------
const COL_BLACK = { r: 0, g: 0, b: 0, a: 0 };
const COL_WHITE = { r: 255, g: 255, b: 255, a: 1 };
const COL_DARK_GREY = { r: 64, g: 64, b: 64, a: 1 };
const COL_VERY_DARK_GREY = { r: 16, g: 16, b: 16, a: 1 };
const COL_GREY = { r: 128, g: 128, b: 128, a: 1 };
const COL_SILVER = { r: 192, g: 192, b: 192, a: 1 };
const COL_FIRE_ENGINE_RED = { r: 200, g: 10, b: 10, a: 1 };
const COL_AMETHYST = { r: 150, g: 100, b: 200, a: 1 };
const COL_DEEP_BLUE = { r: 8, g: 8, b: 64, a: 1 };
const COL_SKY_BLUE = { r: 128, g: 128, b: 224, a: 1 };
const COL_WARM_GREY = { r: 144, g: 128, b: 128, a: 1 };
const COL_ORANGE_ORANGE = { r: 224, g: 124, b: 32 };
const COL_GRAPEFRUIT_YELLOW = { r: 248, g: 210, b: 112 };
const COL_SCHOOL_BUS_YELLOW = { r: 255, g: 216, b: 1 };
const COL_LIME_GREEN = { r: 112, g: 160, b: 1, a: 1 };
const COL_ROBINS_EGG_BLUE = { r: 1, g: 180, b: 180, a: 1 };
const COL_DEEP_PINK = { r: 255, g: 32, b: 144, a: 1 };
const COL_RAW_UMBER = { r: 112, g: 68, b: 17, a: 1 };
const COL_ENGLISH_WALNUT = { r: 68, g: 48, b: 40, a: 1 };
const COL_VANILLA = { r: 243, g: 229, b: 171, a: 1 };
const COL_CHOCOLATE = { r: 32, g: 16, b: 8, a: 1 };
// --------------------------------
// materials
// --------------------------------
const MAT_AIR = 0;
const MAT_GLASS = 1;
const MAT_WATER = 2;
const MAT_MIRROR = 3;
const MAT_COPPER = 4;
const MAT_LINOLEUM = 5;
const MAT_PLASTER = 6;
const MAT_SPECTRALON = 7;
// --------------------------------
// materials: transparency (i.e., use refraction or not)
// --------------------------------
const matTransparent = [];
matTransparent[MAT_AIR] = true;
matTransparent[MAT_GLASS] = true;
matTransparent[MAT_WATER] = true;
matTransparent[MAT_MIRROR] = false;
matTransparent[MAT_COPPER] = false;
matTransparent[MAT_LINOLEUM] = false;
matTransparent[MAT_PLASTER] = false;
matTransparent[MAT_SPECTRALON] = false;
// --------------------------------
// materials: indices of refraction
// --------------------------------
const matRefrIndex = [];
matRefrIndex[MAT_AIR] = 1.0;
matRefrIndex[MAT_GLASS] = 1.5;
matRefrIndex[MAT_WATER] = 1.33;
matRefrIndex[MAT_COPPER] = 0.64;
// --------------------------------
// materials: reflectance for opaque materials
// --------------------------------
const matReflectance = [];
matReflectance[MAT_MIRROR] = 1;
matReflectance[MAT_COPPER] = 0.7;
matReflectance[MAT_LINOLEUM] = 0.6;
matReflectance[MAT_PLASTER] = 0.2;
matReflectance[MAT_SPECTRALON] = 1;
// --------------------------------
// materials: specular (as fraction of reflectance; remainder is Lambertian, i.e., diffuse)
// --------------------------------
// Lambertian diffusion: choose random vector in hemisphere around normal
// to do: add glossy diffusion, i.e., perturb reflected angle by some amount (depending on material)
const matSpecular = [];
matSpecular[MAT_MIRROR] = 1;
matSpecular[MAT_COPPER] = 0.5;
matSpecular[MAT_LINOLEUM] = 0.2;
matSpecular[MAT_PLASTER] = 0;
matSpecular[MAT_SPECTRALON] = 0;
function degToRad(theta) { return theta * DEG_TO_RAD; }
function vecPlus(v, w) { return [v[0] + w[0], v[1] + w[1], v[2] + w[2]]; }
function vecMinus(v, w) { return [v[0] - w[0], v[1] - w[1], v[2] - w[2]]; }
function vecScalar(k, v) { return [k * v[0], k * v[1], k * v[2]]; }
function vecDot(v, w) { return v[0] * w[0] + v[1] * w[1] + v[2] * w[2]; }
function vecCross(v, w) { return [v[1] * w[2] - v[2] * w[1], v[2] * w[0] - v[0] * w[2], v[0] * w[1] - v[1] * w[0]]; }
function vecIsZero(v) { return vecSqLength(v) < EPSILON; }
function vecNormalize(v) { return vecIsZero(v) ? [0, 0, 1] : vecScalar(1 / vecLength(v), v); }
function vecSqLength(v) { return vecDot(v, v); }
function vecLength(v) { return Math.sqrt(vecSqLength(v)); }
function vecOrthonormal(v) { // given v, return mutually orthogonal triple [v2, m, n] with ||v2|| = ||m|| = ||n|| = 1, and v2 in same direction as v
let v2 = vecNormalize(v);
let m = [1, 0, 0]; // pick any m not parallel to v, use it to make n, then redefine m
if (vecIsZero(vecCross(v2, m))) {
m = [0, 1, 0];
}
let n = vecNormalize(vecCross(v2, m));
m = vecCross(n, v2);
return [v2, m, n];
}
function vecPerturb(v, maxDeviation) { // create random vector chosen around v with angle less than deviation
let lowerBound = 0;
if (maxDeviation != undefined) {
lowerBound = Math.cos(maxDeviation) ** 2;
}
let [_, m, n] = vecOrthonormal(v);
let x = (1 - lowerBound) * Math.random() + lowerBound;
let cosTheta = Math.sqrt(x);
let sinTheta = Math.sqrt(1 - cosTheta * cosTheta);
let phi = 2 * Math.PI * Math.random();
return vecPlus(vecPlus(vecScalar(cosTheta, v), vecScalar(sinTheta * Math.cos(phi), m)), vecScalar(sinTheta * Math.sin(phi), n));
}
function randomPointOnSphere() {
let x, y, z;
do {
x = 2 * Math.random() - 1;
y = 2 * Math.random() - 1;
z = 2 * Math.random() - 1;
} while (x ** x + y ** y + z ** z > 1);
return vecNormalize([x, y, z]);
}
function randomONB() { // returns a random orthonormal basis
let u = randomPointOnSphere();
let vTemp, w;
do {
vTemp = randomPointOnSphere();
w = vecCross(u, vTemp)
} while (vecIsZero(w)); // extremely unlikely that this would happen (u and vTemp would be parallel random vectors) but check anyway
w = vecNormalize(w);
let v = vecCross(w, u);
return [u, v, w];
}
function clamp(x, min, max) { return (x < min) ? min : ((x > max) ? max : x); }
function colour(col) { return { r: clamp(col.r, 0, 255), g: clamp(col.g, 0, 255), b: clamp(col.b, 0, 255), a: clamp(col.a, 0, 1) } };
function randomSaturatedColour() {
let colHue = 360 * Math.random();
let c = Math.floor(256 * (1 - Math.abs((colHue / 60) % 2 - 1)));
switch (Math.floor(colHue / 60)) {
case 0:
return { r: 255, g: c, b: 0, a: 1 };
case 1:
return { r: c, g: 255, b: 0, a: 1 };
case 2:
return { r: 0, g: 255, b: c, a: 1 };
case 3:
return { r: 0, g: c, b: 255, a: 1 };
case 4:
return { r: c, g: 0, b: 255, a: 1 };
case 5:
return { r: 255, g: 0, b: c, a: 1 };
default:
throw new Error(`impossible HSV?`);
break;
}
}
class Box { // actually a parallelepiped
constructor(shapes, vtxA, edgeAB, edgeAC, edgeAD, baseColour, material, nudge) {
if (vecDot(edgeAB, vecCross(edgeAC, edgeAD)) < 0) {
console.log(`Looks like your box is inside out.`)
}
nudge = (nudge == undefined || nudge) ? 1 : 0;
let adjVtxA = vecPlus(vecPlus(vecPlus(vtxA, vecScalar(nudge * LITTLE_SPACE, vecNormalize(edgeAB))),
vecScalar(nudge * LITTLE_SPACE, vecNormalize(edgeAC))),
vecScalar(nudge * LITTLE_SPACE, vecNormalize(edgeAD)));
let adjEdgeAB = vecMinus(edgeAB, vecScalar(2 * nudge * LITTLE_SPACE, vecNormalize(edgeAB)));
let adjEdgeAC = vecMinus(edgeAC, vecScalar(2 * nudge * LITTLE_SPACE, vecNormalize(edgeAC)));
let adjEdgeAD = vecMinus(edgeAD, vecScalar(2 * nudge * LITTLE_SPACE, vecNormalize(edgeAD)));
let oppVtx = vecPlus(vecPlus(vecPlus(adjVtxA, adjEdgeAB), adjEdgeAC), adjEdgeAD);
let adjEdgeBA = vecScalar(-1, adjEdgeAB);
let adjEdgeCA = vecScalar(-1, adjEdgeAC);
let adjEdgeDA = vecScalar(-1, adjEdgeAD);
shapes.push(new Square(adjVtxA, adjEdgeAC, adjEdgeAB, baseColour, material));
shapes.push(new Square(adjVtxA, adjEdgeAD, adjEdgeAC, baseColour, material));
shapes.push(new Square(adjVtxA, adjEdgeAB, adjEdgeAD, baseColour, material));
shapes.push(new Square(oppVtx, adjEdgeBA, adjEdgeCA, baseColour, material));
shapes.push(new Square(oppVtx, adjEdgeCA, adjEdgeDA, baseColour, material));
shapes.push(new Square(oppVtx, adjEdgeDA, adjEdgeBA, baseColour, material));
}
}
class Prism { // triangular prism: ABC is triangle; square base in ABD plane
constructor(shapes, vtxA, edgeAB, edgeAC, edgeAD, baseColour, material) {
let adjVtxA = vecPlus(vecPlus(vecPlus(vtxA, vecScalar(LITTLE_SPACE, vecNormalize(edgeAB))),
vecScalar(LITTLE_SPACE, vecNormalize(edgeAC))),
vecScalar(LITTLE_SPACE, vecNormalize(edgeAD)));
let adjEdgeAB = vecMinus(edgeAB, vecScalar(2 * LITTLE_SPACE, vecNormalize(edgeAB)));
let adjEdgeAC = vecMinus(edgeAC, vecScalar(2 * LITTLE_SPACE, vecNormalize(edgeAC)));
let adjEdgeAD = vecMinus(edgeAD, vecScalar(2 * LITTLE_SPACE, vecNormalize(edgeAD)));
let oppVtx = vecPlus(vecPlus(adjVtxA, adjEdgeAC), adjEdgeAD);
let adjEdgeCA = vecScalar(-1, adjEdgeAC);
let adjEdgeCB = vecPlus(adjEdgeCA, adjEdgeAB);
let adjEdgeDA = vecScalar(-1, adjEdgeAD);
shapes.push(new Triangle(adjVtxA, adjEdgeAC, adjEdgeAB, baseColour, material));
shapes.push(new Square(adjVtxA, adjEdgeAD, adjEdgeAC, baseColour, material));
shapes.push(new Square(adjVtxA, adjEdgeAB, adjEdgeAD, baseColour, material));
shapes.push(new Triangle(oppVtx, adjEdgeCA, adjEdgeCB, baseColour, material));
shapes.push(new Square(oppVtx, adjEdgeCB, adjEdgeDA, baseColour, material));
}
}
class Cuboctahedron { // start with cube, cut off corners (which is why A is called chopped vertex; B, C, D are also chopped)
constructor(shapes, choppedVtxA, edgeAB, edgeAC, edgeAD, baseColourSquare, baseColourTriangle, material) {
let adjVtxA = vecPlus(vecPlus(vecPlus(choppedVtxA, vecScalar(LITTLE_SPACE, vecNormalize(edgeAB))),
vecScalar(LITTLE_SPACE, vecNormalize(edgeAC))),
vecScalar(LITTLE_SPACE, vecNormalize(edgeAD)));
// create 12 vertices: combinations of up/down, nesw; A is down south west.
let ds = vecPlus(adjVtxA, vecScalar(0.5 - LITTLE_SPACE / vecLength(edgeAB), edgeAB));
let dw = vecPlus(adjVtxA, vecScalar(0.5 - LITTLE_SPACE / vecLength(edgeAC), edgeAC));
let dn = vecPlus(ds, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAC), edgeAC));
let de = vecPlus(dw, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAB), edgeAB));
let sw = vecPlus(adjVtxA, vecScalar(0.5 - LITTLE_SPACE / vecLength(edgeAD), edgeAD));
let nw = vecPlus(sw, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAC), edgeAC));
let ne = vecPlus(nw, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAB), edgeAB));
let se = vecPlus(sw, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAB), edgeAB));
let un = vecPlus(dn, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAD), edgeAD));
let ue = vecPlus(de, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAD), edgeAD));
let us = vecPlus(ds, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAD), edgeAD));
let uw = vecPlus(dw, vecScalar(1 - 2 * LITTLE_SPACE / vecLength(edgeAD), edgeAD));
shapes.push(new Square(ds, vecMinus(dw, ds), vecMinus(de, ds), baseColourSquare, material));
shapes.push(new Square(ne, vecMinus(dn, ne), vecMinus(un, ne), baseColourSquare, material));
shapes.push(new Square(se, vecMinus(de, se), vecMinus(ue, se), baseColourSquare, material));
shapes.push(new Square(sw, vecMinus(ds, sw), vecMinus(us, sw), baseColourSquare, material));
shapes.push(new Square(nw, vecMinus(dw, nw), vecMinus(uw, nw), baseColourSquare, material));
shapes.push(new Square(us, vecMinus(ue, us), vecMinus(uw, us), baseColourSquare, material));
shapes.push(new Triangle(dn, vecMinus(ne, dn), vecMinus(de, dn), baseColourTriangle, material));
shapes.push(new Triangle(de, vecMinus(se, de), vecMinus(ds, de), baseColourTriangle, material));
shapes.push(new Triangle(ds, vecMinus(sw, ds), vecMinus(dw, ds), baseColourTriangle, material));
shapes.push(new Triangle(dw, vecMinus(nw, dw), vecMinus(dn, dw), baseColourTriangle, material));
shapes.push(new Triangle(un, vecMinus(ue, un), vecMinus(ne, un), baseColourTriangle, material));
shapes.push(new Triangle(ue, vecMinus(us, ue), vecMinus(se, ue), baseColourTriangle, material));
shapes.push(new Triangle(us, vecMinus(uw, us), vecMinus(sw, us), baseColourTriangle, material));
shapes.push(new Triangle(uw, vecMinus(un, uw), vecMinus(nw, uw), baseColourTriangle, material));
}
}
class Ball { // normally use this instead of Sphere (to leave a LITTLE_SPACE)
constructor(shapes, centre, radius, baseColour, material) {
shapes.push(new Sphere(centre, radius - LITTLE_SPACE, baseColour, material));
}
}
class Can {
constructor(shapes, centre, axis, radius, halfHeight, baseColourAround, baseColourCaps, material) {
shapes.push(new Cylinder(centre, axis, radius - LITTLE_SPACE, halfHeight - LITTLE_SPACE, true, baseColourAround, material));
shapes.push(new Disc(vecPlus(centre, vecScalar(halfHeight - LITTLE_SPACE, vecNormalize(axis))), radius - LITTLE_SPACE, axis, baseColourCaps, material));
shapes.push(new Disc(vecPlus(centre, vecScalar(-(halfHeight - LITTLE_SPACE), vecNormalize(axis))), radius - LITTLE_SPACE, vecScalar(-1, axis), baseColourCaps, material));
}
}
class Tube {
constructor(shapes, centre, axis, outerRadius, innerRadius, halfHeight, baseColourAround, baseColourCaps, material) {
shapes.push(new Cylinder(centre, axis, outerRadius - LITTLE_SPACE, halfHeight - LITTLE_SPACE, true, baseColourAround, material));
shapes.push(new Cylinder(centre, axis, innerRadius + LITTLE_SPACE, halfHeight - LITTLE_SPACE, false, baseColourAround, material));
shapes.push(new Annulus(vecPlus(centre, vecScalar(halfHeight - LITTLE_SPACE, vecNormalize(axis))), outerRadius - LITTLE_SPACE, innerRadius + LITTLE_SPACE, axis, baseColourCaps, material));
shapes.push(new Annulus(vecPlus(centre, vecScalar(-(halfHeight - LITTLE_SPACE), vecNormalize(axis))), outerRadius - LITTLE_SPACE, innerRadius + LITTLE_SPACE, vecScalar(-1, axis), baseColourCaps, material));
}
}
class Halfball { // normalDir points away from hemisphere direction (so disc is on top)
constructor(shapes, centre, radius, normalDir, truncateMin, truncateMax, baseColour, material) {
let adjTrMin = (truncateMin == undefined) ? LITTLE_SPACE : truncateMin + LITTLE_SPACE;
let adjTrMax = (truncateMax == undefined) ? undefined : truncateMax - LITTLE_SPACE;
shapes.push(new Hemisphere(centre, radius - LITTLE_SPACE, vecScalar(-1, normalDir), adjTrMin, adjTrMax, true, baseColour, material));
shapes.push(new Disc(vecPlus(centre, vecScalar(-1 * adjTrMin, vecNormalize(normalDir))), Math.sqrt((radius - LITTLE_SPACE) ** 2 - adjTrMin ** 2), normalDir, baseColour, material));
if (truncateMax < radius) { // possibly add cap on other end
shapes.push(new Disc(vecPlus(centre, vecScalar(-1 * adjTrMax, vecNormalize(normalDir))), Math.sqrt((radius - LITTLE_SPACE) ** 2 - adjTrMax ** 2), vecScalar(-1, normalDir), baseColour, material));
}
}
}
class Bowl { // normalDir points in direction of rim
// to do: add truncate; see Halfball
constructor(shapes, centre, outerRadius, innerRadius, normalDir, baseColour, material) {
shapes.push(new Hemisphere(centre, outerRadius - LITTLE_SPACE, vecScalar(-1, normalDir), 0, undefined, true, baseColour, material));
shapes.push(new Hemisphere(centre, innerRadius + LITTLE_SPACE, vecScalar(-1, normalDir), 0, undefined, false, baseColour, material));
shapes.push(new Annulus(centre, outerRadius - LITTLE_SPACE, innerRadius + LITTLE_SPACE, normalDir, baseColour, material));
}
}
class Spotlight {
// to do: * add other kinds of lamps
constructor(shapes, lights, centre, radius, dir, wattage, colour) {
new Bowl(shapes, centre, 1.3 * radius, 1.125 * radius, dir, COL_RAW_UMBER, MAT_COPPER); // 1.125 is slightly bigger than sqrt(5)/2, to make sure that light (Which is set back by 0.5 * radius) fits inside
let s = shapes.push(new Disc(vecPlus(centre, vecScalar(-0.5 * radius, vecNormalize(dir))), radius, dir));
//let s = shapes.push(new Disc(vecPlus(centre, vecScalar(-0.25 * radius, vecNormalize(dir))), radius, dir));
lights.push(s - 1); // store index of light shape so that we can easily find all lights later
shapes[s - 1].isLight = true;
shapes[s - 1].wattage = wattage;
shapes[s - 1].lightColour = colour || COL_WHITE;
shapes[s - 1].area = Math.PI * radius ** 2;
}
}
// --------------------------------
// primitives
// objects in Shape class are exact
// (i.e., they don't use LITTLE_SPACE)
// --------------------------------
class Shape {
constructor() {
this.baseColour = COL_DARK_GREY;
}
set material(mat) {
this._material = mat;
this.transparent = matTransparent[mat];
this.refrIndex = matRefrIndex[mat];
this.reflectance = matReflectance[mat];
this.specular = matSpecular[mat];
this.isLight = false;
}
get material() {
console.log(`don't try to access an object's materials; instead access its properties (reflectance, etc.)`)
}
colour() { return this.baseColour; }
}
class Cylinder extends Shape {
// to do: make Cup class (cylinder with 1 cap)
constructor(centre, axis, radius, halfHeight, convex, baseColour, material) {
super();
this.type = 'cylinder';
this.centre = centre;
this.axis = vecNormalize(axis);
this.radius = radius;
this.halfHeight = halfHeight;
this.convex = convex; // if true, surface points away from centre
this.baseColour = baseColour || COL_WHITE;
this.material = material;
}
normal(p) {
let v = vecMinus(p, this.centre);
return vecScalar((this.convex ? 1 : -1) / this.radius, vecMinus(v, vecScalar(vecDot(v, this.axis), this.axis)));
}
}
class Sphere extends Shape {
constructor(centre, radius, baseColour, material) {
super();
this.type = 'sphere';
this.centre = centre;
this.radius = radius;
this.baseColour = baseColour || COL_FIRE_ENGINE_RED;
this.material = material;
}
normal(p) { return vecScalar(1 / this.radius, vecMinus(p, this.centre)); }
}
class Hemisphere extends Shape { // normalDir points towards half that exists; truncate is minimum distance along normal
constructor(centre, radius, normalDir, truncateMin, truncateMax, convex, baseColour, material) {
super();
this.type = 'hemisphere';
this.centre = centre;
this.radius = radius;
this.normalDir = vecNormalize(normalDir);
this.truncateMin = truncateMin;
this.truncateMax = truncateMax;
this.convex = convex; // if true, surface points away from centre
this.baseColour = baseColour || COL_FIRE_ENGINE_RED;
this.material = material;
}
normal(p) { return vecScalar((this.convex ? 1 : -1) / this.radius, vecMinus(p, this.centre)); }
}
class Plane extends Shape {
constructor(origin, normalDir, baseColour, material) {
super();
this.type = 'plane';
this.origin = origin;
this.normalDir = vecNormalize(normalDir);
this.baseColour = baseColour || COL_DEEP_BLUE;
this.material = material;
}
normal() { return this.normalDir; }
}
class Triangle extends Shape {
constructor(vtxA, edgeAB, edgeAC, baseColour, material) {
super();
this.type = 'triangle';
this.vtxA = vtxA;
this.edgeAB = edgeAB;
this.edgeAC = edgeAC;
this.normalDir = vecNormalize(vecCross(this.edgeAB, this.edgeAC));
this.baseColour = baseColour || COL_LIME_GREEN;
this.material = material;
}
normal() { return this.normalDir; }
}
class Square extends Shape { // actually a parallelogram
constructor(vtxA, edgeAB, edgeAC, baseColour, material) {
super();
this.type = 'square';
this.vtxA = vtxA;
this.edgeAB = edgeAB;
this.edgeAC = edgeAC;
this.normalDir = vecNormalize(vecCross(this.edgeAB, this.edgeAC));
this.baseColour = baseColour || COL_DEEP_PINK;
this.material = material;
}
normal() { return this.normalDir; }
}
class Disc extends Shape {
constructor(centre, radius, normalDir, baseColour, material) {
super();
this.type = 'disc';
this.centre = centre;
this.radius = radius;
this.normalDir = vecNormalize(normalDir);
this.baseColour = baseColour || COL_DEEP_PINK;
this.material = material;
}
normal() { return this.normalDir; }
}
class Annulus extends Shape {
constructor(centre, outerRadius, innerRadius, normalDir, baseColour, material) {
super();
this.type = 'annulus';
this.centre = centre;
this.outerRadius = outerRadius;
this.innerRadius = innerRadius;
this.normalDir = vecNormalize(normalDir);
this.baseColour = baseColour || COL_DEEP_PINK;
this.material = material;
}
normal() { return this.normalDir; }
}
class Ray {
constructor(origin, dir) {
this.origin = origin;
this.dir = vecNormalize(dir);
}
// to do: try moving this out of class to see if it speeds things up
intersectDist(shape) {
switch (shape.type) {
case 'plane': {
let a = shape.origin;
let n = shape.normalDir;
if (Math.abs(vecDot(this.dir, n)) < EPSILON) {
return undefined;
}
let t = vecDot(vecMinus(a, this.origin), n) / vecDot(this.dir, n);
return t > EPSILON ? t : undefined;
} case 'sphere': {
let a = 1;
let halfB = vecDot(this.dir, vecMinus(this.origin, shape.centre));
let c = vecSqLength(vecMinus(this.origin, shape.centre)) - shape.radius * shape.radius;
let t = qRoots(a, halfB, c);
if (t == undefined) {
return undefined;
} else {
if (t[0] > EPSILON) {
return t[0];
} else {
if (t[1] > EPSILON) {
return t[1];
}
}
}
return undefined;
} case 'hemisphere': {
let a = 1;
let halfB = vecDot(this.dir, vecMinus(this.origin, shape.centre));
let c = vecSqLength(vecMinus(this.origin, shape.centre)) - shape.radius * shape.radius;
let t = qRoots(a, halfB, c);
if (t == undefined) {
return undefined;
} else {
for (let i in [0, 1]) { // check intersections to see whether they're in positive direction along ray and in the proper halfspace (at distance within min / max truncation)
if (t[i] > EPSILON) {
let pos = vecPlus(this.origin, vecScalar(t[i], this.dir));
let proj = vecDot(vecMinus(pos, shape.centre), shape.normalDir);
if (proj > shape.truncateMin && (shape.truncateMax == undefined || proj < shape.truncateMax)) {
return t[i];
}
}
}
return undefined;
}
} case 'cylinder': {
let centre = shape.centre;
let axis = shape.axis;
let h = shape.halfHeight;
let v = vecMinus(this.origin, centre);
let vd = vecDot(v, this.dir);
let va = vecDot(v, axis);
let da = vecDot(this.dir, axis);
let a = 1 - da * da;
let halfB = vd - va * da;
let c = vecSqLength(v) - va * va - shape.radius * shape.radius;
let t = qRoots(a, halfB, c);
if (t == undefined) {
return undefined;
} else {
for (let i in [0, 1]) { // check intersections to see whether they're in positive direction along ray and in the proper halfspace (at distance within min / max truncation)
if (t[i] > EPSILON) {
if (h == undefined) { return t[i]; } // cylinder is infinite; no need to check height
let pos = vecPlus(this.origin, vecScalar(t[i], this.dir));
if (Math.abs(vecDot(vecMinus(pos, centre), axis)) < h - EPSILON) { return t[i]; } // hits within height of cylinder
}
}
return undefined;
}
} case 'triangle': {
// Möller-Trumbore algorithm
let h = vecCross(this.dir, shape.edgeAC);
let a = vecDot(shape.edgeAB, h);
if (a > -EPSILON && a < EPSILON) {
return undefined;
}
let f = 1 / a;
let s = vecMinus(this.origin, shape.vtxA);
let u = f * vecDot(s, h);
if (u < 0 || u > 1) {
return undefined;
}
let q = vecCross(s, shape.edgeAB);
let v = f * vecDot(this.dir, q);
if (v < 0 || u + v > 1) {
return undefined;
}
let t = f * vecDot(shape.edgeAC, q);
return (t > EPSILON) ? t : undefined;
} case 'square': {
// Möller-Trumbore algorithm
let h = vecCross(this.dir, shape.edgeAC);
let a = vecDot(shape.edgeAB, h);
if (a > -EPSILON && a < EPSILON) {
return undefined;
}
let f = 1 / a;
let s = vecMinus(this.origin, shape.vtxA);
let u = f * vecDot(s, h);
if (u < 0 || u > 1) {
return undefined;
}
let q = vecCross(s, shape.edgeAB);
let v = f * vecDot(this.dir, q);
if (v < 0 || v > 1) {
return undefined;
}
let t = f * vecDot(shape.edgeAC, q);
return (t > EPSILON) ? t : undefined;
} case 'disc': {
let c = shape.centre;
let n = shape.normalDir;
if (Math.abs(vecDot(this.dir, n)) < EPSILON) {
return undefined;
}
let t = vecDot(vecMinus(c, this.origin), n) / vecDot(this.dir, n);
if (t > EPSILON) { // hits plane of disc; now check radius
let pos = vecPlus(this.origin, vecScalar(t, this.dir));
return (vecSqLength(vecMinus(c, pos)) + EPSILON < shape.radius * shape.radius) ? t : undefined;
}
return undefined;
} case 'annulus': {
let c = shape.centre;
let n = shape.normalDir;
if (Math.abs(vecDot(this.dir, n)) < EPSILON) {
return undefined;
}
let t = vecDot(vecMinus(c, this.origin), n) / vecDot(this.dir, n);
if (t > EPSILON) { // hits plane of disc; now check radii
let pos = vecPlus(this.origin, vecScalar(t, this.dir));
let rSq = vecSqLength(vecMinus(c, pos));
return (rSq + EPSILON < shape.outerRadius * shape.outerRadius && rSq - EPSILON > shape.innerRadius * shape.innerRadius) ? t : undefined;
}
return undefined;
} default: {
throw new Error(`I didn't recognize the shape! (${shape.type})`);
return undefined;
}
}
}
}
class Photon extends Ray {
constructor(origin, dir, power, colour, isCaustic) {
super(origin, dir);
this.power = power;
this.colour = colour || COL_WHITE;
this.isCaustic = isCaustic; // photons are caustic if the are of the form LS+D (lightsource-specular-...-specular-diffuse)
}
}
class Camera {
/*constructor(origin, gazeDir, up, width, height, fieldOfView) {
this.origin = origin;
this.width = width;
this.height = height;
this.fieldOfView = fieldOfView || 60;
this.fovRadians = degToRad(this.fieldOfView / 2);
this.fovScaleWidth = Math.tan(this.fovRadians);
this.fovScaleHeight = this.fovScaleWidth * this.height / this.width;
// find orthonormal basis corresponding to camera angle
this.up = up;
this.ONBw = vecNormalize(vecScalar(-1, gazeDir));
this.ONBu = vecNormalize(vecCross(this.up, this.ONBw));
this.ONBv = vecCross(this.ONBw, this.ONBu);
}*/
constructor(origin, gazeTheta, gazePhi, width, height, fieldOfView) { // theta and phi are in degrees
this.origin = origin;
this.width = width;
this.height = height;
this.gazeTheta = gazeTheta;
this.gazePhi = gazePhi;
this.fieldOfView = fieldOfView || 60;
}
set gazeTheta(theta) {
this._gazeTheta = theta;
this._gazePhi = this._gazePhi || 0;
this.findOrthonormalBasis();
}
set gazePhi(phi) {
this._gazeTheta = this._gazeTheta || 0;
this._gazePhi = phi;
this.findOrthonormalBasis();
}
set fieldOfView(fov) {
this._fieldOfView = fov;
this.fovRadians = degToRad(this._fieldOfView / 2);
this.fovScaleWidth = Math.tan(this.fovRadians);
this.fovScaleHeight = this.fovScaleWidth * this.height / this.width;
}
findOrthonormalBasis() { // find orthonormal basis corresponding to camera angle
let theta = degToRad(this._gazeTheta);
let phi = degToRad(this._gazePhi);
let up = [0, 0, 1];
this.gazeDir = vecNormalize([Math.cos(theta) * Math.cos(phi), Math.sin(theta) * Math.cos(phi), Math.sin(phi)]);
this.ONBw = vecScalar(-1, this.gazeDir);
this.ONBu = vecNormalize(vecCross(up, this.ONBw));
this.ONBv = vecCross(this.ONBw, this.ONBu);
}
toUVW(xyz) {
let transXyz = vecMinus(xyz, this.origin);
return [vecDot(transXyz, this.ONBu), vecDot(transXyz, this.ONBv), vecDot(transXyz, this.ONBw)];
}
toXYZ(uvw) { return vecPlus(vecPlus(vecPlus(this.origin, vecScalar(uvw[0], this.ONBu)), vecScalar(uvw[1], this.ONBv)), vecScalar(uvw[2], this.ONBw)) };
}
class Scene {
constructor(ctx) {
this.ctx = ctx;
this.canvasWidth = ctx.width;
this.canvasHeight = ctx.height;
this.shapes = [];
this.lights = [];
this.photonListDiffuse = [];
this.photonListCaustic = [];
this.photonListBad = []; // for debugging
this.photonKdTDiffuse = [];
this.photonKdTCaustic = [];
}
loadPreset(def) {
switch (def) {
case 0:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_WHITE, MAT_LINOLEUM),
new Plane([0, 80, 0], [0, -1, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([0, -80, 0], [0, 1, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([80, 0, 0], [-1, 0, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([-80, 0, 0], [1, 0, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([0, 0, 80], [1, 0, -1], COL_VERY_DARK_GREY, MAT_PLASTER),
];
new Ball(this.shapes, [0.3, 0, 0.4], 0.4, COL_FIRE_ENGINE_RED, MAT_GLASS);
new Ball(this.shapes, [-1.1, 1.2, 0.25], 0.25, COL_AMETHYST, MAT_PLASTER);
new Ball(this.shapes, [0.5, 2.5, 1.25], 1.25, COL_BLACK, MAT_COPPER);
new Ball(this.shapes, [-1.5, -2.5, 0.8], 0.8, COL_ORANGE_ORANGE, MAT_PLASTER);
this.shapes[0].colour = function(p) {
let f = Math.sin(p[0]) + p[1];
let index;
if (f > 0 && f < 1) {
index = 0;
} else {
index = 1 + ((Math.floor(p[0] / 4) + Math.floor(p[1] / 4 )) & 1);
}
return [COL_DEEP_PINK, COL_GREY, COL_BLACK, COL_DEEP_BLUE][index];
}
this.shapes[8].colour = function(p) { // 8-ball
let cosTheta1, cosTheta2;
cosTheta1 = vecDot(vecMinus(p, this.centre), vecNormalize([-1, -1, 0])) / this.radius;
if (cosTheta1 < 0.905) {
return COL_BLACK;
}
cosTheta1 = vecDot(vecMinus(p, this.centre), vecNormalize([-1, -1, 0.15])) / this.radius;
cosTheta2 = vecDot(vecMinus(p, this.centre), vecNormalize([-1, -1, -0.12])) / this.radius;
if ((cosTheta1 > 0.9920 && cosTheta1 < 0.999) || (cosTheta2 > 0.9900 && cosTheta2 < 0.9980)) {
return COL_BLACK;
}
return COL_WHITE;
}
new Spotlight(this.shapes, this.lights, [0, 0, 10], 1.0, [0, 0, -1], 40);
new Spotlight(this.shapes, this.lights, [-4, -4, 10], 0.5, [1, 1, -1], 80);
this.camera = new Camera([-1, -3, 2], 80, -20, this.canvasWidth, this.canvasHeight);
break;
case 1:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_GREY, MAT_LINOLEUM),
new Plane([0, 18, 0], [0, -1, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([0, -24, 0], [0, 1, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([12, 0, 0], [-1, 0, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([-12, 0, 0], [1, 0, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([0, 0, 12], [0, 0, -1], COL_GREY, MAT_PLASTER),
];
this.shapes[0].colour = function(p) {
let x = Math.abs((p[0] + 100.7) % 2.3 - 1.15);
let y = Math.abs((p[1] + 102.7) % 2.3 - 1.15);
let index = 0;
if (x < 0.03 || y < 0.03 || x + y < 0.6) {
index = 1;
}
return [COL_WHITE, COL_BLACK][index];
}
new Box(this.shapes, [-5, 8, 0], [1.5, -0.3, 0], [0.3, 1.5, 0], [0, 0, 0.5], COL_RAW_UMBER, MAT_COPPER);
new Box(this.shapes, [-2, 8, 0], [1.5, -0.3, 0], [0.3, 1.5, 0], [0, 0, 0.5], COL_WHITE, MAT_GLASS);
new Box(this.shapes, [1, 8, 0], [1.5, -0.3, 0], [0.3, 1.5, 0], [0, 0, 0.5], COL_AMETHYST, MAT_PLASTER);
new Ball(this.shapes, [-5, 10, 1], 1, COL_WHITE, MAT_GLASS);
new Ball(this.shapes, [-2, 10, 1], 1, COL_AMETHYST, MAT_PLASTER);
new Ball(this.shapes, [1, 10, 1], 1, COL_RAW_UMBER, MAT_COPPER);
new Ball(this.shapes, [-1, 14, 2], 2, COL_WHITE, MAT_MIRROR);
new Spotlight(this.shapes, this.lights, [0, 0, 10], 1, [0, 0, -1], 40);
new Spotlight(this.shapes, this.lights, [-6, 10, 4.5], 0.5, [1, 0, -1], 40), COL_FIRE_ENGINE_RED;
new Spotlight(this.shapes, this.lights, [-3, 10, 5.5], 0.5, [0, 0, -1], 40, COL_LIME_GREEN);
new Spotlight(this.shapes, this.lights, [0, 10, 4.5], 0.5, [-1, 0, -1], 40, COL_DEEP_BLUE);
this.camera = new Camera([-2, -6, 5], 90, -10, this.canvasWidth, this.canvasHeight);
break;
case 2:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_GREY, MAT_LINOLEUM),
new Plane([0, 12, 0], [0, -1, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([0, -12, 0], [0, 1, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([12, 0, 0], [-1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([-12, 0, 0], [1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([0, 0, 12], [0, 0, -1], COL_GREY, MAT_PLASTER),
];
this.shapes[0].colour = function(p) {
let x = Math.abs((p[0] + 100.7) % 2.3 - 1.15);
let y = Math.abs((p[1] + 102.7) % 2.3 - 1.15);
let index = 0;
if (x < 0.03 || y < 0.03 || x + y < 0.6) {
index = 1;
}
//let index = (Math.floor((p[0] + 0.7) / 0.32) + Math.floor((p[1] + 14.2) / 0.32)) & 1;
return [COL_WHITE, COL_BLACK][index];
}
new Box(this.shapes, [-1, -1, 0.9], [2, 0, 0], [0, 2, 0], [0, 0, 0.1], COL_ENGLISH_WALNUT, MAT_PLASTER);
new Box(this.shapes, [0.9, -1, 0], [0.1, 0, 0], [0, 0.1, 0], [0, 0, 1], COL_ENGLISH_WALNUT, MAT_PLASTER);
new Box(this.shapes, [0.9, 0.9, 0], [0.1, 0, 0], [0, 0.1, 0], [0, 0, 1], COL_ENGLISH_WALNUT, MAT_PLASTER);
new Box(this.shapes, [-1, 0.9, 0], [0.1, 0, 0], [0, 0.1, 0], [0, 0, 1], COL_ENGLISH_WALNUT, MAT_PLASTER);
new Box(this.shapes, [-1, -1, 0], [0.1, 0, 0], [0, 0.1, 0], [0, 0, 1], COL_ENGLISH_WALNUT, MAT_PLASTER);
new Bowl(this.shapes, [0.3, -0.1, 1.5], 0.5, 0.45, [0, 0, 1], COL_WHITE, MAT_GLASS);
new Halfball(this.shapes, [0.3, -0.1, 1.5], 0.45, [0, 0, 1], 0.2, undefined, COL_WHITE, MAT_WATER);
new Ball(this.shapes, [-0.7, 0.1, 1.15], 0.15, COL_ORANGE_ORANGE, MAT_PLASTER);
new Ball(this.shapes, [-0.5, -0.5, 1.25], 0.25, COL_GRAPEFRUIT_YELLOW, MAT_PLASTER);
new Ball(this.shapes, [0.38, -0.12, 1.30], 0.05, COL_ROBINS_EGG_BLUE, MAT_PLASTER);
new Can(this.shapes, [0.6, -0.7, 1.2], [0, 0, 1], 0.2, 0.1, COL_CHOCOLATE, COL_FIRE_ENGINE_RED, MAT_COPPER);
new Box(this.shapes, [-1.25, 2.75, 0], [0.9, 0.2, 0], [-0.2, 0.9, 0], [0, 0, 0.5], COL_LIME_GREEN, MAT_PLASTER);
new Spotlight(this.shapes, this.lights, [0, -2, 8.5], 1, [0, 0.1, -1], 60);
//new Spotlight(this.shapes, this.lights, [-6, -2, 8.5], 0.5, [3, 1, -1], 40);
//new Spotlight(this.shapes, this.lights, [0, 10, 4.5], 1, [0, 0, -1], 40);
//new Spotlight(this.shapes, this.lights, [-3, 10, 4.5], 0.5, [0, 0, -1], 40);
this.camera = new Camera([-1.1, -1.6, 2], 54, -32, this.canvasWidth, this.canvasHeight);
break;
case 3:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_GREY, MAT_LINOLEUM),
/* new Plane([0, 16, 0], [0, -1, 0], COL_VERY_DARK_GREY, MAT_PLASTER),
new Plane([0, -16, 0], [0, 1, 0], COL_VERY_DARK_GREY, 0.02),
new Plane([16, 0, 0], [-1, 0, 0], COL_DARK_GREY, 0.02),
new Plane([-16, 0, 0], [1, 0, 0], COL_DARK_GREY, 0.02),*/
];
new Bowl(this.shapes, [-2.3, 1, 1], 1, 0.8, [0, 0, 1], COL_DEEP_BLUE, MAT_COPPER);
new Ball(this.shapes, [-2.3, 1, 0.7], 0.5, COL_GRAPEFRUIT_YELLOW, MAT_PLASTER);
new Ball(this.shapes, [0.35, -0.8, 2.8], 0.3, COL_ORANGE_ORANGE, MAT_PLASTER);
new Ball(this.shapes, [-0.4, 3.5, 2], 2, COL_RAW_UMBER, MAT_PLASTER);
new Cuboctahedron(this.shapes, [-0.7, -2, 0], [2.5, 0, 0], [0, 2.5, 0], [0, 0, 2.5], COL_DEEP_PINK, COL_DARK_GREY, MAT_PLASTER);
this.shapes[0].colour = function(p) {
let index = ((Math.floor((0.6 * p[0] + 0.8 * p[1] + 0.7) / 3.2) + Math.floor((0.8 * p[0] - 0.6 * p[1] + 0.2) / 3.2)) & 1);
return [COL_WHITE, COL_BLACK][index];
}
new Spotlight(this.shapes, this.lights, [0, -2, 8.5], 1, [0, 0.1, -1], 40);
this.camera = new Camera([-3, -7, 4.5], 68, -16, this.canvasWidth, this.canvasHeight);
break;
case 4:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_GREY, MAT_LINOLEUM),
new Plane([0, 12, 0], [0, -1, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([0, -24, 0], [0, 1, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([12, 0, 0], [-1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([-12, 0, 0], [1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([0, 0, 12], [0, 0, -1], COL_GREY, MAT_PLASTER),
]
this.shapes[0].colour = function(p) {
let index = ((Math.floor((0.6 * p[0] + 0.8 * p[1] + 0.7) / 3.2) + Math.floor((0.8 * p[0] - 0.6 * p[1] + 0.2) / 3.2)) & 1);
return [COL_WHITE, COL_BLACK][index];
}
new Ball(this.shapes, [0, 0, 2], 2, COL_RAW_UMBER, MAT_COPPER);
new Ball(this.shapes, [3.2, 0.4, 1], 1, COL_LIME_GREEN, MAT_PLASTER);
new Prism(this.shapes, [-3, -2.5, 0], [9, -2.6, 0], [9, 0.2, 0], [0, 0, 2.2], COL_WHITE, MAT_GLASS);
new Spotlight(this.shapes, this.lights, [-1, -3, 8.5], 1.0, [0, 0, -1], 40);
new Spotlight(this.shapes, this.lights, [-6, 0, 6.5], 0.5, [1, 0, -1], 40);
new Box(this.shapes, [2, 3, 0], [2, -1, 0], [0.1, 0.2, 0], [0, 0, 5], COL_WHITE, MAT_MIRROR);
new Box(this.shapes, [4.5, 1.5, 0], [1, -2, 0], [0.2, 0.1, 0], [0, 0, 5], COL_WHITE, MAT_MIRROR);
this.camera = new Camera([-2, -13, 5.5], 68, -17, this.canvasWidth, this.canvasHeight);
break;
case 5:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_SILVER, MAT_LINOLEUM),
new Plane([0, 4, 0], [0, -1, 0], COL_GRAPEFRUIT_YELLOW, MAT_PLASTER),
new Plane([0, -14, 0], [0, 1, 0], COL_GREY, MAT_PLASTER),
new Plane([4, 0, 0], [-1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([-4, 0, 0], [1, 0, 0], COL_FIRE_ENGINE_RED, MAT_PLASTER),
new Plane([0, 0, 8], [0, 0, -1], COL_DARK_GREY, MAT_PLASTER),
]
/*this.shapes[0].colour = function(p) {
let index = ((Math.floor((0.6 * p[0] + 0.8 * p[1] + 0.7) / 3.2) + Math.floor((0.8 * p[0] - 0.6 * p[1] + 0.2) / 3.2)) & 1);
return [COL_WHITE, COL_BLACK][index];
}*/
//new Ball(this.shapes, [0, 0, 3], 2, COL_WHITE, MAT_GLASS);
/*new Ball(this.shapes, [-1.7, 1, 1], 1, COL_WHITE, MAT_GLASS);
new Ball(this.shapes, [1.2, -0.3, 1], 1, COL_WHITE, MAT_MIRROR);*/
let tallestX = 0;
let tallestY = 0;
let tallestZ = 0;
let shortestX = 0;
let shortestY = 0;
let shortestZ = Infinity;
for (let b = 0; b < 5; b++) {
let centreX = 6 * Math.random() - 3;
let centreY = 6 * Math.random() - 3;
let theta = Math.PI / 2 * Math.random();
let cosTh = Math.cos(theta);
let sinTh = Math.sin(theta);
let base = 0; // (Math.random() < 0.9) ? 0 : 4 * Math.random();
let height = 0.1 + 3 * Math.random();
if (base + height > tallestZ) {
tallestX = centreX;
tallestY = centreY;
tallestZ = base + height;
}
if (base + height < shortestZ) {
shortestX = centreX;
shortestY = centreY;
shortestZ = base + height;
}
let colour = {};
let colHue = 360 * Math.random();
let c = Math.floor(256 * (1 - Math.abs((colHue / 60) % 2 - 1)));
switch (Math.floor(colHue / 60)) {
case 0:
colour = { r: 255, g: c, b: 0, a: 1 };
break;
case 1:
colour = { r: c, g: 255, b: 0, a: 1 };
break;
case 2:
colour = { r: 0, g: 255, b: c, a: 1 };
break;
case 3:
colour = { r: 0, g: c, b: 255, a: 1 };
break;
case 4:
colour = { r: c, g: 0, b: 255, a: 1 };
break;
case 5:
colour = { r: 255, g: 0, b: c, a: 1 };
break;
default:
throw new Error(`impossible HSV?`);
break;
}
//let colR = // Math.floor(256 * Math.random());
//let colG = // Math.floor(256 * Math.random());
//let colB = // Math.floor(256 * Math.random());
//let colour = { r: colR, g: colG, b: colB, a: 1 };
let material = MAT_PLASTER;// (Math.random() < 0.95) ? ((Math.random < 0.90) ? MAT_PLASTER : MAT_COPPER) : MAT_MIRROR;
new Box(this.shapes, [centreX - (cosTh + sinTh) / 2, centreY - (-sinTh + cosTh) / 2, base], [cosTh, -sinTh, 0], [sinTh, cosTh, 0], [0, 0, height], colour, material);
}
new Ball(this.shapes, [tallestX, tallestY, tallestZ + 0.5], 0.5, COL_WHITE, MAT_MIRROR);
new Ball(this.shapes, [shortestX, shortestY, shortestZ + 0.5], 0.5, COL_WHITE, MAT_GLASS);
new Spotlight(this.shapes, this.lights, [-1.5, -1, 6.75], 1, [0.5, 0, -1], 20);
new Spotlight(this.shapes, this.lights, [1.5, 1, 6.75], 1, [-0.5, 0, -1], 20);
this.camera = new Camera([0, -10, 6], 90, -20, this.canvasWidth, this.canvasHeight);
break;
case 6:
this.shapes = [
new Plane([0, 0, 0], [0, 0, 1], COL_SILVER, MAT_LINOLEUM),
new Plane([0, 4, 0], [0, -1, 0], COL_GRAPEFRUIT_YELLOW, MAT_PLASTER),
new Plane([0, -14, 0], [0, 1, 0], COL_GREY, MAT_PLASTER),
new Plane([4, 0, 0], [-1, 0, 0], COL_SKY_BLUE, MAT_PLASTER),
new Plane([-4, 0, 0], [1, 0, 0], COL_FIRE_ENGINE_RED, MAT_PLASTER),
new Plane([0, 0, 8], [0, 0, -1], COL_DARK_GREY, MAT_PLASTER),
]
for (let b = 0; b < 0; b++) {