@@ -23,25 +23,43 @@ function main() {
23
23
24
24
const origLon = - 0.72 , origLat = 51.05 ;
25
25
26
+ let alva , arjs , arCamView , ctx , video ;
27
+ let gotFirstGps = false ;
28
+
26
29
Camera . Initialize ( config ) . then ( async ( media ) => {
27
- const video = media . el ;
30
+ video = media . el ;
28
31
const size = resize2cover ( video . videoWidth , video . videoHeight , container . clientWidth , container . clientHeight ) ;
29
32
30
33
canvas . width = container . clientWidth ;
31
34
canvas . height = container . clientHeight ;
32
35
video . style . width = size . width + 'px' ;
33
36
video . style . height = size . height + 'px' ;
34
37
35
- const ctx = canvas . getContext ( '2d' , {
38
+ ctx = canvas . getContext ( '2d' , {
36
39
alpha : false ,
37
40
desynchronized : true
38
41
} ) ;
42
+
43
+
44
+ container . appendChild ( canvas ) ;
45
+ container . appendChild ( view ) ;
46
+
47
+ initAlva ( ) . then ( initArjs ) ;
39
48
40
- const alva = await AlvaAR . Initialize ( canvas . width , canvas . height ) ;
41
- const arCamView = new ARCamView ( view , canvas . width , canvas . height ) ;
49
+ } ) . catch ( error => alert ( error ) ) ;
50
+
51
+ document . getElementById ( "start" ) . addEventListener ( "click" , e => {
52
+ setupFrameHandler ( ) ;
53
+ } ) ;
54
+
55
+
56
+ async function initAlva ( ) {
57
+ alva = await AlvaAR . Initialize ( canvas . width , canvas . height ) ;
58
+ arCamView = new ARCamView ( view , canvas . width , canvas . height ) ;
59
+ }
42
60
43
- const arjs = new THREEx . LocationBased ( arCamView . scene , arCamView . camera , { initialPositionAsOrigin : true } ) ;
44
- console . log ( 'sending fakeGps' ) ;
61
+ function initArjs ( ) {
62
+ arjs = new THREEx . LocationBased ( arCamView . scene , arCamView . camera , { initialPositionAsOrigin : true } ) ;
45
63
46
64
const geom = new THREE . BoxGeometry ( 20 , 20 , 20 ) ;
47
65
const props = [ {
@@ -76,47 +94,51 @@ function main() {
76
94
yDis :- 100
77
95
}
78
96
] ;
79
- let objectsAdded = false ;
80
97
81
- arjs . on ( "gpsupdate" , e => {
82
- console . log ( 'camera position now:' ) ;
98
+ arjs . on ( "gpsupdate" , pos => {
99
+ alert ( `Got GPS position: ${ pos . coords . longitude } ${ pos . coords . latitude } ` ) ;
100
+ console . log ( `camera position now:` ) ;
83
101
console . log ( arCamView . camera . position ) ;
84
- if ( ! objectsAdded ) {
102
+ if ( ! gotFirstGps ) {
85
103
for ( let i = 0 ; i < props . length ; i ++ ) {
86
104
const object = new THREE . Mesh ( geom , props [ i ] . mtl ) ;
87
105
object . visible = false ;
88
106
const pos = arjs . lonLatToWorldCoords ( origLon + props [ i ] . lonDis , origLat + props [ i ] . latDis ) ;
89
107
console . log ( pos ) ;
90
108
arCamView . addObject ( object , pos [ 0 ] , arCamView . camera . position . y + props [ i ] . yDis , pos [ 1 ] ) ;
91
109
}
92
- objectsAdded = true ;
93
- onFrame ( ( ) => {
94
- ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
95
- ctx . drawImage ( video , 0 , 0 , video . videoWidth , video . videoHeight ) ;
96
- const frame = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
97
- const pose = alva . findCameraPose ( frame ) ;
98
- if ( pose ) {
99
- arCamView . updateCameraPose ( pose ) ;
100
- } else {
101
- arCamView . lostCamera ( ) ;
102
- const dots = alva . getFramePoints ( ) ;
103
- for ( const p of dots ) {
104
- ctx . fillStyle = 'white' ;
105
- ctx . fillRect ( p . x , p . y , 2 , 2 ) ;
106
- }
107
- }
108
- return true ;
109
- } , 30 ) ;
110
+ gotFirstGps = true ;
111
+ //setupFrameHandler();
110
112
}
111
113
} ) ;
114
+ // arjs.startGps();
115
+ arjs . fakeGps ( - 0.72 , 51.05 ) ;
116
+ }
112
117
113
- container . appendChild ( canvas ) ;
114
- container . appendChild ( view ) ;
115
- arjs . fakeGps ( origLon , origLat ) ;
118
+ function setupFrameHandler ( ) {
119
+ if ( gotFirstGps ) {
120
+ onFrame ( ( ) => {
116
121
117
-
118
-
119
- } ) . catch ( error => alert ( error ) ) ;
122
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
123
+ ctx . drawImage ( video , 0 , 0 , video . videoWidth , video . videoHeight ) ;
124
+ const frame = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
125
+ const pose = alva . findCameraPose ( frame ) ;
126
+ if ( pose ) {
127
+ arCamView . updateCameraPose ( pose ) ;
128
+ } else {
129
+ arCamView . lostCamera ( ) ;
130
+ const dots = alva . getFramePoints ( ) ;
131
+ for ( const p of dots ) {
132
+ ctx . fillStyle = 'white' ;
133
+ ctx . fillRect ( p . x , p . y , 2 , 2 ) ;
134
+ }
135
+ }
136
+ return true ;
137
+ } , 30 ) ;
138
+ } else {
139
+ alert ( 'Cannot start frame processing as no GPS location yet' ) ;
140
+ }
141
+ }
120
142
}
121
143
122
144
main ( ) ;
0 commit comments