注意cesium版本: "three": "^0.168.0",
代码:
<template>
<Layout :bg-color="'black'">
<template #content>
<div id="cesiumContainers">
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
</div>
</template>
</Layout>
</template>
<script setup>
import Layout from "@/layout/layout.vue";
import * as Cesium from "cesium";
import { onMounted, ref, reactive } from "vue";
const { Viewer, Cartesian3, Color, Ion } = Cesium;
let option = {
infoBox: false,
animation: false, // 是否显示动画控件
homeButton: false, // 是否显示home键
geocoder: false, // 是否显示地名查找控件
baseLayerPicker: true, // 是否显示图层选择控件
timeline: false, // 是否显示时间线控件
fullscreenButton: false, // 是否全屏显示
infoBox: false, // 是否显示点击要素之后显示的信息
sceneModePicker: false, // 是否显示投影方式控件 三维/二维
navigationInstructionsInitiallyVisible: false,
navigationHelpButton: false, // 是否显示帮助信息控件
orderIndependentTranslucency: false,
shouldAnimate: true,
scene3DOnly: false, // 每个几何实例将只能以3D渲染以节省GPU内存
selectionIndicator: false, // 取消点击有绿框
// imageryProvider: false, // 不提供地图
baseLayerPicker: false, //是否显示图层选择控件
// 使用Cesium World Terrain图层可以三维展示地形地势、水形数据让模型效果更立体
terrainProvider: Cesium.Terrain.fromWorldTerrain(),
};
const init = async () => {
// Cesium.Ion.defaultAccessToken =
// "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxOGNkMWFmYS1hMTBkLTQyODItOGYzNi00OTM4ZTkwZTNlZGMiLCJpZCI6MTQ5MzYwLCJpYXQiOjE3Mjg5NzA2MTJ9.4pq1GnZqWPOvw4JiWO8CXJ13GyYlVflECE53m_UqWwA";
const viewer = new Cesium.Viewer("cesiumContainer", option);
try {
const tileset = await Cesium.Cesium3DTileset.fromIonAssetId(40866);
viewer.scene.primitives.add(tileset);
viewer.zoomTo(tileset);
// const response = await axios.get("/assets/json/tileset.json");
// console.log(response, "res");
const classificationTilesetUrl = {
asset: {
version: "1.0",
tilesetVersion: "1.0",
},
properties: {
Longitude: {
minimum: -1.3194245623155147,
maximum: -1.3194092552469474,
},
Latitude: {
minimum: 0.6988049305192636,
maximum: 0.6988185175658763,
},
Height: {
minimum: 73.0000006258161,
maximum: 102.999890661765747,
},
},
geometricError: 1000.0,
root: {
boundingVolume: {
region: [
-1.3194315762936881, 0.6987998244450816, -1.3194015908860113,
0.6988235397748883, 73.0000006258161, 102.999890661765747,
],
},
geometricError: 0,
refine: "ADD",
content: {
uri: "content.b3dm",
},
},
};
const classificationTileset = await Cesium.Cesium3DTileset.fromUrl(
classificationTilesetUrl,
{
classificationType: Cesium.ClassificationType.CESIUM_3D_TILE,
}
);
classificationTileset.style = new Cesium.Cesium3DTileStyle({
color: "rgba(255, 0, 0, 0.5)",
});
viewer.scene.primitives.add(classificationTileset);
const nonClassificationTileset = await Cesium.Cesium3DTileset.fromUrl(
classificationTilesetUrl,
{
show: false,
}
);
nonClassificationTileset.style = new Cesium.Cesium3DTileStyle({
color: "rgba(255, 0, 0, 0.5)",
});
viewer.scene.primitives.add(nonClassificationTileset);
} catch (error) {
console.log(`Error loading tileset: ${error}`);
}
};
onMounted(() => {
init();
});
</script>
<style lang="less" scoped>
@import url("cesium/Build/Cesium/Widgets/widgets.css");
#cesiumContainers {
width: 100%;
height: 100%;
#cesiumContainer {
width: 100%;
height: 100%;
}
}
/* This file is automatically rebuilt by the Cesium build process. */
</style>