home.vue 2.45 KB
Newer Older
forevertyler's avatar
forevertyler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<template>
  <div class="app-container home">
    <div id="cesiumContainer"></div>
  </div>
</template>
<script>
export default {
  name: "cesium",
  data() {
    return {};
  },
  created() {
    var that = this;
    this.$nextTick(() => {
lei's avatar
lei committed
15 16
      Cesium.Ion.defaultAccessToken =
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2ZmY5ZWUyZS00MjQwLTQzZjUtYTBjZi02ZWZiYzJhMmY2NTYiLCJpZCI6MTMxMzY5LCJpYXQiOjE3NDE2NzY4Mzh9.QD-8cQI_VqPG2t-S8KxLyMFux0R429lfTdhQWrdeWhE";
forevertyler's avatar
forevertyler committed
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
      const viewer = new Cesium.Viewer("cesiumContainer", {
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false,
        animation: false,
        infoBox: false,
        selectionIndicator: false,
        geocoder: false,
        timeline: false,
        fullscreenButton: false,
        shouldAnimate: false,
        scene3DOnly: true,
        navigationHelpButton: false,
      });
      viewer.cesiumWidget.creditContainer.style.display = "none";

      // 使用 fromUrl 方法加载模型
      Cesium.Cesium3DTileset.fromUrl("/terra_b3dms/tileset.json")
        .then((tileset) => {
          viewer.scene.primitives.add(tileset);
          viewer.zoomTo(tileset);

          // 调整模型高度
          var heightOffset = -330.0; // 调整这个值以匹配地形高度
          var boundingSphere = tileset.boundingSphere;
lei's avatar
lei committed
42 43 44
          var cartographic = Cesium.Cartographic.fromCartesian(
            boundingSphere.center
          );
forevertyler's avatar
forevertyler committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

          var surface = Cesium.Cartesian3.fromRadians(
            cartographic.longitude,
            cartographic.latitude,
            0.0
          );

          var offset = Cesium.Cartesian3.fromRadians(
            cartographic.longitude,
            cartographic.latitude,
            cartographic.height + heightOffset
          );

          var translation = Cesium.Cartesian3.subtract(
            offset,
            surface,
            new Cesium.Cartesian3()
          );

          tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);

lei's avatar
lei committed
66
          tileset.maximumMemoryUsage = 1024 * 1024;
forevertyler's avatar
forevertyler committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
          tileset.maximumScreenSpaceError = 4;

          // 开启地形深度检测
          viewer.scene.globe.depthTestAgainstTerrain = true;
        })
        .catch((error) => {
          console.error("模型加载失败:", error);
        });
    });
  },
  methods: {},
};
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  height: 100%;
  #cesiumContainer {
    width: 100%;
    height: 100%;
  }
}
lei's avatar
lei committed
90
</style>