-
Notifications
You must be signed in to change notification settings - Fork 132
/
custom-linetrip.html
327 lines (299 loc) · 12.4 KB
/
custom-linetrip.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
<!DOCTYPE html>
<html>
<head>
<title>Customize your own components</title>
<script type="text/javascript" src="https://unpkg.com/[email protected]/build/dat.gui.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.js"></script>
<script type="text/javascript" src="https://unpkg.com/@maptalks/gl/dist/maptalksgl.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script type="text/javascript"
src="https://unpkg.com/[email protected]/examples/js/libs/stats.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/libs/lz-string.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/maptalks.three@latest/dist/maptalks.three.js"></script>
<script type="text/javascript" src="js/geoutil.js"></script>
<script type="text/javascript" src="buildings.js"></script>
<style>
html,
body {
margin: 0px;
height: 100%;
width: 100%;
}
#map {
width: 100%;
height: 100%;
background-color: #000;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new maptalks.Map("map", {
center: [13.416935229170008, 52.529564137540376],
zoom: 15,
pitch: 70,
bearing: 180,
centerCross: true,
doubleClickZoom: false,
// baseLayer: new maptalks.TileLayer('tile', {
// urlTemplate: 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
// subdomains: ['a', 'b', 'c', 'd'],
// attribution: '© <a href="http://osm.org">OpenStreetMap</a> contributors, © <a href="https://carto.com/">CARTO</a>'
// })
});
// features to draw
var features = [];
buildings.forEach(function (b) {
features = features.concat(b.features);
});
// the ThreeLayer to draw buildings
var threeLayer = new maptalks.ThreeLayer('t', {
forceRenderOnMoving: true,
forceRenderOnRotating: true,
// animation: true
});
var stats;
threeLayer.prepareToDraw = function (gl, scene, camera) {
stats = new Stats();
stats.domElement.style.zIndex = 100;
document.getElementById('map').appendChild(stats.domElement);
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, -10, 10).normalize();
scene.add(light);
var material = new THREE.MeshBasicMaterial({ color: '#3e35cf' });
var meshs = [];
var heightPerLevel = 10;
var polygons = features.map(f => {
const polygon = maptalks.GeoJSON.toGeometry(f);
var levels = f.properties.levels || 1;
polygon.setProperties({
height: heightPerLevel * levels,
});
return polygon;
});
const extrudePolygons = threeLayer.toExtrudePolygons(polygons, { topColor: '#fff', interactive: false }, material);
meshs.push(extrudePolygons);
threeLayer.addMesh(meshs);
addLines();
};
const sceneConfig = {
postProcess: {
enable: true,
antialias: { enable: true }
}
};
const groupLayer = new maptalks.GroupGLLayer('group', [threeLayer], { sceneConfig });
groupLayer.addTo(map);
var material = new THREE.LineBasicMaterial({
linewidth: 1,
color: 0x00ffff,
opacity: 0.8,
transparent: true
});
var lineMaterial = new THREE.LineBasicMaterial({
linewidth: 1,
color: 0xd3887,
opacity: 0.2,
transparent: true
});
var lineTrips;
function addLines() {
fetch('./data/berlin-roads.txt').then(function (res) {
return res.text();
}).then(function (geojson) {
geojson = LZString.decompressFromBase64(geojson);
geojson = JSON.parse(geojson);
var lineStrings = maptalks.GeoJSON.toGeometry(geojson);
var timer = 'generate line time';
console.time(timer);
var list = [];
lineStrings.forEach(lineString => {
list.push({
lineString,
len: lineLength(lineString)
});
});
list = list.sort(function (a, b) {
return b.len - a.len
});
var offset = 500;
lineTrips = list.slice(0, offset).map(d => {
var line = new LineTrip(d.lineString, {
chunkLength: d.len / 100,
speed: 1,
altitude: 2,
}, material, threeLayer)
return line;
});
var line = threeLayer.toLines(list.map(l => {
return l.lineString
}), { interactive: false }, lineMaterial);
console.log('lines.length:', lineTrips.length);
console.timeEnd(timer);
threeLayer.addMesh(line);
threeLayer.addMesh(lineTrips);
initGui();
animation();
})
}
function animation() {
// layer animation support Skipping frames
threeLayer._needsUpdate = !threeLayer._needsUpdate;
if (threeLayer._needsUpdate) {
threeLayer.redraw();
}
stats.update();
requestAnimationFrame(animation);
}
function initGui() {
var params = {
add: true,
color: material.color.getStyle(),
show: true,
opacity: 1,
altitude: 0
};
var gui = new dat.GUI();
gui.add(params, 'add').onChange(function () {
if (params.add) {
threeLayer.addMesh(lineTrips);
} else {
threeLayer.removeMesh(lineTrips);
}
});
gui.addColor(params, 'color').name('line color').onChange(function () {
material.color.set(params.color);
lineTrips.forEach(function (mesh) {
mesh.setSymbol(material);
});
});
gui.add(params, 'opacity', 0, 1).onChange(function () {
material.opacity = params.opacity;
lineTrips.forEach(function (mesh) {
mesh.setSymbol(material);
});
});
// gui.add(params, 'show').onChange(function () {
// lineTrips.forEach(function (mesh) {
// if (params.show) {
// mesh.show();
// } else {
// mesh.hide();
// }
// });
// });
gui.add(params, 'altitude', 0, 300).onChange(function () {
lineTrips.forEach(function (mesh) {
mesh.setAltitude(params.altitude);
});
});
}
//default values
var OPTIONS = {
chunkLength: 50,
speed: 1,
altitude: 0,
interactive: false
};
/**
* custom component
* */
class LineTrip extends maptalks.BaseObject {
constructor(lineString, options, material, layer) {
options = maptalks.Util.extend({}, OPTIONS, options, { layer, lineString });
super();
//Initialize internal configuration
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L135
this._initOptions(options);
const { altitude, chunkLength, speed } = options;
const chunkLines = lineSlice(lineString, chunkLength);
const centerPt = layer.coordinateToVector3(lineString.getCenter());
//cache position for faster computing,reduce double counting
const positionMap = {};
for (let i = 0, len = chunkLines.length; i < len; i++) {
const chunkLine = chunkLines[i];
for (let j = 0, len1 = chunkLine.length; j < len1; j++) {
const lnglat = chunkLine[j];
const key = lnglat.join(',').toString();
if (!positionMap[key]) {
positionMap[key] = layer.coordinateToVector3(lnglat).sub(centerPt);
}
}
}
let len = 0;
chunkLines.forEach(element => {
len += element.length;
});
len *= 3;
//generate geometry
const result = getChunkLinesPosition(chunkLines.slice(0, 1), layer, positionMap, centerPt);
const positions = result.positions;
const geometry = new THREE.BufferGeometry();
const ps = new Float32Array(len); // 3 vertices per point
geometry.addAttribute('position', new THREE.BufferAttribute(ps, 3).setDynamic(true));
setLineGeometryAttribute(geometry, positions.slice(0, 6));
this._createLine(geometry, material);
//set object3d position
const z = layer.altitudeToVector3(altitude, altitude).x;
const center = lineString.getCenter();
const v = layer.coordinateToVector3(center, z);
this.getObject3d().position.copy(v);
this._params = {
index: 0,
len: chunkLines.length,
chunkLines,
layer,
speed: Math.min(1, speed),
idx: 0,
positions: [],
positionMap,
centerPt
};
this._init();
}
_init() {
const { len, chunkLines, layer, positionMap, centerPt } = this._params;
for (let i = 0; i < len; i++) {
let ps = [];
if (i > 0) {
const prePs = this._params.positions[i - 1];
for (let j = 0, len1 = prePs.length; j < len1; j++) {
ps.push(prePs[j]);
}
const nextPs = getChunkLinesPosition([chunkLines[i]], layer, positionMap, centerPt).positions;
for (let j = 0, len1 = nextPs.length; j < len1; j++) {
ps.push(nextPs[j]);
}
} else {
const result = chunkLines.slice(0, i);
ps = getChunkLinesPosition(result, layer, positionMap, centerPt).positions;
}
this._params.positions[i] = ps;
}
}
_animation() {
const { index, positions, idx, speed, len, chunkLines, layer, positionMap, centerPt } = this._params;
const i = Math.round(index);
if (i > idx) {
this._params.idx++;
let ps = positions[i];
if (!ps) {
const result = chunkLines.slice(0, i);
ps = getChunkLinesPosition(result, layer, positionMap, centerPt).positions;
this._params.positions[i] = ps;
}
setLineGeometryAttribute(this.getObject3d().geometry, ps);
this.getObject3d().geometry.attributes.position.needsUpdate = true;
}
if (index >= len) {
this._params.index = -1;
this._params.idx = -1;
}
this._params.index += speed;
}
}
</script>
</body>
</html>