forked from peterqliu/threebox
-
Notifications
You must be signed in to change notification settings - Fork 149
/
18-extrusions.html
142 lines (121 loc) · 4.31 KB
/
18-extrusions.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
<!doctype html>
<head>
<title>Threbox extrusions sample</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="https://d3js.org/d3.v6.min.js"></script>
<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"></div>
<script type="module">
///// THIS EXTRUSION SAMPLE SHOWS HOW TO CREATE EXTRUSIONS IN TWO WAYS
///// THE FIRST WAY IS TO CREATE AN ARRAY OF VECTOR2 AND USE tb.extrusion
///// THE SECOND WAY IS TO READ geoJson FEATURES FROM A FILE
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 origin1 = [-74.001058, 40.714914, 0];
let origin2 = [-74.0001269, 40.7151698, 50]
let map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/outdoors-v11',
center: origin1,
zoom: 16.9,
pitch: 60,
antialias: true,
heading: 0, hash: true
});
// we can add Threebox to mapbox to add built-in mouseover/mouseout and click behaviors
window.tb = new Threebox(
map,
map.getCanvas().getContext('webgl'),
{
defaultLights: true,
enableSelectingFeatures: true, //change this to false to disable fill-extrusion features selection
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
}
);
let redMaterial = new THREE.MeshPhongMaterial({
color: 0x660000,
side: THREE.DoubleSide
});
let stats;
import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
function animate() {
requestAnimationFrame(animate);
stats.update();
}
let buildings = [];
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) {
//[jscastro] we add a star based on this example https://threejs.org/examples/?q=extrud#webgl_geometry_extrude_shapes
const points = [], numPts = 5;
for (let i = 0; i < numPts * 2; i++) {
const l = i % 2 == 1 ? 10 : 20;
const a = i / numPts * Math.PI;
points.push(new THREE.Vector2(Math.cos(a) * l, Math.sin(a) * l));
}
const material1 = new THREE.MeshLambertMaterial({ color: 0xb00000, wireframe: false });
const material2 = new THREE.MeshLambertMaterial({ color: 0xff8000, wireframe: false });
let star = tb.extrusion({
coordinates: points,
geometryOptions: { depth: 20, steps: 1, bevelEnabled: true, bevelThickness: 2, bevelSize: 4, bevelSegments: 1 },
anchor: 'center',
units: 'meters',
rotation: { x: 90, y: 0, z: 20 },
materials: [material1, material2]
});
star.addTooltip("A animated extruded star over Columbus Park", true);
star.setCoords(origin2);
star.set({ rotation: {x: 0, y: 0, z: 720}, duration: 20000 })
tb.add(star);
//[jscastro] we read a geoJson that is filled with 3 real features from the composite source
d3.json('./geojson/extrusion.geojson').then(function (fc) {
console.log(fc);
//then we create the extrusions based on the geoJson features
addBuildings(fc.features);
})
},
render: function (gl, matrix) {
tb.update();
}
});
});
function addBuildings(data, info, height = 1) {
data.forEach((b) => {
let center = b.properties.center;
let s = tb.projectedUnitsPerMeter(center[1]);
let extrusion = tb.extrusion({
coordinates: b.geometry.coordinates,
geometryOptions: { curveSegments: 1, bevelEnabled: false, depth: b.layer.paint["fill-extrusion-height"] * s },
materials: redMaterial
});
extrusion.addTooltip(b.properties.tooltip, true);
extrusion.setCoords([center[0], center[1], 0]);
tb.add(extrusion);
});
}
</script>
</body>