-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1304 lines (1109 loc) · 52.1 KB
/
main.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
// This code is based on three.js, which comes with the following license:
//
// The MIT License
//
// Copyright © 2010-2024 three.js authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
import * as THREE from 'three';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { DragControls } from 'three/addons/controls/DragControls.js';
import { VRButton } from 'three/addons/webxr/VRButton.js';
import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js';
import { InteractiveGroup } from 'three/addons/interactive/InteractiveGroup.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
// import { createMeshesFromInstancedMesh } from 'three/examples/jsm/utils/SceneUtils.js';
import * as CONST from './raytracing/Constants.js';
import { Util } from './raytracing/Util.js';
import { SceneObject } from './raytracing/SceneObject.js';
import { RectangleShape } from './raytracing/shapes/RectangleShape.js';
import { SphereShape } from './raytracing/shapes/SphereShape.js';
import { CylinderMantleShape } from './raytracing/shapes/CylinderMantleShape.js';
import { ColourSurface } from './raytracing/surfaces/ColourSurface.js';
import { MirrorSurface } from './raytracing/surfaces/MirrorSurface.js';
import { ThinFocussingSurface } from './raytracing/surfaces/ThinFocussingSurface.js';
import { CheckerboardSurface } from './raytracing/surfaces/CheckerboardSurface.js';
import { RaytracingScene } from './raytracing/RaytracingScene.js';
import { RaytracingSphere } from './raytracing/RaytracingSphere.js';
// this works fine both locally and when deployed on github
const fragmentShaderCodeFile = await fetch("./raytracing/fragmentShader.glsl");
const fragmentShaderCode = await fragmentShaderCodeFile.text();
const vertexShaderCodeFile = await fetch("./raytracing/vertexShader.glsl");
const vertexShaderCode = await vertexShaderCodeFile.text();
let appName = 'GUPGPU resonaTHOR';
let appLongName = 'GUPGPU (Glasgow University Physics Graphics Processing Unit) resonaTHOR';
let appDescription = 'the premier web-based, GPU-powered, highly scientific, raytracer that simulates the view inside optical resonators';
let scene;
let renderer;
let backgroundTexture;
let camera;
let orbitControls;
let dragControls;
let raytracingSphere;
let raytracingScene;
let background = 0;
// enables lifting stuff to a base level (in case of VR the eye level)
let baseY = 0.0;
let fovScreen = 68;
let raytracingSphereRadius = 100.0;
// camera with wide aperture
let apertureRadius = 0.0;
let atanFocusDistance = Math.atan(3e8); // 1 light second
let noOfRays = 1;
let autofocus = false;
// the status text area
let status; // = document.createElement('div');
let statusTime; // the time the last status was posted
// the info text area
let info;
// the menu
let gui;
let GUIParams;
let autofocusControl, focusDistanceControl, baseYControl, backgroundControl, vrControlsVisibleControl, focussingTypeControl, showSelfConjugatePlanesControl, showSphereControl;
let GUIMesh;
// let showGUIMesh;
// let meshRotationX = -Math.PI/4, meshRotationY = 0, meshRotationZ = 0;
// true if stored photo is showing
let showingStoredPhoto = false;
let storedPhoto;
let storedPhotoDescription;
let storedPhotoInfoString;
// my Canon EOS450D
const click = new Audio('./assets/click.m4a');
// mirror parameters
let xMin = -0.5;
let xMax = +0.5;
let yMin = -0.5;
let yMax = +0.5;
let zMin = -0.5;
let zMax = +0.5;
let xMinMirrorOpticalPower = 0;
let xMaxMirrorOpticalPower = 0;
let zMinMirrorOpticalPower = 0;
let zMaxMirrorOpticalPower = 0;
let stripeWidth = -0.01;
let stripeProtrusion = 0.001;
let xMinMirrorIndex, xMaxMirrorIndex, zMinMirrorIndex, zMaxMirrorIndex, selfConjugateZPlane1Index, selfConjugateZPlane2Index;
let xMinStripeIndex, xMaxStripeIndex, zMinStripeIndex, zMaxStripeIndex;
let sphereIndex;
let focussingType = 0;
let reflectionLossDB = -10;
let showSelfConjugatePlanes = false;
init();
animate();
function init() {
// create the info element first so that any problems can be communicated
createStatus();
scene = new THREE.Scene();
// scene.background = new THREE.Color( 'skyblue' );
let windowAspectRatio = window.innerWidth / window.innerHeight;
camera = new THREE.PerspectiveCamera( fovScreen, windowAspectRatio, 0.1, 2*raytracingSphereRadius + 1 );
camera.position.z = 0.4;
screenChanged();
renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.xr.enabled = true;
document.body.appendChild( VRButton.createButton( renderer ) ); // for VR content
document.body.appendChild( renderer.domElement );
// document.getElementById('livePhoto').appendChild( renderer.domElement );
loadBackgroundImage();
// initRaytracingScene();
raytracingSphere = new RaytracingSphere(
raytracingSphereRadius,
createUniforms(),
vertexShaderCode,
fragmentShaderCode
);
scene.add( raytracingSphere );
// addRaytracingSphere();
// user interface
addEventListenersEtc();
addOrbitControls();
// the controls menu
// refreshGUI();
createGUI();
// addDragControls();
// check if VR is supported (see https://developer.mozilla.org/en-US/docs/Web/API/XRSystem/isSessionSupported)...
// if (navigator.xr) {
if ( 'xr' in navigator ) {
// renderer.xr.enabled = false;
// navigator.xr.isSessionSupported("immersive-vr").then((isSupported) => {
navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
if (supported) {
// ... and enable the relevant features
renderer.xr.enabled = true;
// use renderer.xr.isPresenting to find out if we are in XR mode -- see https://threejs.org/docs/#api/en/renderers/webxr/WebXRManager
// (and https://threejs.org/docs/#api/en/renderers/WebGLRenderer.xr, which states that renderer.xr points to the WebXRManager)
document.body.appendChild( VRButton.createButton( renderer ) ); // for VR content
addXRInteractivity();
}
});
}
createInfo();
refreshInfo();
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
// requestAnimationFrame( animate );
// stats.begin();
if(!showingStoredPhoto) {
// update uniforms
updateUniforms();
renderer.render( scene, camera );
}
// stats.end();
}
function initRaytracingScene() {
raytracingScene = new RaytracingScene();
let redSurfaceID = raytracingScene.createColourSurfaceID(
Util.red, // colourFactor
false // semitransparent
);
let sphereSurfaceID = raytracingScene.createColourSurfaceID(
Util.yellow, // colourFactor
false // semitransparent
);
// raytracingScene.addColourSurface( ColourSurface.red );
xMinMirrorIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, yMin, zMin), // corner
new THREE.Vector3(0, yMax-yMin, 0), // span vector 1
new THREE.Vector3(0, 0, zMax-zMin), // span vector 2
), // shapeID
raytracingScene.createThinFocussingSurfaceID(
new THREE.Vector3(xMin, 0.5*(yMin+yMax), 0.5*(zMin+zMax)), // principalPoint
xMinMirrorOpticalPower, // opticalPower
CONST.SPHERICAL_FOCUSSING_TYPE, // focussing type
Util.zHat, // optical-power direction
true, // reflective
CONST.IDEAL_REFRACTION_TYPE, // refraction type
CONST.ONE_SURFACE_COLOUR_FACTOR // colourFactor
) // surfaceID
));
xMinStripeIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin+stripeProtrusion, -0.5*stripeWidth, zMin), // corner
new THREE.Vector3(0, stripeWidth, 0), // span vector 1
new THREE.Vector3(0, 0, zMax-zMin) // span vector 2
), // shapeID
redSurfaceID // surfaceID
));
xMaxMirrorIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMax, yMin, zMin), // corner
new THREE.Vector3(0, yMax-yMin, 0), // span vector 1
new THREE.Vector3(0, 0, zMax-zMin) // span vector 2
), // shapeID
raytracingScene.createThinFocussingSurfaceID(
new THREE.Vector3(xMax, 0.5*(yMin+yMax), 0.5*(zMin+zMax)), // principalPoint
xMaxMirrorOpticalPower, // opticalPower
CONST.SPHERICAL_FOCUSSING_TYPE, // focussing type
Util.zHat, // optical-power direction
true, // reflective
CONST.IDEAL_REFRACTION_TYPE, // refraction type
CONST.ONE_SURFACE_COLOUR_FACTOR // colourFactor
) // surfaceID
));
xMaxStripeIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMax-stripeProtrusion, -0.5*stripeWidth, zMin), // corner
new THREE.Vector3(0, stripeWidth, 0), // span vector 1
new THREE.Vector3(0, 0, zMax-zMin) // span vector 2
), // shapeID
redSurfaceID // surfaceID
));
zMinMirrorIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, yMin, zMin), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, yMax-yMin, 0) // span vector 2
), // shapeID
raytracingScene.createThinFocussingSurfaceID(
new THREE.Vector3(0.5*(xMin+xMax), 0.5*(yMin+yMax), zMin), // principalPoint
zMinMirrorOpticalPower, // opticalPower
CONST.SPHERICAL_FOCUSSING_TYPE, // focussing type
Util.xHat, // optical-power direction
true, // reflective
CONST.IDEAL_REFRACTION_TYPE, // refraction type
CONST.ONE_SURFACE_COLOUR_FACTOR // colourFactor
) // surfaceID
));
zMinStripeIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, -0.5*stripeWidth, zMin+stripeProtrusion), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, stripeWidth, 0) // span vector 2
), // shapeIndex
redSurfaceID // surfaceID
));
zMaxMirrorIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, yMin, zMax), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, yMax-yMin, 0) // span vector 2
), // shapeID
raytracingScene.createThinFocussingSurfaceID(
new THREE.Vector3(0.5*(xMin+xMax), 0.5*(yMin+yMax), zMax), // principalPoint
zMaxMirrorOpticalPower, // opticalPower
CONST.SPHERICAL_FOCUSSING_TYPE, // focussing type
Util.xHat, // optical-power direction
true, // reflective
CONST.IDEAL_REFRACTION_TYPE, // refraction type
CONST.ONE_SURFACE_COLOUR_FACTOR // colourFactor
) // surfaceID
));
zMaxStripeIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, -0.5*stripeWidth, zMax-stripeProtrusion), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, stripeWidth, 0) // span vector 2
), // shapeID
redSurfaceID // surfaceID
));
let selfConjugatePlane1SurfaceID = raytracingScene.createCheckerboardSurfaceID(
.1, .1, // widths
new THREE.Vector4(1, 0.6, 0.6, 1), Util.white, // colour factors
true, true // semitransparencies
);
let selfConjugatePlane2SurfaceID = raytracingScene.createCheckerboardSurfaceID(
.1, .1, // widths
new THREE.Vector4(.6, 0.6, 1, 1), Util.white, // colour factors
true, true // semitransparencies
);
selfConjugateZPlane1Index = raytracingScene.addSceneObject(new SceneObject(
showSelfConjugatePlanes, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, yMin, zMax), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, yMax-yMin, 0) // span vector 2
), // shapeIndex
selfConjugatePlane1SurfaceID // surfaceID
));
selfConjugateZPlane2Index = raytracingScene.addSceneObject(new SceneObject(
showSelfConjugatePlanes, // visible
raytracingScene.createRectangleShapeID(
new THREE.Vector3(xMin, yMin, zMax), // corner
new THREE.Vector3(xMax-xMin, 0, 0), // span vector 1
new THREE.Vector3(0, yMax-yMin, 0) // span vector 2
), // shapeIndex
selfConjugatePlane2SurfaceID // surfaceID
));
sphereIndex = raytracingScene.addSceneObject(new SceneObject(
true, // visible
raytracingScene.createSphereShapeID(
new THREE.Vector3(0, 0, 0), // centre
0.01 // radius
),
sphereSurfaceID // surfaceID
));
console.log( raytracingScene.getSceneSummary() );
}
function createUniforms() {
// create arrays of random numbers (as GLSL is rubbish at doing random numbers)
let randomNumbersX = [];
let randomNumbersY = [];
// make the first random number 0 in both arrays, meaning the 0th ray starts from the centre of the aperture
randomNumbersX.push(0);
randomNumbersY.push(0);
// fill in the rest of the array with random numbers
let i=1;
do {
// create a new pairs or random numbers (x, y) such that x^2 + y^2 <= 1
let x = 2*Math.random()-1; // random number between -1 and 1
let y = 2*Math.random()-1; // random number between -1 and 1
if(x*x + y*y <= 1) {
// (x,y) lies within a circle of radius 1
// add a new point to the array of points on the aperture
randomNumbersX.push(x);
randomNumbersY.push(y);
i++;
}
} while (i < 100);
initRaytracingScene();
return {
maxTraceLevel: { value: 10 },
backgroundTexture: { value: backgroundTexture },
focusDistance: { value: 10.0 },
apertureXHat: { value: new THREE.Vector3(1, 0, 0) },
apertureYHat: { value: new THREE.Vector3(0, 1, 0) },
apertureRadius: { value: apertureRadius },
randomNumbersX: { value: randomNumbersX },
randomNumbersY: { value: randomNumbersY },
noOfRays: { value: 1 },
viewDirection: { value: new THREE.Vector3(0, 0, -1) },
keepVideoFeedForward: { value: true },
sceneObjects: { value: raytracingScene.sceneObjects },
noOfSceneObjects: { value: raytracingScene.noOfSceneObjects },
rectangleShapes: { value: raytracingScene.rectangleShapes },
sphereShapes: { value: raytracingScene.sphereShapes },
cylinderMantleShapes: { value: raytracingScene.cylinderMantleShapes },
colourSurfaces: { value: raytracingScene.colourSurfaces },
mirrorSurfaces: { value: raytracingScene.mirrorSurfaces },
// thinLensSurfaces: { value: raytracingScene.thinLensSurfaces },
thinFocussingSurfaces: { value: raytracingScene.thinFocussingSurfaces },
checkerboardSurfaces: { value: raytracingScene.checkerboardSurfaces },
};
}
function updateUniforms() {
let xMinMirrorSurface = raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[xMinMirrorIndex].surfaceID.index];
let xMaxMirrorSurface = raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[xMaxMirrorIndex].surfaceID.index];
let zMinMirrorSurface = raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[zMinMirrorIndex].surfaceID.index];
let zMaxMirrorSurface = raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[zMaxMirrorIndex].surfaceID.index];
let reflectionCoefficient = 1-Math.pow(10, 0.1*reflectionLossDB);
xMinMirrorSurface.colourFactor = Util.coefficient2colourFactor(reflectionCoefficient);
xMaxMirrorSurface.colourFactor = Util.coefficient2colourFactor(reflectionCoefficient);
zMinMirrorSurface.colourFactor = Util.coefficient2colourFactor(reflectionCoefficient);
zMaxMirrorSurface.colourFactor = Util.coefficient2colourFactor(reflectionCoefficient);
xMinMirrorSurface.opticalPower = xMinMirrorOpticalPower;
xMaxMirrorSurface.opticalPower = xMaxMirrorOpticalPower;
zMinMirrorSurface.opticalPower = zMinMirrorOpticalPower;
zMaxMirrorSurface.opticalPower = zMaxMirrorOpticalPower;
xMinMirrorSurface.focussingType = focussingType;
xMaxMirrorSurface.focussingType = focussingType;
zMinMirrorSurface.focussingType = focussingType;
zMaxMirrorSurface.focussingType = focussingType;
let selfConjugateZPlane1 = raytracingScene.sceneObjects[selfConjugateZPlane1Index];
let selfConjugateZPlane2 = raytracingScene.sceneObjects[selfConjugateZPlane2Index];
if(showSelfConjugatePlanes) {
let selfConjugateZPlane1Rectangle = raytracingScene.rectangleShapes[selfConjugateZPlane1.shapeIndex];
let selfConjugateZPlane2Rectangle = raytracingScene.rectangleShapes[selfConjugateZPlane2.shapeIndex];
let L = zMax - zMin;
let f1 = 1/zMinMirrorOpticalPower; // might be infinity, if opt. power = 0
let f2 = 1/zMaxMirrorOpticalPower; // might be infinity, if opt. power = 0
let o11; // will hold first solution for o1
let o12; // will hold second solutuion for o1
let unstable = false; // will be true if resonator is unstable (and therefore has self-conjugate planes), false otherwise
if((zMinMirrorOpticalPower == 0) && (zMaxMirrorOpticalPower == 0)) {
// on edge of stability, so don't set unstable to true
// console.log("plane-plane resonator; no SC planes");
} else if((zMinMirrorOpticalPower == 0) && (zMaxMirrorOpticalPower != 0)) {
let discriminant = L*(L-2*f2);
if(discriminant >= 0) {
unstable = true;
let sqrtDiscriminant = Math.sqrt(discriminant);
o11 = -sqrtDiscriminant;
o12 = +sqrtDiscriminant;
}
// console.log("Mirror 1 planar; discriminant="+discriminant);
} else if((zMaxMirrorOpticalPower == 0) && (zMinMirrorOpticalPower != 0)) {
let discriminant = L*(L-2*f1);
if(discriminant >= 0) {
unstable = true;
let sqrtDiscriminant = Math.sqrt(discriminant);
o11 = L - sqrtDiscriminant;
o12 = L + sqrtDiscriminant;
}
// console.log("Mirror 2 planar; discriminant="+discriminant);
} else {
// discriminant for calculation of o1
let discriminant = calculateDiscriminantForSCPs(f1, f2, L);
// console.log("d="+discriminant);
if(discriminant >= 0) {
// self-conjugate planes exist; show them
unstable = true;
let denominator = 2*(f1+f2-L);
if(denominator == 0.0) {
o11 = 1e6;
o12 = f1;
} else {
let sqrtDiscriminant = Math.sqrt(discriminant);
o11 = (L*(2*f2-L)-sqrtDiscriminant)/denominator;
o12 = (L*(2*f2-L)+sqrtDiscriminant)/denominator;
}
}
// console.log("General case; discriminant="+discriminant);
}
if(unstable) {
// self-conjugate planes exist
selfConjugateZPlane1.visible = true;
selfConjugateZPlane2.visible = true;
selfConjugateZPlane1Rectangle.corner.z = zMin + o11;
selfConjugateZPlane2Rectangle.corner.z = zMin + o12;
// console.log("o<sub>1,(1,2)</sub> = "+o11+", "+o12);
} else {
// self-conjugate planes don't exist; hide them
selfConjugateZPlane1.visible = false;
selfConjugateZPlane2.visible = false;
// console.log("No SC planes");
}
} else {
// self-conjugate planes don't exist; hide them
selfConjugateZPlane1.visible = false;
selfConjugateZPlane2.visible = false;
}
// // are we in VR mode?
let deltaY;
// if(renderer.xr.enabled && renderer.xr.isPresenting) {
deltaY = baseY;
// } else {
// deltaY = 0;
// }
GUIMesh.position.y = deltaY - 1;
// shift the xMin mirror
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[xMinMirrorIndex].shapeID.index].corner.y = yMin + deltaY;
raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[xMinMirrorIndex].surfaceID.index].principalPoint.y = 0.5*(yMin+yMax) + deltaY;
// shift the xMax mirror
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[xMaxMirrorIndex].shapeID.index].corner.y = yMin + deltaY;
raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[xMaxMirrorIndex].surfaceID.index].principalPoint.y = 0.5*(yMin+yMax) + deltaY;
// shift the zMin mirror
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[zMinMirrorIndex].shapeID.index].corner.y = yMin + deltaY;
raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[zMinMirrorIndex].surfaceID.index].principalPoint.y = 0.5*(yMin+yMax) + deltaY;
// shift the zMax mirror
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[zMaxMirrorIndex].shapeID.index].corner.y = yMin + deltaY;
raytracingScene.thinFocussingSurfaces[raytracingScene.sceneObjects[zMaxMirrorIndex].surfaceID.index].principalPoint.y = 0.5*(yMin+yMax) + deltaY;
// shift the stripes
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[xMinStripeIndex].shapeID.index].corner.y = -0.5*stripeWidth + deltaY;
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[xMaxStripeIndex].shapeID.index].corner.y = -0.5*stripeWidth + deltaY;
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[zMinStripeIndex].shapeID.index].corner.y = -0.5*stripeWidth + deltaY;
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[zMaxStripeIndex].shapeID.index].corner.y = -0.5*stripeWidth + deltaY;
// shift the self-conjugate planes
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[selfConjugateZPlane1Index].shapeID.index].corner.y = yMin + deltaY;
raytracingScene.rectangleShapes[raytracingScene.sceneObjects[selfConjugateZPlane2Index].shapeID.index].corner.y = yMin + deltaY;
// let t = 1e-3*Date.now();
// raytracingSphere.uniforms.cylinderMantleShapes.value[0].nDirection = new THREE.Vector3( Math.cos(t), 0, Math.sin(t) );
// raytracingSphere.uniforms.rectangleShapes.value[0].corner = new THREE.Vector3( -.5*Math.cos(3*t), -0.5, -1.-.5*Math.sin(3*t) );
// raytracingSphere.uniforms.rectangleShapes.value[0].span1 = new THREE.Vector3( Math.cos(3*t), 0, Math.sin(3*t) );
// raytracingSphere.uniforms.rectangleShapes.value[0].nNormal = new THREE.Vector3( -Math.sin(3*t), 0, Math.cos(3*t));
// raytracingSphere.uniforms.thinCylLensSurfaces.value[0].nOpticalPowerDirection = new THREE.Vector3( Math.cos(2*t), 0, Math.sin(2*t) );
raytracingSphere.uniforms.backgroundTexture.value = backgroundTexture;
// create the points on the aperture
// create basis vectors for the camera's clear aperture
let viewDirection = new THREE.Vector3();
let apertureBasisVector1 = new THREE.Vector3();
let apertureBasisVector2 = new THREE.Vector3();
camera.getWorldDirection(viewDirection);
viewDirection.normalize();
// postStatus(`viewDirection.lengthSq() = ${viewDirection.lengthSq()}`);
// if(counter < 10) console.log(`viewDirection = (${viewDirection.x.toPrecision(2)}, ${viewDirection.y.toPrecision(2)}, ${viewDirection.z.toPrecision(2)})`);
if((viewDirection.x == 0.0) && (viewDirection.y == 0.0)) {
// viewDirection is along z direction
apertureBasisVector1.crossVectors(viewDirection, new THREE.Vector3(1, 0, 0)).normalize();
} else {
// viewDirection is not along z direction
apertureBasisVector1.crossVectors(viewDirection, new THREE.Vector3(0, 0, 1)).normalize();
}
apertureBasisVector1.crossVectors(THREE.Object3D.DEFAULT_UP, viewDirection).normalize();
// viewDirection = new THREE.Vector3(0, 0, -1);
// apertureBasisVector1 = new THREE.Vector3(1, 0, 0);
apertureBasisVector2.crossVectors(viewDirection, apertureBasisVector1).normalize();
raytracingSphere.uniforms.noOfRays.value = noOfRays;
raytracingSphere.uniforms.apertureXHat.value.copy(apertureBasisVector1);
raytracingSphere.uniforms.apertureYHat.value.copy(apertureBasisVector2);
raytracingSphere.uniforms.viewDirection.value.copy(viewDirection);
raytracingSphere.uniforms.apertureRadius.value = apertureRadius;
let focusDistance = Math.tan(atanFocusDistance);
if(raytracingSphere.uniforms.focusDistance.value != focusDistance) {
raytracingSphere.uniforms.focusDistance.value = focusDistance;
// GUIParams.'tan<sup>-1</sup>(focus. dist.)'.value = atanFocusDistance;
focusDistanceControl.setValue(atanFocusDistance);
}
// (re)create random numbers
// let i=0;
// let randomNumbersX = [];
// let randomNumbersY = [];
// do {
// // create a new pairs or random numbers (x, y) such that x^2 + y^2 <= 1
// let x = 2*Math.random()-1; // random number between -1 and 1
// let y = 2*Math.random()-1; // random number between -1 and 1
// if(x*x + y*y <= 1) {
// // (x,y) lies within a circle of radius 1
// // add a new point to the array of points on the aperture
// randomNumbersX.push(apertureRadius*x);
// randomNumbersY.push(apertureRadius*y);
// i++;
// }
// } while (i < 100);
// raytracingSphere.uniforms.randomNumbersX.value = randomNumbersX;
// raytracingSphere.uniforms.randomNumbersY.value = randomNumbersY;
}
// /** create raytracing sphere */
// function addRaytracingSphere() {
// // create arrays of random numbers (as GLSL is rubbish at doing random numbers)
// let randomNumbersX = [];
// let randomNumbersY = [];
// // make the first random number 0 in both arrays, meaning the 0th ray starts from the centre of the aperture
// randomNumbersX.push(0);
// randomNumbersY.push(0);
// // fill in the rest of the array with random numbers
// let i=1;
// do {
// // create a new pairs or random numbers (x, y) such that x^2 + y^2 <= 1
// let x = 2*Math.random()-1; // random number between -1 and 1
// let y = 2*Math.random()-1; // random number between -1 and 1
// if(x*x + y*y <= 1) {
// // (x,y) lies within a circle of radius 1
// // add a new point to the array of points on the aperture
// randomNumbersX.push(x);
// randomNumbersY.push(y);
// i++;
// }
// } while (i < 100);
// raytracingSphere = new RaytracingSphere(
// raytracingSphereRadius,
// {
// maxTraceLevel: { value: 10 },
// backgroundTexture: { value: backgroundTexture },
// focusDistance: { value: 10.0 },
// apertureXHat: { value: new THREE.Vector3(1, 0, 0) },
// apertureYHat: { value: new THREE.Vector3(0, 1, 0) },
// apertureRadius: { value: apertureRadius },
// randomNumbersX: { value: randomNumbersX },
// randomNumbersY: { value: randomNumbersY },
// noOfRays: { value: 1 },
// viewDirection: { value: new THREE.Vector3(0, 0, -1) },
// keepVideoFeedForward: { value: true },
// sceneObjects: { value: raytracingScene.sceneObjects },
// noOfSceneObjects: { value: raytracingScene.noOfSceneObjects },
// rectangles: { value: raytracingScene.rectangles },
// colours: { value: raytracingScene.colours },
// mirrors: { value: raytracingScene.mirrors },
// },
// vertexShaderCode,
// fragmentShaderCode
// );
// scene.add( raytracingSphere );
// }
function calculateDiscriminantForSCPs(f1, f2, L) {
return L*(L-2*f1)*(L-2*f2)*(L-2*(f1+f2));
}
// see https://github.com/mrdoob/three.js/blob/master/examples/webgl_animation_skinning_additive_blending.html
function createGUI() {
// const
gui = new GUI();
// gui.hide();
// GUIMesh = new HTMLMesh( gui.domElement ); // placeholder
GUIParams = {
maxTraceLevel: raytracingSphere.uniforms.maxTraceLevel.value,
'Horiz. FOV (°)': fovScreen,
'Aperture radius': apertureRadius,
'tan<sup>-1</sup>(focus. dist.)': atanFocusDistance,
'No of rays': noOfRays,
autofocus: function() {
autofocus = !autofocus;
autofocusControl.name( 'Autofocus: ' + (autofocus?'On':'Off') );
focusDistanceControl.disable(autofocus);
}, // (autofocus?'On':'Off'),
// 'Autofocus': autofocus,
'Point forward (in -<b>z</b> direction)': pointForward,
'Show/hide info': toggleInfoVisibility,
vrControlsVisible: function() {
GUIMesh.visible = !GUIMesh.visible;
vrControlsVisibleControl.name( guiMeshVisible2String() );
},
background: function() {
background = (background + 1) % 5;
loadBackgroundImage();
backgroundControl.name( background2String() );
},
baseY: baseY,
makeEyeLevel: function() { baseY = camera.position.y; baseYControl.setValue(baseY); },
reflectionLossDB: reflectionLossDB, // 10*Math.log10(1-raytracingSphereShaderMaterial.uniforms.reflectionCoefficient.value),
xMinMirrorOpticalPower: xMinMirrorOpticalPower,
xMaxMirrorOpticalPower: xMaxMirrorOpticalPower,
zMinMirrorOpticalPower: zMinMirrorOpticalPower,
zMaxMirrorOpticalPower: zMaxMirrorOpticalPower,
focussingType: function() {
focussingType = (focussingType + 1) % 2;
focussingTypeControl.name( focussingType2String() );
},
showSelfConjugatePlanes: function() {
showSelfConjugatePlanes = !showSelfConjugatePlanes;
showSelfConjugatePlanesControl.name( showSelfConjugatePlanes2String() );
},
sphereX: raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.x,
sphereY: raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.y,
sphereZ: raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.z,
sphereR: raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].radius,
showSphere: function() {
raytracingScene.sceneObjects[sphereIndex].visible = !raytracingScene.sceneObjects[sphereIndex].visible;
showSphereControl.name( showSphere2String() );
},
}
gui.add( GUIParams, 'maxTraceLevel', 0, 100, 1 ).name( 'Max. trace level' ).onChange( (r) => {raytracingSphere.uniforms.maxTraceLevel.value = r; } );
baseYControl = gui.add( GUIParams, 'baseY', 0, 3, 0.001).name( "<i>y</i><sub>base</sub>" ).onChange( (y) => { baseY = y; } );
gui.add( GUIParams, 'makeEyeLevel' ).name( 'Eye level -> <i>y</i><sub>base</sub>' );
gui.add( GUIParams, 'reflectionLossDB', -30, 0, 0.1 ).name( 'Refl. loss (dB)' ).onChange( (l) => { reflectionLossDB = l; } );
gui.add( GUIParams, 'xMinMirrorOpticalPower', -10, 10, 0.001 ).name( "OP<sub><i>x</i>,min</sub>" ).onChange( (o) => { xMinMirrorOpticalPower = o; } );
gui.add( GUIParams, 'xMaxMirrorOpticalPower', -10, 10, 0.001 ).name( "OP<sub><i>x</i>,max</sub>" ).onChange( (o) => { xMaxMirrorOpticalPower = o; } );
gui.add( GUIParams, 'zMinMirrorOpticalPower', -10, 10, 0.001 ).name( "OP<sub><i>z</i>,min</sub>" ).onChange( (o) => { zMinMirrorOpticalPower = o; } );
gui.add( GUIParams, 'zMaxMirrorOpticalPower', -10, 10, 0.001 ).name( "OP<sub><i>z</i>,max</sub>" ).onChange( (o) => { zMaxMirrorOpticalPower = o; } );
focussingTypeControl = gui.add( GUIParams, 'focussingType' ).name( focussingType2String() );
showSelfConjugatePlanesControl = gui.add( GUIParams, 'showSelfConjugatePlanes' ).name( showSelfConjugatePlanes2String() );
gui.add( GUIParams, 'sphereX', -.5, .5, 0.0001 ).name( "<i>x</i><sub>sphere centre</sub>" ).onChange( (x) => { raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.x = x } );
gui.add( GUIParams, 'sphereY', -.5, .5, 0.0001 ).name( "<i>y</i><sub>sphere centre</sub>" ).onChange( (y) => { raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.y = y } );
gui.add( GUIParams, 'sphereZ', -.5, .5, 0.0001 ).name( "<i>z</i><sub>sphere centre</sub>" ).onChange( (z) => { raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].centre.z = z } );
gui.add( GUIParams, 'sphereR', 0, 0.1, 0.0001 ).name( "<i>r</i><sub>sphere</sub>" ).onChange( (r) => { raytracingScene.sphereShapes[raytracingScene.sceneObjects[sphereIndex].shapeID.index].radius = r } );
showSphereControl = gui.add( GUIParams, 'showSphere' ).name( showSphere2String() );
// const folderVirtualCamera = gui.addFolder( 'Virtual camera' );
gui.add( GUIParams, 'Horiz. FOV (°)', 1, 170, 1).onChange( setScreenFOV );
gui.add( GUIParams, 'Aperture radius', 0.0, 1.0, 0.01).onChange( (r) => { apertureRadius = r; } );
// autofocusControl = gui.add( GUIParams, 'autofocus' ).name( 'Autofocus: ' + (autofocus?'On':'Off') );
// gui.add( GUIParams, 'Autofocus' ).onChange( (b) => { autofocus = b; focusDistanceControl.disable(autofocus); } );
focusDistanceControl = gui.add( GUIParams, 'tan<sup>-1</sup>(focus. dist.)',
//Math.atan(0.1),
0.01, // -0.5*Math.PI, // allow only positive focussing distances
0.5*Math.PI,
0.0001
).onChange( (a) => { atanFocusDistance = a; } );
focusDistanceControl.disable(autofocus);
// focusDistanceControl = gui.add( GUIParams, 'tan<sup>-1</sup>(focus. dist.)',
// //Math.atan(0.1),
// -0.5*Math.PI,
// 0.5*Math.PI,
// 0.001
// ).onChange( (a) => { atanFocusDistance = a; } );
// folderVirtualCamera.add( atanFocusDistance, 'atan focus dist', -0.5*Math.PI, +0.5*Math.PI ).listen();
gui.add( GUIParams, 'No of rays', 1, 100, 1).onChange( (n) => { noOfRays = n; } );
gui.add( GUIParams, 'Point forward (in -<b>z</b> direction)' );
backgroundControl = gui.add( GUIParams, 'background' ).name( background2String() );
if(renderer.xr.enabled) {
vrControlsVisibleControl = gui.add( GUIParams, 'vrControlsVisible' );
}
// create the GUI mesh at the end to make sure that it includes all controls
GUIMesh = new HTMLMesh( gui.domElement );
GUIMesh.visible = false;
vrControlsVisibleControl.name( guiMeshVisible2String() ); // this can be called only after GUIMesh has been created
enableDisableResonatorControls();
}
function enableDisableResonatorControls() {
}
function background2String() {
switch (background) {
case 0: return 'Glasgow University, West Quadrangle'; // '360-180 Glasgow University - Western Square.jpg' // https://www.flickr.com/photos/pano_philou/1041580126
case 1: return 'Glasgow University, East Quadrangle'; // '360-180 Glasgow University - Eastern Square.jpg' // https://www.flickr.com/photos/pano_philou/1141564032
case 2: return 'Mugdock'; // 'Mugdock Woods 6 Milngavie Scotland Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/3485817556
case 3: return 'Mugdock bluebells'; // 'Bluebells_13_Mugdock_Woods_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49889830418
case 4: return 'Glencoe'; // '360-180 The Glencoe Pass And The Three Sisters.jpg' // https://www.flickr.com/photos/pano_philou/1140758031
default: return 'Undefined';
// 'Tower_University_Glasgow_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49890100126
// 'Saddle_05_Arran_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49889356918
}
}
function getBackgroundInfo() {
switch (background) {
case 0: return '<a href="https://www.flickr.com/photos/pano_philou/1041580126"><i>360-180 Glasgow University - Western Square</i></a> by pano_philou'; // https://www.flickr.com/photos/pano_philou/1041580126
case 1: return '<a href="https://www.flickr.com/photos/pano_philou/1141564032"><i>360-180 Glasgow University - Eastern Square</i></a> by pano_philou'; //
case 2: return '<a href="https://www.flickr.com/photos/gawthrop/3485817556"><i>Mugdock Woods 6 Milngavie Scotland Equirectangular</i></a> by Peter Gawthrop'; // https://www.flickr.com/photos/gawthrop/3485817556
case 3: return '<a href="https://www.flickr.com/photos/gawthrop/49889830418"><i>Bluebells_13_Mugdock_Woods_Scotland-Equirectangular</i></a> by Peter Gawthrop'; //
case 4: return '<a href="https://www.flickr.com/photos/pano_philou/1140758031"><i>360-180 The Glencoe Pass And The Three Sisters</i></a> by pano_philou'; // https://www.flickr.com/photos/pano_philou/1140758031
default: return 'Undefined';
// 'Tower_University_Glasgow_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49890100126
// 'Saddle_05_Arran_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49889356918
}
}
function focussingType2String() {
switch(focussingType) {
case CONST.SPHERICAL_FOCUSSING_TYPE: return 'Spherical, thin, mirrors';
case CONST.CYLINDRICAL_FOCUSSING_TYPE: return 'Cylindrical, thin, mirrors';
default: return 'Undefined';
}
}
function showSelfConjugatePlanes2String() {
return 'Self-conjugate planes '+(showSelfConjugatePlanes?'shown':'hidden');
}
function showSphere2String() {
return 'Sphere '+(raytracingScene.sceneObjects[sphereIndex].visible?'shown':'hidden');
}
function guiMeshVisible2String() {
return 'VR controls '+(GUIMesh.visible?'visible':'hidden');
}
function addXRInteractivity() {
// see https://github.com/mrdoob/three.js/blob/master/examples/webxr_vr_sandbox.html
// the two hand controllers
const geometry = new THREE.BufferGeometry();
geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
const controller1 = renderer.xr.getController( 0 );
controller1.add( new THREE.Line( geometry ) );
scene.add( controller1 );
const controller2 = renderer.xr.getController( 1 );
controller2.add( new THREE.Line( geometry ) );
scene.add( controller2 );
//
const controllerModelFactory = new XRControllerModelFactory();
const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
scene.add( controllerGrip1 );
const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
scene.add( controllerGrip2 );
//
const group = new InteractiveGroup( renderer, camera );
group.listenToPointerEvents( renderer, camera );
group.listenToXRControllerEvents( controller1 );
group.listenToXRControllerEvents( controller2 );
scene.add( group );
// place this below the resonator
// GUIMesh = new HTMLMesh( gui.domElement );
GUIMesh.position.x = 0;
GUIMesh.position.y = baseY - 1.5;
GUIMesh.position.z = -0.4;
GUIMesh.rotation.x = -Math.PI/4;
GUIMesh.scale.setScalar( 2 );
group.add( GUIMesh );
}
function createVideoFeeds() {
// create the video stream for the user-facing camera first, as some devices (such as my iPad), which have both cameras,
// but can (for whatever reason) only have a video feed from one at a time, seem to go with the video stream that was
// created last, and as the standard view is looking "forward" it is preferable to see the environment-facing camera.
videoFeedU = document.getElementById( 'videoFeedU' );
// see https://github.com/mrdoob/three.js/blob/master/examples/webgl_materials_video_webcam.html
if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
// user-facing camera
const constraintsU = { video: {
// 'deviceId': cameraId, // this could be the device ID selected
width: {ideal: 1280}, // {ideal: 10000},
// height: {ideal: 10000},
facingMode: {ideal: 'user'}
// aspectRatio: { exact: width / height }
} };
navigator.mediaDevices.getUserMedia( constraintsU ).then( function ( stream ) {
// apply the stream to the video element used in the texture
videoFeedU.srcObject = stream;
videoFeedU.play();
videoFeedU.addEventListener("playing", () => {
aspectRatioVideoFeedU = videoFeedU.videoWidth / videoFeedU.videoHeight;
updateUniforms();
postStatus(`User-facing(?) camera resolution ${videoFeedU.videoWidth} × ${videoFeedU.videoHeight}`);
});
} ).catch( function ( error ) {
postStatus(`Unable to access user-facing camera/webcam (Error: ${error})`);
} );
} else {
postStatus( 'MediaDevices interface, which is required for video streams from device cameras, not available.' );
}
videoFeedE = document.getElementById( 'videoFeedE' );
// see https://github.com/mrdoob/three.js/blob/master/examples/webgl_materials_video_webcam.html
if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
// environment-facing camera
const constraintsE = { video: {
// 'deviceId': cameraId, // this could be the device ID selected
width: {ideal: 1280}, // {ideal: 10000},
// height: {ideal: 10000},
facingMode: {ideal: 'environment'}
// aspectRatio: { exact: width / height }
} };
navigator.mediaDevices.getUserMedia( constraintsE ).then( function ( stream ) {
// apply the stream to the video element used in the texture
videoFeedE.srcObject = stream;
videoFeedE.play();
videoFeedE.addEventListener("playing", () => {
aspectRatioVideoFeedE = videoFeedE.videoWidth / videoFeedE.videoHeight;
updateUniforms();
postStatus(`Environment-facing(?) camera resolution ${videoFeedE.videoWidth} × ${videoFeedE.videoHeight}`);
});
} ).catch( function ( error ) {
postStatus(`Unable to access environment-facing camera/webcam (Error: ${error})`);
} );
} else {
postStatus( 'MediaDevices interface, which is required for video streams from device cameras, not available.' );
}
}
function loadBackgroundImage() {
const textureLoader = new THREE.TextureLoader();
// textureLoader.crossOrigin = "Anonymous";
let filename;
switch (background) {
case 1:
filename = './raytracing/backgrounds/360-180 Glasgow University - Eastern Square.jpg'; // https://www.flickr.com/photos/pano_philou/1141564032
break;
case 2:
filename = './raytracing/backgrounds/Mugdock Woods 6 Milngavie Scotland Equirectangular.jpg'; // https://www.flickr.com/photos/gawthrop/3485817556
break;
case 3:
filename = './raytracing/backgrounds/Bluebells_13_Mugdock_Woods_Scotland-Equirectangular.jpg'; // https://www.flickr.com/photos/gawthrop/49889830418
break;
case 4:
filename = './raytracing/backgrounds/360-180 The Glencoe Pass And The Three Sisters.jpg'; // https://www.flickr.com/photos/pano_philou/1140758031
break;
case 0:
default:
filename = './raytracing/backgrounds/360-180 Glasgow University - Western Square.jpg'; // https://www.flickr.com/photos/pano_philou/1041580126
// 'Tower_University_Glasgow_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49890100126
// 'Saddle_05_Arran_Scotland-Equirectangular.jpg' // https://www.flickr.com/photos/gawthrop/49889356918
}
backgroundTexture = textureLoader.load(filename);
}
function addEventListenersEtc() {
// handle device orientation
// window.addEventListener("deviceorientation", handleOrientation, true);
// handle window resize
window.addEventListener("resize", onWindowResize, false);
// handle screen-orientation (landscape/portrait) change
screen.orientation.addEventListener( "change", recreateVideoFeeds );
// share button functionality
document.getElementById('takePhotoButton').addEventListener('click', takePhoto);
// toggle fullscreen button functionality
document.getElementById('fullscreenButton').addEventListener('click', toggleFullscreen);