1
- import {
2
- selectAppData ,
3
- selectIsSomeoneScreenSharing
4
- } from '@100mslive/react-sdk' ;
1
+ import { selectIsSomeoneScreenSharing } from '@100mslive/react-sdk' ;
5
2
//@ts -expect-error AR.js doesn't have type definitions
6
3
import * as THREEx from '@ar-js-org/ar.js/three.js/build/ar-threex.js' ;
7
4
import { IThemeManager } from '@jupyterlab/apputils' ;
@@ -10,9 +7,9 @@ import { ISignal, Signal } from '@lumino/signaling';
10
7
import * as THREE from 'three' ;
11
8
import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry' ;
12
9
import { GLTF , GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader' ;
13
- import { APP_DATA } from './constants' ;
14
- import { hmsActions , hmsStore } from './hms' ;
10
+ import { hmsStore } from './hms' ;
15
11
import { IModelRegistryData } from './registry' ;
12
+ import { useCubeStore } from './store' ;
16
13
17
14
const FIRST_SCENE = 0 ;
18
15
const SECOND_SCENE = 1 ;
@@ -29,19 +26,9 @@ class ArCube {
29
26
* Construct a new JupyterLab-Gather widget.
30
27
*/
31
28
constructor ( ) {
32
- this . secondSceneSignal = new Signal ( this ) ;
33
29
this . scaleSignal = new Signal ( this ) ;
34
30
this . bgCubeCenter = new THREE . Vector3 ( ) ;
35
31
this . initialize ( ) ;
36
-
37
- // this.animate();
38
- // window.addEventListener('markerFound', () => {
39
- // console.log('Marker found');
40
- // });
41
-
42
- // window.addEventListener('markerLost', () => {
43
- // console.log('Marker lost');
44
- // });
45
32
}
46
33
47
34
modelInScene : string [ ] ;
@@ -66,59 +53,45 @@ class ArCube {
66
53
resolve : any ;
67
54
deltaTime : number ;
68
55
totalTime : number ;
69
- okToLoadModel : boolean ;
70
56
renderTarget : THREE . WebGLRenderTarget ;
71
57
sceneGroups : THREE . Group [ ] ;
72
58
isSecondScene : boolean ;
73
59
bgCubeBoundingBox : THREE . Box3 ;
74
60
readonly existingWebcam : HTMLVideoElement | null ;
75
61
readonly newWebcam : HTMLVideoElement | undefined ;
76
- readonly secondSceneSignal : Signal < this, boolean > ;
77
62
readonly scaleSignal : Signal < this, IScaleSignal > ;
78
63
bgCubeCenter : THREE . Vector3 ;
79
64
arjsVid : HTMLElement | null ;
80
- // sceneGroup: THREE.Group;
81
- // sceneGroupArray: THREE.Group[];
82
- // edgeGroup: THREE.Group;
83
- // gltfModel: THREE.Group;
84
- // observer: IntersectionObserver;
85
- // readonly markerControls: any;
86
- // readonly ambientLight: THREE.AmbientLight;
87
- // readonly rotationArray: THREE.Vector3[];
88
- // readonly markerRoot: THREE.Group;
89
- // readonly markerGroup: THREE.Group;
90
- // readonly pointLight: THREE.PointLight;
91
- // readonly loader: THREE.TextureLoader;
92
- // readonly stageMesh: THREE.MeshBasicMaterial;
93
- // readonly stage: THREE.Mesh;
94
- // readonly edgeGeometry: THREE.CylinderGeometry;
95
- // readonly edgeCenters: THREE.Vector3[];
96
- // readonly edgeRotations: THREE.Vector3[];
97
- // readonly animationRequestId: number | undefined;
98
- // readonly now: number;
99
- // readonly then: number;
100
- // readonly elapsed: number;
101
- // readonly fpsInterval: number;
102
- // readonly webcamFromArjs: HTMLElement | null;
103
- // model: IModelRegistryData;
65
+ videoDeviceIdUnsub : ( ) => void ;
66
+ isSecondSceneUnsub : ( ) => void ;
104
67
themeChangedSignal : ISignal <
105
68
IThemeManager ,
106
69
IChangedArgs < string , string | null >
107
- > ;
70
+ > | null ;
108
71
109
72
initialize ( ) {
110
73
this . sceneGroups = [ ] ;
111
74
this . modelInScene = new Array ( 2 ) ;
112
75
this . scenesWithModel = { } ;
113
- hmsStore . subscribe (
114
- this . setupSource . bind ( this ) ,
115
- selectAppData ( APP_DATA . videoDeviceId )
76
+
77
+ this . videoDeviceIdUnsub = useCubeStore . subscribe (
78
+ state => state . videoDeviceId ,
79
+ videoDeviceId => {
80
+ console . log ( 'dev - videoDeviceId' , videoDeviceId ) ;
81
+ this . setupSource ( ) ;
82
+ }
116
83
) ;
117
84
118
- this . themeChangedSignal = hmsStore . getState (
119
- selectAppData ( APP_DATA . themeChanged )
85
+ this . isSecondSceneUnsub = useCubeStore . subscribe (
86
+ state => state . isSecondScene ,
87
+ isSecondScene => ( this . isSecondScene = isSecondScene )
120
88
) ;
121
- this . themeChangedSignal . connect ( this . handleThemeChange . bind ( this ) ) ;
89
+
90
+ this . themeChangedSignal = useCubeStore . getState ( ) . themeChangedSignal ;
91
+
92
+ this . themeChangedSignal
93
+ ? this . themeChangedSignal . connect ( this . handleThemeChange . bind ( this ) )
94
+ : console . log ( 'Theme change signal not found' ) ;
122
95
123
96
this . setupThreeStuff ( ) ;
124
97
@@ -131,10 +104,21 @@ class ArCube {
131
104
this . setupScene ( FIRST_SCENE ) ;
132
105
}
133
106
107
+ cleanUp ( ) {
108
+ this . videoDeviceIdUnsub ( ) ;
109
+ this . isSecondSceneUnsub ( ) ;
110
+
111
+ useCubeStore . setState ( {
112
+ canLoadModel : true ,
113
+ modelInScene : [ ] ,
114
+ scenesWithModel : { } ,
115
+ isSecondScene : false
116
+ } ) ;
117
+ }
118
+
134
119
setupThreeStuff ( ) {
135
120
console . log ( 'setting up three stuff' ) ;
136
121
137
- this . okToLoadModel = true ;
138
122
this . scene = new THREE . Scene ( ) ;
139
123
140
124
// promise to track if AR.js has loaded the webcam
@@ -199,13 +183,11 @@ class ArCube {
199
183
this . clock = new THREE . Clock ( ) ;
200
184
this . deltaTime = 0 ;
201
185
this . totalTime = 0 ;
202
-
203
- hmsActions . setAppData ( APP_DATA . renderer , this . renderer ) ;
204
186
}
205
187
206
188
setupSource ( ) {
207
189
console . log ( 'setting up source' ) ;
208
- const deviceId = hmsStore . getState ( selectAppData ( APP_DATA . videoDeviceId ) ) ;
190
+ const deviceId = useCubeStore . getState ( ) . videoDeviceId ;
209
191
210
192
this . arToolkitSource = new THREEx . ArToolkitSource ( {
211
193
sourceType : 'webcam' ,
@@ -228,22 +210,6 @@ class ArCube {
228
210
return cubeColorValue ;
229
211
}
230
212
231
- // updateSource() {
232
- // const deviceId = hmsStore.getState(selectAppData('videoDeviceId'));
233
-
234
- // this.arToolkitSource = new THREEx.ArToolkitSource({
235
- // sourceType: 'webcam',
236
- // deviceId
237
- // });
238
-
239
- // this.arjsVid = document.getElementById('arjs-video');
240
-
241
- // if (this.arjsVid) {
242
- // this.arjsVid.remove();
243
- // }
244
- // this.arToolkitSource.init();
245
- // }
246
-
247
213
setupContext ( ) {
248
214
console . log ( 'setting up context' ) ;
249
215
@@ -390,10 +356,9 @@ class ArCube {
390
356
// }
391
357
392
358
// load model
393
- this . okToLoadModel = false ;
394
- hmsActions . setAppData ( APP_DATA . canLoadModel , false ) ;
359
+ useCubeStore . setState ( { canLoadModel : false } ) ;
395
360
396
- if ( 'url' in model ) {
361
+ if ( 'url' in model ! ) {
397
362
this . gltfLoader . load (
398
363
model . url ,
399
364
gltf => {
@@ -406,7 +371,7 @@ class ArCube {
406
371
console . log ( 'Error loading model url' , error ) ;
407
372
}
408
373
) ;
409
- } else if ( 'gltf' in model ) {
374
+ } else if ( 'gltf' in model ! ) {
410
375
// const data = JSON.stringify(model.gltf);
411
376
const data = model . gltf ;
412
377
this . gltfLoader . parse (
@@ -462,7 +427,6 @@ class ArCube {
462
427
463
428
// add model to scene
464
429
this . sceneGroups [ sceneNumber ] . add ( gltfModel ) ;
465
- this . okToLoadModel = true ;
466
430
467
431
// Track which scenes a model is loaded in
468
432
// This is mostly to reflect changes to a model in JupyterCAD if it's loaded in multiple scenes
@@ -484,10 +448,11 @@ class ArCube {
484
448
// Track which model is loaded in which scene
485
449
// This is to get model names on the scale sliders
486
450
this . modelInScene [ sceneNumber ] = modelName ;
451
+ useCubeStore . setState ( { modelInScene : this . modelInScene } ) ;
487
452
488
453
// update app data state
489
- hmsActions . setAppData ( APP_DATA . loadedModels , updatedScenesWithModel ) ;
490
- hmsActions . setAppData ( APP_DATA . canLoadModel , true ) ;
454
+ useCubeStore . setState ( { canLoadModel : true } ) ;
455
+ useCubeStore . setState ( { scenesWithModel : updatedScenesWithModel } ) ;
491
456
492
457
// Send scale value to right sidebar
493
458
this . scaleSignal . emit ( { sceneNumber, scale : minRatio } ) ;
@@ -522,15 +487,14 @@ class ArCube {
522
487
}
523
488
524
489
findModelByName ( name : string ) {
525
- const modelRegistry = hmsStore . getState (
526
- selectAppData ( APP_DATA . modelRegistry )
527
- ) ;
490
+ const modelRegistry = useCubeStore . getState ( ) . modelRegistry ;
528
491
return modelRegistry . find (
529
492
( model : IModelRegistryData ) => model . name === name
530
493
) ;
531
494
}
532
495
533
496
changeModelInScene ( sceneNumber : number , modelName : string ) {
497
+ console . log ( 'dev - change model in scene' , sceneNumber , modelName ) ;
534
498
// update tracking stuff
535
499
const modelNameToRemove = this . modelInScene [ sceneNumber ] ;
536
500
const updatedModels = { ...this . scenesWithModel } ;
@@ -558,14 +522,12 @@ class ArCube {
558
522
559
523
enableSecondScene ( ) {
560
524
console . log ( 'enabling second' ) ;
561
- this . isSecondScene = true ;
562
525
this . setupScene ( SECOND_SCENE ) ;
563
- this . secondSceneSignal . emit ( true ) ;
526
+ useCubeStore . setState ( { isSecondScene : true } ) ;
564
527
}
565
528
566
529
disableSecondScene ( ) {
567
530
console . log ( 'disabling second' ) ;
568
- this . isSecondScene = false ;
569
531
//TODO this won't work with more than two scenes but it's fine for now
570
532
this . sceneGroups . pop ( ) ;
571
533
@@ -575,7 +537,7 @@ class ArCube {
575
537
}
576
538
} ) ;
577
539
578
- this . secondSceneSignal . emit ( false ) ;
540
+ useCubeStore . setState ( { isSecondScene : false } ) ;
579
541
}
580
542
581
543
resizeCanvasToDisplaySize ( ) {
0 commit comments