Skip to content

Commit a26ff06

Browse files
authored
Merge pull request #409 from AR-js-org/three-location-based-example-fix
Three location based example fix
2 parents 2e6cf3f + da4a2cd commit a26ff06

File tree

1 file changed

+60
-34
lines changed
  • three.js/examples/location-based

1 file changed

+60
-34
lines changed

three.js/examples/location-based/index.js

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,70 @@ const camera = new THREE.PerspectiveCamera(80, 2, 0.1, 50000);
1111
const renderer = new THREE.WebGLRenderer({ canvas: document.querySelector('#canvas1') });
1212

1313
const geom = new THREE.BoxGeometry(20,20,20);
14-
const material = new THREE.MeshBasicMaterial({color: 0xff0000});
15-
const mesh = new THREE.Mesh(geom, material);
1614

1715
const threex = new THREEx.LocationBased(scene, camera);
1816
const cam = new THREEx.WebcamRenderer(renderer, '#video1');
1917

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+
2820

2921
let orientationControls;
3022

3123
if (isMobile()){
3224
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+
}
5026

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;
5629

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 {
5736
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+
}
5941
});
60-
}
42+
threex.startGps();
43+
}
6144

6245
requestAnimationFrame(render);
6346

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+
6478
function render(time) {
6579
resizeUpdate();
6680
if(orientationControls) orientationControls.update();
@@ -78,3 +92,15 @@ function resizeUpdate() {
7892
camera.aspect = canvas.clientWidth / canvas.clientHeight;
7993
camera.updateProjectionMatrix();
8094
}
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

Comments
 (0)