forked from Sruthie-36/3D_FastfloodApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon2.html
190 lines (157 loc) · 5.76 KB
/
addon2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Terrain and OSM Buildings with Water</title>
<style>
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<script src="/3d_dev/libs/Cesium-1.117/Build/Cesium/Cesium.js"></script>
<script src="/3d_dev/libs/three/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<style>
@import url(/3d_dev/libs/Cesium-1.117/Apps/Sandcastle/CesiumSandcastle.css);
@import url(/3d_dev/libs/Cesium-1.117/Apps/Sandcastle/templates/bucket.css);
</style>
</head>
<body>
<div id="cesiumContainer" class="fullSize"></div>
<script type="module">
// Initialize Cesium viewer
//var viewer = new Cesium.Viewer('cesiumContainer');
Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmZjkyMzk4ZC1iMzdhLTRhNGMtYTAyMy1hNjE3YjMwMTQ3YTkiLCJpZCI6MTkwNTc2LCJpYXQiOjE3MTYyMDcwMzV9.K5Xn9YIdcDqEJnAsDm9tbMR8e_fURZ-Rn6jpMp7EDSI";
// Initialize Cesium viewer
//var viewer = new Cesium.Viewer('cesiumContainer');
var viewer = new Cesium.Viewer('cesiumContainer', {
terrain: Cesium.Terrain.fromWorldTerrain({
requestWaterMask: true,
}),
});
// Function to load GLB model and extract transformations
function loadGLBModel(url) {
return new Promise((resolve, reject) => {
const loader = new THREE.GLTFLoader();
loader.load(url, (gltf) => {
const model = gltf.scene;
const node = model.children[0];
// Log the extras to verify
//console.log('extras:', node.userData.extras);
// Ensure extras exists on the node
// const extras = node.userData.extras || {};
// const affineTransform = extras.affine_transform;
// const bounds = extras.bounds;
const affineTransform = node.userData.affine_transform;
const bounds = node.userData.bounds;
if (!affineTransform || !bounds) {
reject(new Error('Missing affine transform or bounds in extras'));
} else {
resolve({
model: gltf,
affineTransform: affineTransform,
bounds: bounds
});
}
}, undefined, reject);
});
}
// Function to convert bounds to Cesium position
function boundsToCesiumPosition(bounds) {
const centerX = (bounds.min_x + bounds.max_x) / 2;
const centerY = (bounds.min_y + bounds.max_y) / 2;
return Cesium.Cartesian3.fromDegrees(centerX, centerY, 0);
}
// Load and place the model using its transformations
async function loadAndPlaceModel(url) {
try {
const { model, affineTransform, bounds } = await loadGLBModel(url);
const position = boundsToCesiumPosition(bounds);
viewer.entities.add({
name: 'GLTF Model from Metadata',
position: position,
model: {
uri: url,
minimumPixelSize: 128,
maximumScale: 20000,
color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), // Red color
colorBlendMode: Cesium.ColorBlendMode.REPLACE,
}
});
// Fly the camera to the model
viewer.flyTo(viewer.entities);
} catch (error) {
console.error('Error loading GLB model:', error);
}
}
// OSM Buildings
let buildingTileset;
try {
buildingTileset = await Cesium.Cesium3DTileset.fromIonAssetId(
96188);
viewer.scene.primitives.add(buildingTileset);
// const tileset = await Cesium.Cesium3DTileset.fromIonAssetId(40866);
// viewer.scene.primitives.add(tileset);
} catch (error) {
console.log(`Error loading building tileset.
${error}`);
}
function createModel(url, height) {
//viewer.entities.removeAll();
const position = Cesium.Cartesian3.fromDegrees(//-61.37184, 15.30451,roseau
//-61.37808,15.31574 ,
//-61.46170, 15.3000,
-61.451490, 15.460817,
// -61.4516,
// 15.4604,
height
);
const heading = Cesium.Math.toRadians(90);
const pitch = 0;
const roll = 0;
const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
const orientation = Cesium.Transforms.headingPitchRollQuaternion(
position,
hpr
);
const entity = viewer.entities.add({
name: url,
position: position,
orientation: orientation,
model: {
uri: url,
minimumPixelSize: 128,
maximumScale: 20000,
// color: Cesium.Color.DEEPSKYBLUE.withAlpha(0.8),
// colorBlendMode: Cesium.ColorBlendMode.MIX,
// scale: 20,
},
});
viewer.trackedEntity = entity;
entity.polygon = {
heightReference: Cesium.HeightReference.CLAMP_TO_TERRAIN
};
console.log("pos1", position)
console.log("entity", entity)
}
//const resource = await Cesium.IonResource.fromAssetId(2621143);
createModel(
"bef.glb",
200
);
createModel(
"aft.glb",
400.0
);
//createModel(resource, 0.0);
// const entity1 = viewer.entities.add({
// model: { uri: resource },
// });
//viewer.trackedEntity = entity1;
</script>
</body>
</html>