This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
threejs_camera-animation.ts
151 lines (136 loc) · 4.82 KB
/
threejs_camera-animation.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
/*
* 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 { GeoCoordinates } from "@here/harp-geoutils";
import { MapView, MapViewEventNames, MapViewUtils } from "@here/harp-mapview";
import * as THREE from "three";
import { HelloWorldExample } from "./getting-started_hello-world_npm";
/**
* This example builds on top of the [[HelloWorldExample]], so please consult that first for
* any questions regarding basic setup of the map.
*
* This example shows how to use [[MapViewUtils]] to compute camera positions and orientations
* and use them to animate them with [THREE.js](https://threejs.org/).
*
* [CatmulRomCurve3](https://threejs.org/docs/index.html#api/en/extras/curves/CatmullRomCurve3) is
* used to interpolate between two camera positions and
* [Quaternion](https://threejs.org/docs/index.html#api/en/math/Quaternion.slerp) is used to
* interpolate between two camera orientations.
*/
export namespace ThreejsCameraAnimation {
interface Location {
target: GeoCoordinates;
tilt: number;
heading: number;
distance: number;
}
let currentLocation = 0;
const locations: Location[] = [
{
// HERE Berlin
target: new GeoCoordinates(52.5308419, 13.3850719),
tilt: 0,
heading: 0,
distance: 1000
},
{
// Museumsinsel Berlin
target: new GeoCoordinates(52.5169285, 13.4010829),
tilt: 45,
heading: 45,
distance: 300
},
{
// TV-Tower Berlin
target: new GeoCoordinates(52.520836, 13.409401, 300),
tilt: 45,
heading: 180,
distance: 500
}
];
const message = document.createElement("div");
const threejsLink = "https://threejs.org";
message.innerHTML = `Example showing camera animations using
<a href="${threejsLink}">three.js</a>
<br>
Tap our use left/right keys to change location`;
message.style.cssText = `
color: #000;
width: 80%;
left: 50%;
position: relative;
margin-left: -40%;
font-size: 15px;
`;
document.body.appendChild(message);
function startTransition(mapView: MapView, location: Location) {
const startPosition = mapView.camera.position.clone();
const startQuaternion = mapView.camera.quaternion.clone();
const targetPosition = MapViewUtils.getCameraPositionFromTargetCoordinates(
location.target,
location.distance,
location.heading,
location.tilt,
mapView.projection
);
const targetQuaternion = MapViewUtils.getCameraRotationAtTarget(
mapView.projection,
location.target,
location.heading,
location.tilt
);
const startTime = Date.now();
const curve = new THREE.CatmullRomCurve3([startPosition, targetPosition]);
const updateListener = () => {
const time = Date.now();
let t = (time - startTime) / 1000;
if (t >= 1) {
t = 1;
mapView.endAnimation();
mapView.removeEventListener(MapViewEventNames.Render, updateListener);
}
mapView.camera.position.copy(curve.getPoint(t));
const rotation = startQuaternion.clone().slerp(targetQuaternion, t);
mapView.camera.quaternion.copy(rotation);
mapView.camera.updateMatrixWorld(true);
};
mapView.addEventListener(MapViewEventNames.Render, updateListener);
mapView.beginAnimation();
}
startTransition(HelloWorldExample.mapView, locations[0]);
window.ontouchend = (ev: TouchEvent) => {
const oldLocation = locations[currentLocation];
currentLocation++;
if (currentLocation >= locations.length) {
currentLocation = 0;
}
const newLocation = locations[currentLocation];
if (oldLocation === newLocation) {
return;
}
startTransition(HelloWorldExample.mapView, newLocation);
};
window.onkeydown = (ev: KeyboardEvent) => {
const oldLocation = locations[currentLocation];
switch (ev.code) {
case "ArrowLeft":
currentLocation--;
break;
case "ArrowRight":
currentLocation++;
break;
}
if (currentLocation < 0) {
currentLocation = locations.length - 1;
} else if (currentLocation >= locations.length) {
currentLocation = 0;
}
const newLocation = locations[currentLocation];
if (oldLocation === newLocation) {
return;
}
startTransition(HelloWorldExample.mapView, newLocation);
};
}