This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
getting-started_free-camera.ts
305 lines (262 loc) · 11.4 KB
/
getting-started_free-camera.ts
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
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import { DebugTileDataSource } from "@here/harp-debug-datasource";
import { GeoCoordinates, MathUtils, webMercatorTilingScheme } from "@here/harp-geoutils";
import { MapControls } from "@here/harp-map-controls";
import {
CopyrightElementHandler,
CopyrightInfo,
MapView,
MapViewEventNames,
MapViewOptions,
MapViewUtils
} from "@here/harp-mapview";
import { APIFormat, OmvDataSource } from "@here/harp-omv-datasource";
import * as THREE from "three";
import { accessToken } from "../config";
// Import the gesture handlers from the three.js additional libraries.
// The controls are not in common.js they explictly require a
// global instance of THREE and they must be imported only for their
// side effect.
import "three/examples/js/controls/TrackballControls";
import "three/examples/js/controls/TransformControls";
/**
* This app adds another freely moveable camera into the map view scene.
* It can be used as a handy map inspection/debugging tool
* easily enabling visual checks of the map rendering with different camera settings.
*
* The app enables to change the position of the camera: translate it, rotate it
* as well as change the point of view (to the one the user would actually see).
*
* ```typescript
* [[include:harp_gl_freecamera_app_0.ts]]
* ```
*
*/
export namespace FreeCameraAppDebuggingToolExample {
interface Helper extends THREE.Object3D {
update(): void;
}
interface FreeCameraAppOptions extends MapViewOptions {
geoCenter?: GeoCoordinates;
decoderUrl: string;
}
/**
* [[FreeCameraApp]] class adds a debug camera view which enables to see the rendered map view
* from a third person perspective as well as allows to freely modify the debug camera
* position/rotation.
*
* The parameters of the [[FreeCameraApp]] are set in the [[FreeCameraAppOptions]] object.
*
* ```typescript
* [[include:harp_gl_freecamera_app_0.ts]]
* ```
*
*/
export class FreeCameraApp {
private readonly mapView: MapView;
private readonly mapControls: MapControls;
private readonly helpers: Helper[] = [];
// creates a new MapView for the HTMLCanvasElement of the given id
constructor(readonly options: FreeCameraAppOptions) {
this.mapView = new MapView(options);
this.mapView.fog.enabled = false;
this.mapControls = new MapControls(this.mapView);
this.mapControls.enabled = false;
CopyrightElementHandler.install("copyrightNotice", this.mapView);
// center the camera somewhere around Berlin geo locations
if (options.geoCenter !== undefined) {
this.mapView.geoCenter = options.geoCenter;
}
// resize the mapView to maximum
this.mapView.resize(window.innerWidth, window.innerHeight);
// react on resize events
window.addEventListener("resize", () => {
this.mapView.resize(window.innerWidth, window.innerHeight);
});
}
/**
* Attaches the [[OmvDataSource]] and [[DebugTileDataSource]] to the map as well as
* initializes the debug view (making the: `R`, `T` and `V` keys modify the camera's current
* rotation (`R`), translation/postion (`T`) and changing the camera view to the one the
* user is seeing (`V`).
*/
start() {
const hereCopyrightInfo: CopyrightInfo = {
id: "here.com",
year: new Date().getFullYear(),
label: "HERE",
link: "https://legal.here.com/terms"
};
const copyrights: CopyrightInfo[] = [hereCopyrightInfo];
const omvDataSource = new OmvDataSource({
baseUrl: "https://xyz.api.here.com/tiles/herebase.02",
apiFormat: APIFormat.XYZOMV,
styleSetName: "tilezen",
maxZoomLevel: 17,
authenticationCode: accessToken,
copyrightInfo: copyrights
});
const debugTileDataSource = new DebugTileDataSource(webMercatorTilingScheme);
this.mapView.addDataSource(omvDataSource);
this.mapView.addDataSource(debugTileDataSource);
this.initializeDebugView();
}
private initializeDebugView() {
const pointOfView = new THREE.PerspectiveCamera(
this.mapView.camera.fov,
this.mapView.canvas.width / this.mapView.canvas.height,
0.1,
4000000
); // use an arbitrary large distance for the far plane.
this.mapView.scene.add(pointOfView);
// Setup relative to eye camera which is actually used in
// map view to render tiles thus increasing data accuracy.
const cameraRelativeToEye = new THREE.PerspectiveCamera();
this.mapView.scene.add(cameraRelativeToEye);
pointOfView.position.set(0, -1500, 1500);
this.mapView.pointOfView = pointOfView;
const transformControls = new (THREE as any).TransformControls(
pointOfView,
this.mapView.canvas
);
transformControls.setSpace("world");
transformControls.attach(cameraRelativeToEye);
const applyTransformControls = () => {
// Apply helper camera offset to main (map view) camera.
this.mapView.camera.position.add(cameraRelativeToEye.position);
// Make sure that pitch limit contraint is preserved
const ypr = MapViewUtils.extractYawPitchRoll(cameraRelativeToEye.quaternion);
ypr.pitch = Math.max(
Math.min(ypr.pitch, MathUtils.degToRad(this.mapControls.maxPitchAngle)),
0
);
// Finally apply rotation from transformation gizmo.
this.mapControls.setRotation(
MathUtils.radToDeg(ypr.yaw),
MathUtils.radToDeg(ypr.pitch)
);
// Reset RTE camera orientation according to constraints applied.
cameraRelativeToEye.copy(this.mapView.camera);
// Reset RTE camera position to origin.
cameraRelativeToEye.position.setScalar(0);
transformControls.update();
};
applyTransformControls();
const applyMapControls = () => {
cameraRelativeToEye.copy(this.mapView.camera, true);
cameraRelativeToEye.position.setScalar(0);
transformControls.update();
};
transformControls.addEventListener("mouseDown", () => {
trackball.enabled = false;
});
transformControls.addEventListener("mouseUp", () => {
trackball.enabled = true;
});
transformControls.addEventListener("objectChange", () => {
applyTransformControls();
this.mapView.update();
});
this.mapView.scene.add(transformControls);
const cameraHelper = new THREE.CameraHelper(cameraRelativeToEye);
// Set the renderOrder to an arbitrary large number, just to be sure that the camera
// helpers are rendered on top of the map objects.
cameraHelper.renderOrder = 5000;
this.mapView.scene.add(cameraHelper);
this.helpers.push(cameraHelper);
// Set up the trackball gesture handler
const trackball = new (THREE as any).TrackballControls(
pointOfView,
this.mapView.canvas
);
trackball.staticMoving = true;
trackball.rotateSpeed = 3.0;
trackball.zoomSpeed = 4.0;
trackball.panSpeed = 2.0;
trackball.addEventListener("start", () => {
this.mapView.beginAnimation();
});
trackball.addEventListener("end", () => {
this.mapView.endAnimation();
});
// Update the debug controls.
this.mapView.addEventListener(MapViewEventNames.Render, () => {
trackball.update();
this.helpers.forEach(helper => helper.update());
});
window.focus();
window.addEventListener("resize", () => {
const { width, height } = this.mapView.canvas;
pointOfView.aspect = width / height;
pointOfView.updateProjectionMatrix();
this.mapView.update();
});
window.addEventListener("keydown", event => {
switch (event.code) {
case "KeyT":
transformControls.setMode("translate");
// Allow translations at any axis.
transformControls.showX = true;
transformControls.showY = true;
transformControls.showZ = true;
this.mapView.update();
break;
case "KeyR":
transformControls.setMode("rotate");
// Only pitch and yaw may be adjusted.
transformControls.showX = true;
transformControls.showY = false;
transformControls.showZ = true;
this.mapView.update();
break;
case "KeyV":
if (this.mapView.pointOfView !== undefined) {
this.mapView.pointOfView = undefined;
this.mapControls.enabled = true;
transformControls.enabled = false;
trackball.enabled = false;
applyTransformControls();
} else {
this.mapView.pointOfView = pointOfView;
this.mapControls.enabled = false;
transformControls.enabled = true;
trackball.enabled = true;
applyMapControls();
}
this.mapView.update();
break;
default:
break;
} // switch
});
}
}
function main() {
const message = document.createElement("div");
message.innerHTML = `
Press 'R' to rotate<br>
Press 'T' to translate<br>
Press 'V' to change the scene point of view<br>`;
message.style.position = "absolute";
message.style.cssFloat = "right";
message.style.top = "10px";
message.style.right = "10px";
document.body.appendChild(message);
const canvas = document.getElementById("mapCanvas") as HTMLCanvasElement;
const geoCenter = new GeoCoordinates(52.518611, 13.376111);
// snippet:harp_gl_freecamera_app_0.ts
const app = new FreeCameraApp({
decoderUrl: "./decoder.bundle.js",
canvas,
theme: "./resources/berlin_tilezen_base.json",
geoCenter
});
app.start();
// end:harp_gl_freecamera_app_0.ts
}
main();
}