-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile.js
More file actions
371 lines (309 loc) · 10.9 KB
/
mobile.js
File metadata and controls
371 lines (309 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"use strict";
// mobile.js
// (C) 2018 Minoru Akagi | MIT License
// https://github.com/minorua/Qgis2threejs
Q3D.Config.AR = {
DH: 1.5, // device height from ground (in CRS vertical unit)
FOV: 70, // device camera's field of view
MND: 0 // magnetic North direction (clockwise from upper direction of map, in degrees)
};
var app = Q3D.application,
ARMode = false;
var orbitControls, devControls, oldFOV;
app.start = function () {
if (ARMode) devControls.connect();
else orbitControls.enabled = true;
};
app.pause = function () {
if (ARMode) devControls.disconnect();
else orbitControls.enabled = false;
};
app.resume = function () {
if (ARMode) devControls.connect();
else orbitControls.enabled = true;
};
app.eventListener.resize = function () {
var width, height;
if (ARMode) {
var v = document.getElementById("video"),
asp = window.innerWidth / window.innerHeight,
vasp = v.videoWidth / v.videoHeight;
if (vasp > asp) {
width = window.innerWidth;
height = parseInt(width / vasp);
}
else {
height = window.innerHeight;
width = parseInt(height * vasp);
}
}
else {
width = window.innerWidth;
height = window.innerHeight;
}
app.setCanvasSize(width, height);
app.render();
};
app.cameraAction._move = app.cameraAction.move;
app.cameraAction.move = function () {
app.cameraAction._move(app.queryTargetPosition.x,
app.queryTargetPosition.y,
app.queryTargetPosition.z + Q3D.Config.AR.DH * app.scene.userData.zScale); // + device height from ground
};
app._setRotateAnimationMode = app.setRotateAnimationMode;
app.setRotateAnimationMode = function (enabled) {
app._setRotateAnimationMode(enabled);
document.getElementById("stop-button").style.display = (enabled) ? "block" : "none";
};
function init() {
orbitControls = app.controls;
devControls = new THREE.DeviceOrientationControls(app.camera);
devControls.alphaOffset = -Q3D.Config.AR.MND * Math.PI / 180; // counter-clockwise, in radians
// store default camera FOV (non-AR mode)
oldFOV = app.camera.fov;
// load settings from local storage
try {
var data = JSON.parse(localStorage.getItem("Qgis2threejs"));
if (data) {
Q3D.Config.AR.FOV = data.fov;
}
}
catch (e) {
console.log(e);
}
// add event listeners
// AR mode switch
document.getElementById("ar-checkbox").addEventListener("change", function () {
if (this.checked) startARMode();
else stopARMode();
});
// current location button
document.getElementById("current-location").addEventListener("click", function () {
if (ARMode) moveToCurrentLocation();
else zoomToCurrentLocation();
});
// layers button
document.getElementById("layers-button").addEventListener("click", function () {
var hidden = document.getElementById("layerlist").classList.contains("hidden");
hideAll();
if (hidden) {
document.getElementById("layerlist").classList.remove("hidden");
document.getElementById("layers-button").classList.add("pressed");
}
});
// settings button
document.getElementById("settings-button").addEventListener("click", function () {
var fov = document.getElementById("fov");
var hidden = document.getElementById("settings").classList.contains("hidden");
hideAll();
if (hidden) {
fov.value = Q3D.Config.AR.FOV;
document.getElementById("settings").classList.remove("hidden");
document.getElementById("settings-button").classList.add("pressed");
}
});
document.getElementById("settings-ok").addEventListener("click", function () {
Q3D.Config.AR.FOV = document.getElementById("fov").value;
if (ARMode) {
app.camera.fov = Q3D.Config.AR.FOV;
app.camera.updateProjectionMatrix();
}
hideAll();
// save settings in local storage
try {
if (document.getElementById("save-in-storage").checked) {
var data = {
fov: Q3D.Config.AR.FOV
};
localStorage.setItem("Qgis2threejs", JSON.stringify(data));
}
}
catch (e) {
console.log(e);
}
});
document.getElementById("settings-cancel").addEventListener("click", function () {
hideAll();
});
// information (about) button
document.getElementById("info-button").addEventListener("click", function () {
var active = document.getElementById("info-button").classList.contains("pressed");
hideAll();
if (!active) {
app.showInfo();
document.getElementById("info-button").classList.add("pressed");
}
});
// stop button
document.getElementById("stop-button").addEventListener("click", function () {
app.setRotateAnimationMode(false);
});
}
function initLayerList(scene) {
var list = document.getElementById("layerlist");
Object.keys(scene.mapLayers).forEach(function (layerId) {
var layer = scene.mapLayers[layerId];
var item = document.createElement("div");
item.innerHTML = "<div><input type='checkbox'" +
((layer.properties.visible) ? " checked" : "") +
">" + layer.properties.name + "</div><div><input type='range'><span></span></div>";
// visibility checkbox
item.querySelector("input[type=checkbox]").addEventListener("change", function () {
layer.visible = this.checked;
});
// opacity slider
var slider = item.querySelector("input[type=range]"),
label = item.querySelector("span"),
o = parseInt(layer.opacity * 100);
slider.value = o;
slider.addEventListener("input", function () {
label.innerHTML = this.value + " %";
});
slider.addEventListener("change", function () {
label.innerHTML = this.value + " %";
layer.opacity = this.value / 100;
});
label.innerHTML = o + " %";
list.appendChild(item);
});
}
function startARMode(position) {
ARMode = true;
app.camera.fov = Q3D.Config.AR.FOV;
app.camera.updateProjectionMatrix();
if (typeof position === "undefined") {
app.camera.position.set(0, 0, 30);
document.getElementById("current-location").classList.add("touchme");
}
else {
app.camera.position.copy(position);
}
if (Q3D.Config.bgColor !== null) {
app.renderer.setClearColor(0, 0);
}
if (orbitControls.autoRotate) {
app.setRotateAnimationMode(false);
}
orbitControls.enabled = false;
app.controls = devControls;
app.controls.connect();
app.startAnimation();
navigator.mediaDevices.getUserMedia({video: {facingMode: "environment"}}).then(function (stream) {
var v = document.getElementById("video");
v.addEventListener("loadedmetadata", function () {
app.eventListener.resize();
});
v.srcObject = stream;
document.getElementById("view").classList.add("transparent");
}).catch(function (error) {
alert(error);
});
document.querySelectorAll(".action-move").forEach(function (elm) {
elm.classList.toggle("hidden");
});
document.querySelector(".action-zoom").classList.add("hidden");
document.querySelector(".action-orbit").classList.add("hidden");
}
function startARModeHere() {
var vec3 = new THREE.Vector3();
vec3.copy(app.queryTargetPosition);
vec3.z += Q3D.Config.AR.DH * app.scene.userData.zScale;
startARMode(vec3);
document.getElementById("ar-checkbox").checked = true;
}
function moveHere() {
app.camera.position.copy(app.queryTargetPosition);
app.camera.position.z += Q3D.Config.AR.DH * app.scene.userData.zScale;
}
function stopARMode() {
ARMode = false;
devControls.disconnect();
app.controls = orbitControls;
app.controls.enabled = true;
app.stopAnimation();
document.getElementById("current-location").classList.remove("touchme");
var v = Q3D.Config.viewpoint,
p = v.pos,
t = v.lookAt;
app.camera.position.set(p.x, p.y, p.z);
app.camera.lookAt(t.x, t.y, t.z);
app.controls.target.set(t.x, t.y, t.z);
var v = document.getElementById("video");
v.srcObject = null;
document.getElementById("view").classList.remove("transparent");
app.camera.fov = oldFOV;
app.camera.updateProjectionMatrix();
app.setCanvasSize(window.innerWidth, window.innerHeight);
if (Q3D.Config.bgColor !== null) app.renderer.setClearColor(Q3D.Config.bgColor || 0, 1);
document.querySelectorAll(".action-move").forEach(function (elm) {
elm.classList.toggle("hidden");
});
document.querySelector(".action-zoom").classList.remove("hidden");
document.querySelector(".action-orbit").classList.remove("hidden");
}
function getCurrentPosition (callback) {
app.popup.show("Fetching current location...");
navigator.geolocation.getCurrentPosition(function (position) {
// error message if failed to get current position
var pos = position.coords;
if (pos.longitude === undefined || pos.latitude === undefined || pos.altitude === undefined) {
app.popup.show("Could not fetch current location.", "", false, 3000);
return;
}
// get z coordinate of current location from DEM layer if scene has a DEM layer
var layer, pt = app.scene.toWorldCoordinates(pos.longitude, pos.latitude, pos.altitude, true);
for (var lyrId in app.scene.mapLayers) {
layer = app.scene.mapLayers[lyrId];
if (layer instanceof Q3D.DEMLayer) {
var z = layer.getZ(pt.x, pt.y);
if (z !== null) {
pt.z = (z + Q3D.Config.AR.DH + app.scene.userData.zShift) * app.scene.userData.zScale;
break;
}
}
}
callback(pt);
var acc = Number.parseFloat(pos.accuracy);
acc = (acc > 2) ? acc.toFixed(0) : acc.toFixed(1);
var msg = "Accuracy: <span class='accuracy'>" + acc + "</span>m";
app.popup.show(msg, "Current location", false, 5000);
},
function (error) {
app.popup.hide();
alert("Cannot get current location: " + error.message);
},
{enableHighAccuracy: true});
}
function moveToCurrentLocation() {
// AR mode is on
document.getElementById("current-location").classList.remove("touchme");
getCurrentPosition(function (pt) {
// move camera
app.cameraAction.move(pt.x, pt.y, pt.z);
});
}
function zoomToCurrentLocation() {
// AR mode is off
getCurrentPosition(function (pt) {
// indicate current position using query marker
app.queryMarker.position.set(pt.x, pt.y, pt.z);
app.queryMarker.visible = true;
app.queryMarker.updateMatrixWorld();
// zoom in on current position
app.cameraAction.zoomIn(pt.x, pt.y, pt.z);
});
}
// layers, settings and info buttons
function hideAll() {
document.getElementById("layerlist").classList.add("hidden");
document.getElementById("layers-button").classList.remove("pressed");
document.getElementById("settings").classList.add("hidden");
document.getElementById("settings-button").classList.remove("pressed");
app.popup.hide();
document.getElementById("info-button").classList.remove("pressed");
}
app.popup._hide = app.popup.hide;
app.popup.hide = function () {
document.getElementById("info-button").classList.remove("pressed");
app.popup._hide();
};