<template>
  <div id="cesium-viewer" ref="viewerDivRef"></div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import * as Cesium from 'cesium'
import 'cesium/Build/Cesium/Widgets/widgets.css'
const viewerDivRef = ref<HTMLDivElement>()
window.CESIUM_BASE_URL = 'node_modules/cesium/Build/Cesium/'
// window.CESIUM_BASE_URL = 'libs/cesium/' //打包部署

Cesium.Ion.defaultAccessToken =
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3YjU4MjJlMS0wMWE4LTRhOWQtYjQ1OC04MTgzMzFhMzQ5ZjAiLCJpZCI6MTE1ODUxLCJpYXQiOjE2NjkyMDM1MzN9.8ajEuv3VKYg8wvFiQlUWWY6Ng6JfY4PuVgRyStL1B-E'

//离线地球底图
const earth_native = Cesium.TileMapServiceImageryProvider.fromUrl(
  // 'node_modules/cesium/Build/CesiumUnminified/Assets/Textures/NaturalEarthII',
  Cesium.buildModuleUrl('Assets/Textures/NaturalEarthII'),
)
onMounted(async () => {
  // 修改相机的默认矩形范围
  Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(
    75.0, // 西经
    0.0, // 南纬
    140.0, // 东经
    60.0, // 北纬
  )
  //未联网状态加载默认本地底图，保证地球有底图显示
  let viewer = new Cesium.Viewer(viewerDivRef.value as HTMLElement, {
    infoBox: false, //是否显示点击要素之后显示的信息
    homeButton: false, //是否显示Home按钮
    navigationHelpButton: false, //是否显示帮助信息控件
    timeline: false, //是否显示时间线控件
    animation: false, //是否显示动画控件
    sceneModePicker: true, //是否显示投影方式控件
    sceneMode: Cesium.SceneMode.SCENE3D, //设置场景模式，3D球体
    // terrain: Cesium.Terrain.fromWorldTerrain(),
    baseLayerPicker: false, //是否显示图层选择控件
    requestRenderMode: true, //启用请求渲染模式
    scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
    fullscreenButton: false, //右下角 全屏控件
    orderIndependentTranslucency: false,
    contextOptions: { webgl: { alpha: true } },
    baseLayer: Cesium.ImageryLayer.fromProviderAsync(earth_native, {}),
  })
  //去除cesium logo，隐藏版本信息
  const creditContainer = viewer.cesiumWidget.creditContainer as HTMLDivElement
  creditContainer.style.display = 'none'
})
</script>

<style scoped>
#cesium-viewer {
  width: 100%;
  height: 100vh;
}
</style>
