1- //import * as THREE from 'three';
2- // TODO import direct from AR.js npm package
3- //import * as Arjs from '../js/arjs.js';
1+ function isMobile ( ) {
2+ if ( / A n d r o i d | w e b O S | i P h o n e | i P a d | i P o d | B l a c k B e r r y | I E M o b i l e | O p e r a M i n i / i. test ( navigator . userAgent ) ) {
3+ // true for mobile device
4+ return true ;
5+ }
6+ return false ;
7+ }
48
59const scene = new THREE . Scene ( ) ;
610const camera = new THREE . PerspectiveCamera ( 80 , 2 , 0.1 , 50000 ) ;
@@ -13,7 +17,7 @@ const mesh = new THREE.Mesh(geom, material);
1317const threex = new THREEx . LocationBased ( scene , camera ) ;
1418const cam = new THREEx . WebcamRenderer ( renderer , '#video1' ) ;
1519
16- // If using your own GPS location, change the lon and lat of the three meshes
20+ // If using your own GPS location, change the lon and lat of the four meshes.
1721threex . add ( mesh , - 0.72 , 51.051 ) ; // slightly north
1822const material2 = new THREE . MeshBasicMaterial ( { color : 0xffff00 } ) ;
1923const material3 = new THREE . MeshBasicMaterial ( { color : 0x0000ff } ) ;
@@ -22,34 +26,37 @@ threex.add(new THREE.Mesh(geom, material2), -0.72, 51.049); // slightly south
2226threex . add ( new THREE . Mesh ( geom , material3 ) , - 0.722 , 51.05 ) ; // slightly west
2327threex . add ( new THREE . Mesh ( geom , material4 ) , - 0.718 , 51.05 ) ; // slightly east
2428
25- const get = { m : 0 } ;
26- const parts = window . location . href . split ( '?' ) ;
29+ let orientationControls ;
2730
28- if ( parts . length == 2 ) {
29- if ( parts [ 1 ] . endsWith ( '#' ) ) {
30- parts [ 1 ] = parts [ 1 ] . slice ( 0 , - 1 ) ;
31+ if ( isMobile ( ) ) {
32+ orientationControls = new THREEx . DeviceOrientationControls ( camera ) ;
33+ if ( navigator . geolocation !== 'undefined' ) {
34+ navigator . geolocation . getCurrentPosition ( ( pos ) => {
35+ if ( pos !== 'undefined' ) {
36+ console . log ( 'geolocation works' ) ;
37+ threex . startGps ( ) ;
38+ } else {
39+ console . log ( 'geolocation error' ) ;
40+ }
41+ } )
42+ if ( navigator . geolocation . getCurrentPosition ( ( ) => { } ) == null ) {
43+ console . log ( 'geolocation not enabled use fakeGps' ) ;
44+ threex . fakeGps ( - 0.72 , 51.05 ) ;
3145 }
32- const params = parts [ 1 ] . split ( '&' ) ;
33- for ( let i = 0 ; i < params . length ; i ++ ) {
34- const param = params [ i ] . split ( '=' ) ;
35- get [ param [ 0 ] ] = param [ 1 ] ;
46+
47+ } else {
48+ threex . fakeGps ( - 0.72 , 51.05 ) ;
3649 }
37- }
3850
39- let orientationControls ;
40-
41- // Use query string to control behaviour
42- // m=1 or m=2, use DeviceOrientationControls (use on mobile device)
43- // m=2, use actual GPS location
44- // m not 1, use a fake GPS location
45- // so m other than 1 or 2 can be used to test on a desktop machine
46- if ( get . m == 1 || get . m == 2 ) {
47- orientationControls = new THREEx . DeviceOrientationControls ( camera ) ;
48- }
49- if ( get . m == 2 ) {
50- threex . startGps ( ) ;
51+ threex . on ( "gpsupdate" , pos => {
52+ console . log ( `${ pos . coords . latitude } ${ pos . coords . longitude } ` ) ;
53+ } ) ;
5154} else {
5255 threex . fakeGps ( - 0.72 , 51.05 ) ;
56+
57+ threex . on ( "gpsupdate" , pos => {
58+ console . log ( `${ pos . coords . latitude } ${ pos . coords . longitude } ` ) ;
59+ } ) ;
5360}
5461
5562requestAnimationFrame ( render ) ;
0 commit comments