async addModules() {
try {
// 加载 3dtils 模型
const tileset = await Cesium.Cesium3DTileset.fromUrl(
this.cesiumData.json
)
this.viewer.scene.primitives.add(tileset)
let lowestHeight = Number.POSITIVE_INFINITY
let lowestTile = null
// 遍历寻找高度最低瓦片
function findLowestHeight(tile) {
if (!tile.children.length) {
const boundingSphere = tile.boundingVolume.boundingSphere
const cartographic = Cesium.Cartographic.fromCartesian(
boundingSphere.center
)
if (cartographic.height < lowestHeight) {
lowestHeight = cartographic.height
lowestTile = tile
}
} else {
tile.children.forEach(child => {
findLowestHeight(child)
})
}
}
findLowestHeight(tileset.root)
// 最低高度
this.lowestHeight = lowestHeight
this.lowestTile = lowestTile
// 设置x y z偏移量
var heightOffset = -lowestHeight
var boundingSphere = tileset.boundingSphere
var cartographic = Cesium.Cartographic.fromCartesian(
boundingSphere.center
)
var surface = Cesium.Cartesian3.fromRadians(
cartographic.longitude,
cartographic.latitude,
0
)
var offset = Cesium.Cartesian3.fromRadians(
cartographic.longitude,
cartographic.latitude,
heightOffset
)
var translation = Cesium.Cartesian3.subtract(
offset,
surface,
new Cesium.Cartesian3()
)
// 改变矩阵
tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
this.viewer.zoomTo(tileset)
} catch (error) {
console.error('Error loading tileset:', error)
}
},
遍历 3dtiles 找到最低瓦片,设置高度为负最低瓦片高度,但是想要完全准确还是需要自己微调