-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
153 lines (136 loc) · 5.98 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tom3dtiles</title>
<style>
body {
margin:0;
padding:0;
}
#container {
position: absolute;
width: 100%;
height: 100%;
}
</style>
<!--script src="./mapbox-gl-dev.js"></script -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mapbox-gl.js"></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.css' rel='stylesheet' />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script src="./config.js"></script>
<script src="./mapbox3dtileslayer.js"></script>
<!--script src="./camerasync.js"></script-->
<script>
let glmap;
let width;
let cameraSync;
function logMatrix(label, matrix) {
console.log(label);
if (!matrix) {
console.log('undefined');
return
}
console.log(`[${matrix[0]} ${matrix[1]} ${matrix[2]} ${matrix[3]} || ${matrix[4]} ${matrix[5]} ${matrix[6]} ${matrix[7]} || ${matrix[8]} ${matrix[9]} ${matrix[10]} ${matrix[11]} || ${matrix[12]} ${matrix[13]} ${matrix[14]} ${matrix[15]}]`);
}
let tileslayer = new Mapbox3DTileLayer( {
id: 'Velsen',
url: 'https://saturnus.geodan.nl/maquette_nl/data/buildingtiles_velsen_3857/tileset.json',
});
function Raycaster() {
let raycaster = new THREE.Raycaster();
//raycaster.params.Points.threshold = 1;
return raycaster;
}
let raycaster = Raycaster();
function mouseToThree(mouseX, mouseY) {
let height = 400;
return new THREE.Vector3(
mouseX / width * 2 - 1,
-(mouseY / height) * 2 + 1,
1
);
}
function sortIntersectsByDistanceToRay(intersects) {
return intersects.sort(d=>d.distanceToRay);
}
function checkIntersects(mouse_position,camera, scene) {
let mouse_vector = mouseToThree(...mouse_position);
camera.updateMatrixWorld();
camera.projectionMatrixInverse.getInverse(camera.projectionMatrix);
raycaster.setFromCamera(mouse_vector, camera);
let intersects = [];
scene.traverse(c=>{
if (c.isMesh) {
intersects = raycaster.intersectObject(c);
if (intersects[0]) {
let sorted_intersects = sortIntersectsByDistanceToRay(intersects);
let intersect = sorted_intersects[0];
intersect.object.material.color.set( 0xff0000 );
let idx = intersect.object.geometry.attributes._batchid.data.array[intersect.faceIndex*7+6];
console.log('Intersections: ',intersects.length);
console.log("Mousepos: ",mouse_vector);
console.log("Intersection: ",intersect,"Blockid:",intersect.object.parent.userData.id[idx]);
} else {
//removeHighlights();
//hideTooltip();
}
}
})
}
function mapClicked(event) {
console.log('******************* CLICK ********************');
console.log(`glmap._canvas.width: ${glmap._canvas.width}, height: ${glmap._canvas.height}`);
console.log(`glmap.transform.scale: ${glmap.transform.scale}, glmap.transform.pixelsToGLUnits: ${glmap.transform.pixelsToGLUnits}`)
logMatrix('glmap.transform.glCoordMatrix:', glmap.transform.glCoordMatrix);
logMatrix('tileslayer.camera.projectionMatrix.elements:', tileslayer.camera.projectionMatrix.elements);
logMatrix('tileslayer.camera.projectionMatrixInverse.elements:', tileslayer.camera.projectionMatrixInverse.elements);
//let features = glmap.queryRenderedFeatures(event.point, {layers:['drawPolygons']});
//if (features.length) {
// console.log(features)
//}
checkIntersects([event.point.x,event.point.y], tileslayer.camera, tileslayer.scene);
}
function init() {
width = document.querySelector('#container').clientWidth;
glmap = new mapboxgl.Map({
container: 'container',
center: [4.62067229, 52.45920665],
zoom: 18,
style: "mapbox://styles/mapbox/light-v9"
});
glmap.on('load',_=>{
glmap.addLayer(tileslayer);
});
glmap.on("mousemove", e => {
checkIntersects(
[e.point.x,e.point.y],
tileslayer.camera,
tileslayer.scene //tileslayer.tileset.root.totalContent
);
});
glmap.on('click', e => mapClicked(e));
glmap.once('move', ()=>{
//cameraSync = new CameraSync(glmap, tileslayer.camera);
})
/*updatedCamera = new THREE.PerspectiveCamera( 28, window.innerWidth / window.innerHeight, 0.000000000001, Infinity);
// The CameraSync object will keep the Mapbox and THREE.js camera movements in sync.
// It requires a world group to scale as we zoom in. Rotation is handled in the camera's
// projection matrix itself (as is field of view and near/far clipping)
// It automatically registers to listen for move events on the map so we don't need to do that here
let world = new THREE.Group();
let scene = new THREE.Scene();
scene.add(world);
cameraSync = new CameraSync(glmap, updatedCamera, world);
*/
}
addEventListener('load', init)
</script>
</head>
<body>
<div id='container'></div>
Based on <a href="https://observablehq.com/@tomvantilburg/mapbox-3d-tiles-viewer">this observable</a>.
</body>
</html>