@@ -11,56 +11,70 @@ const camera = new THREE.PerspectiveCamera(80, 2, 0.1, 50000);
11
11
const renderer = new THREE . WebGLRenderer ( { canvas : document . querySelector ( '#canvas1' ) } ) ;
12
12
13
13
const geom = new THREE . BoxGeometry ( 20 , 20 , 20 ) ;
14
- const material = new THREE . MeshBasicMaterial ( { color : 0xff0000 } ) ;
15
- const mesh = new THREE . Mesh ( geom , material ) ;
16
14
17
15
const threex = new THREEx . LocationBased ( scene , camera ) ;
18
16
const cam = new THREEx . WebcamRenderer ( renderer , '#video1' ) ;
19
17
20
- // If using your own GPS location, change the lon and lat of the four meshes.
21
- threex . add ( mesh , - 0.72 , 51.051 ) ; // slightly north
22
- const material2 = new THREE . MeshBasicMaterial ( { color : 0xffff00 } ) ;
23
- const material3 = new THREE . MeshBasicMaterial ( { color : 0x0000ff } ) ;
24
- const material4 = new THREE . MeshBasicMaterial ( { color : 0x00ff00 } ) ;
25
- threex . add ( new THREE . Mesh ( geom , material2 ) , - 0.72 , 51.049 ) ; // slightly south
26
- threex . add ( new THREE . Mesh ( geom , material3 ) , - 0.722 , 51.05 ) ; // slightly west
27
- threex . add ( new THREE . Mesh ( geom , material4 ) , - 0.718 , 51.05 ) ; // slightly east
18
+ const oneDegAsRad = THREE . Math . degToRad ( 1 ) ;
19
+
28
20
29
21
let orientationControls ;
30
22
31
23
if ( isMobile ( ) ) {
32
24
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 ) ;
45
- }
46
-
47
- } else {
48
- threex . fakeGps ( - 0.72 , 51.05 ) ;
49
- }
25
+ }
50
26
51
- threex . on ( "gpsupdate" , pos => {
52
- console . log ( `${ pos . coords . latitude } ${ pos . coords . longitude } ` ) ;
53
- } ) ;
54
- } else {
55
- threex . fakeGps ( - 0.72 , 51.05 ) ;
27
+ let fake = null ;
28
+ let first = true ;
56
29
30
+ // Uncomment to use a fake GPS location
31
+ // fake = { lat: 51.05, lon : -0.72 };
32
+ if ( fake ) {
33
+ setupObjects ( fake . lon , fake . lat ) ;
34
+ threex . fakeGps ( fake . lon , fake . lat ) ;
35
+ } else {
57
36
threex . on ( "gpsupdate" , pos => {
58
- console . log ( `${ pos . coords . latitude } ${ pos . coords . longitude } ` ) ;
37
+ if ( first ) {
38
+ setupObjects ( pos . coords . longitude , pos . coords . latitude ) ;
39
+ first = false ;
40
+ }
59
41
} ) ;
60
- }
42
+ threex . startGps ( ) ;
43
+ }
61
44
62
45
requestAnimationFrame ( render ) ;
63
46
47
+
48
+
49
+ let mousedown = false , lastX = 0 ;
50
+
51
+ // Mouse events for testing on desktop machine
52
+ if ( ! isMobile ( ) ) {
53
+ window . addEventListener ( "mousedown" , e => {
54
+ mousedown = true ;
55
+ } ) ;
56
+
57
+ window . addEventListener ( "mouseup" , e => {
58
+ mousedown = false ;
59
+ } ) ;
60
+
61
+ window . addEventListener ( "mousemove" , e => {
62
+ if ( ! mousedown ) return ;
63
+ if ( e . clientX < lastX ) {
64
+ camera . rotation . y -= oneDegAsRad ;
65
+ if ( camera . rotation . y < 0 ) {
66
+ camera . rotation . y += 2 * Math . PI ;
67
+ }
68
+ } else if ( e . clientX > lastX ) {
69
+ camera . rotation . y += oneDegAsRad ;
70
+ if ( camera . rotation . y > 2 * Math . PI ) {
71
+ camera . rotation . y -= 2 * Math . PI ;
72
+ }
73
+ }
74
+ lastX = e . clientX ;
75
+ } ) ;
76
+ }
77
+
64
78
function render ( time ) {
65
79
resizeUpdate ( ) ;
66
80
if ( orientationControls ) orientationControls . update ( ) ;
@@ -78,3 +92,15 @@ function resizeUpdate() {
78
92
camera . aspect = canvas . clientWidth / canvas . clientHeight ;
79
93
camera . updateProjectionMatrix ( ) ;
80
94
}
95
+
96
+ function setupObjects ( longitude , latitude ) {
97
+ // Use position of first GPS update (fake or real)
98
+ const material = new THREE . MeshBasicMaterial ( { color : 0xff0000 } ) ;
99
+ const material2 = new THREE . MeshBasicMaterial ( { color : 0xffff00 } ) ;
100
+ const material3 = new THREE . MeshBasicMaterial ( { color : 0x0000ff } ) ;
101
+ const material4 = new THREE . MeshBasicMaterial ( { color : 0x00ff00 } ) ;
102
+ threex . add ( new THREE . Mesh ( geom , material ) , longitude , latitude + 0.001 ) ; // slightly north
103
+ threex . add ( new THREE . Mesh ( geom , material2 ) , longitude , latitude - 0.001 ) ; // slightly south
104
+ threex . add ( new THREE . Mesh ( geom , material3 ) , longitude - 0.001 , latitude ) ; // slightly west
105
+ threex . add ( new THREE . Mesh ( geom , material4 ) , longitude + 0.001 , latitude ) ; // slightly east
106
+ }
0 commit comments