forked from peterqliu/threebox
-
Notifications
You must be signed in to change notification settings - Fork 148
/
05-logistics.html
178 lines (145 loc) · 4.28 KB
/
05-logistics.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
<!doctype html>
<head>
<title>Animated truck</title>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
background: black;
}
#map {
width: 100%;
height: 100%;
}
#explainer {
z-index: 99;
}
</style>
</head>
<body>
<div id='map' class='map'></div>
<div id='explainer'>Click on the map to drive the truck there</div>
<script type="module">
// This example downloads a truck model from an external OBJ/MTL file, adds it to the map, and drives it around via paths fetched from the Mapbox Directions API
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;
var origin = [-122.4340, 37.7353];
var destination, line;
var truck;
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center: origin,
zoom: 16,
pitch: 60,
bearing: 90
});
let stats;
import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
function animate() {
requestAnimationFrame(animate);
stats.update();
}
map.on('style.load', function () {
// stats
stats = new Stats();
map.getContainer().appendChild(stats.dom);
animate();
map
.addLayer({
id: 'custom_layer',
type: 'custom',
renderingMode: '3d',
onAdd: function (map, mbxContext) {
window.tb = new Threebox(
map,
mbxContext,
{
defaultLights: true
}
);
// Royalty Free License: Vehicles by https://www.cgtrader.com/antonmoek
// from https://www.cgtrader.com/free-3d-models/car/concept/cartoon-low-poly-city-cars-pack
var options = {
type: 'gltf',
obj: 'models/vehicles/truck.glb',
scale: 40,
units: 'meters',
anchor: "bottom",
rotation: { x: 90, y: 90, z: 0 }, //rotation to postiion the truck and heading properly
}
tb.loadObj(options, function (model) {
truck = model.setCoords(origin);
truck.addEventListener('ObjectChanged', onObjectChanged, false);
tb.add(truck);
})
},
render: function (gl, matrix) {
tb.update();
}
});
})
.on('click', function (e) {
var pt = [e.lngLat.lng, e.lngLat.lat];
travelPath(pt);
})
function onObjectChanged(e) {
let model = e.detail.object; //here's the object already modified
let action = e.detail.action; //here's the action that changed the object
console.log(action);
}
function travelPath(destination) {
// request directions. See https://docs.mapbox.com/api/navigation/#directions for details
var url = "https://api.mapbox.com/directions/v5/mapbox/driving/" + [origin, destination].join(';') + "?geometries=geojson&access_token=" + config.accessToken
fetchFunction(url, function (data) {
// extract path geometry from callback geojson, and set duration of travel
var options = {
path: data.routes[0].geometry.coordinates,
duration: 10000
}
// start the truck animation with above options, and remove the line when animation ends
truck.followPath(
options,
function () {
tb.remove(line);
}
);
// set up geometry for a line to be added to map, lofting it up a bit for *style*
var lineGeometry = options.path
.map(function (coordinate) {
return coordinate.concat([15])
})
// create and add line object
line = tb.line({
geometry: lineGeometry,
width: 5,
color: 'steelblue'
})
tb.add(line);
// set destination as the new origin, for the next trip
origin = destination;
})
}
//convenience function for fetch
function fetchFunction(url, cb) {
fetch(url)
.then(
function (response) {
if (response.status === 200) {
response.json()
.then(function (data) {
cb(data)
})
}
}
)
}
</script>
</body>