forked from peterqliu/threebox
-
Notifications
You must be signed in to change notification settings - Fork 150
/
19-fixedzoom.html
365 lines (333 loc) · 7.43 KB
/
19-fixedzoom.html
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
<!doctype html>
<head>
<title>Threebox fixed zoom</title>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id='map' class='map'></div>
<script type="module">
if (!config) console.error("Config not set! Make a copy of 'config_template.js', add in your access token, and save the file as 'config.js'.");
mapboxgl.accessToken = config.accessToken;
let mapConfig = {
MAD: {
origin: [-3.460539968876, 40.4849214450, 0], destination: [-3.378467560041173, 40.551832030917126, 1500], center: [-3.44885, 40.49198, 0], zoom: 13.4, pitch: 50, bearing: -13, scale: 1, timezone: 'Europe/Madrid'
},
names: {
customLayer: "custom-layer"
}
}
let map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-v9',
center: mapConfig.MAD.center,
zoom: mapConfig.MAD.zoom,
pitch: mapConfig.MAD.pitch,
antialias: true,
bearing: mapConfig.MAD.bearing
});
// we can add Threebox to mapbox to add built-in mouseover/mouseout and click behaviors
window.tb = new Threebox(
map,
map.getCanvas().getContext('webgl'),
{
realSunlight: true,
enableSelectingObjects: true, //change this to false to disable 3D objects selection
enableTooltips: true, // change this to false to disable default tooltips on fill-extrusion and 3D models
}
);
tb.altitudeStep = 1;
tb.setSunlight(new Date(2021, 0, 18, 12));
import { GUI } from 'https://threejs.org/examples/jsm/libs/dat.gui.module.js';
import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
let stats, gui;
function animate() {
requestAnimationFrame(animate);
stats.update();
}
let plane;
let api = {
fixedZoom: true,
pan: true,
maxZoom: 15
};
map.on('style.load', function () {
init();
map.addLayer({
id: 'custom_layer',
type: 'custom',
renderingMode: '3d',
onAdd: function (map, mbxContext) {
// Creative Commons License attribution: Plane model by https://sketchfab.com/ideehochzwei
// from https://sketchfab.com/3d-models/plane-aa001f5a88f64b16b98356c042f2d5f3
let options = {
obj: './models/plane/plane.glb',
type: 'gltf',
scale: mapConfig.MAD.scale,
rotation: { x: 90, y: 0, z: 0 },
anchor: 'center',
bbox: false
}
if (api.fixedZoom) options.fixedZoom = api.maxZoom;
tb.loadObj(options, function (model) {
plane = model
.setCoords(mapConfig.MAD.origin);
plane.setRotation({ x: 0, y: 0, z: 135 })
plane.addTooltip("You can set the fixed scale of this plane", true);
plane.addEventListener('ObjectChanged', onObjectChanged, false);
plane.castShadow = true;
tb.add(plane);
fly(flightPlan);
//setTimeout(() => {
// let opt = {
// coords: mapConfig.MAD.destination, duration: 20000
// };
// plane.set(opt)
//}, 3000);
})
},
render: function (gl, matrix) {
tb.update();
}
});
});
function init() {
// stats
stats = new Stats();
map.getContainer().appendChild(stats.dom);
animate();
// gui
gui = new GUI();
// this will define if there's a fixed zoom level for the model
gui.add(api, 'fixedZoom').name('fixed zoom').onChange(changeScale);
gui.add(api, 'pan').name('pan').onChange(changeScale);
gui.add(api, 'maxZoom', 0, map.transform.maxZoom).step(0.5).onChange(changeScale);
}
function onObjectChanged(e) {
let model = e.detail.object; //here's the object already modified
if (api.pan) map.panTo(model.coordinates);
}
function changeScale() {
plane.fixedZoom = (api.fixedZoom ? api.maxZoom : null);
plane.setObjectScale(map.transform.scale);
tb.map.repaint = true;
}
let line;
function fly(data) {
// extract path geometry from callback geojson, and set duration of travel
var options = {
path: data.geometry.coordinates,
duration: 40000
}
// start the truck animation with above options, and remove the line when animation ends
plane.followPath(
options,
function () {
tb.remove(line);
}
);
// set up geometry for a line to be added to map, lofting it up a bit for *style*
let lineGeometry = options.path;
// create and add line object
line = tb.line({
geometry: lineGeometry,
width: 5,
color: 'steelblue'
})
tb.add(line, mapConfig.names.customLayer);
}
let flightPlan = {
"geometry": {
"coordinates": [
[
-3.459164318324355,
40.483196679459695,
0
],
[
-3.46032158100065006,
40.48405772625512,
0
],
[
-3.4601480276212726,
40.48464924045098,
0
],
[
-3.4605399688768728,
40.48492144503072,
0
],
[
-3.4544247306827174,
40.489871726679894,
0
],
[
-3.4419511970175165,
40.49989552385142,
100
],
[
-3.4199262740950473,
40.51776139362727,
800
],
[
-3.4064155093898023,
40.52744748436612,
1000
],
[
-3.394276165400413,
40.53214151673197,
1400
],
[
-3.3774962506359145,
40.53130304189972,
1800
],
[
-3.35977648690141,
40.523996322867305,
2000
],
[
-3.3492733309630296,
40.51239798757899,
1000
],
[
-3.345716577158697,
40.494919870461985,
1000
],
[
-3.351353597163751,
40.4797236141558,
1000
],
[
-3.3787722011184655,
40.45432754114316,
1000
],
[
-3.4223595762896935,
40.41937230956262,
1000
],
[
-3.444433667203299,
40.40449665396977,
1000
],
[
-3.4678526394398546,
40.39535552525871,
1000
],
[
-3.4864554257066516,
40.39245520592732,
1000
],
[
-3.503812672766088,
40.39513567933946,
1000
],
[
-3.5170856837534643,
40.40280870363367,
1000
],
[
-3.526080266123671,
40.41452098042856,
1000
],
[
-3.5294395670147196,
40.42627781810833,
1000
],
[
-3.5263900139946713,
40.43272665526561,
1000
],
[
-3.520955322876091,
40.43652271714541,
900
],
[
-3.512454752467022,
40.442395503099675,
600
],
[
-3.496113157862709,
40.45605326123382,
400
],
[
-3.4802314192833705,
40.46895283940685,
200
],
[
-3.4673761065382394,
40.47937019244051,
100
],
[
-3.4611694105603874,
40.4843367730719,
0
],
[
-3.460447314584286,
40.48495391198887,
0
],
[
-3.460162097548647,
40.48469346302471,
0
],
[
-3.460400363301318,
40.48398852655413,
0
],
[
-3.4591431034406526,
40.48323937836338,
0
]
],
"type": "LineString"
},
"type": "Feature",
"properties": {}
}
</script>
</body>