2024-7-24 cesium 3dtiles贴合地面通用

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 找到最低瓦片,设置高度为负最低瓦片高度,但是想要完全准确还是需要自己微调

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值