天空盒子

天空盒、雨雪雾、夜景、后处理、火焰粒子等场景效果。下面是该示例的核心 script 片段,供参考学习;完整可运行代码请进入在线 Demo 或查看仓库源码。

在线运行 Demo 全部示例 查看完整源码

示例代码

import * as Cesium from 'cesium'
import { initCesium } from '@/utils/cesiumPluginsExtends/index'

let skyObj = null;
let flag = true;
export default {
  data () {
    return {
      activeId: 'light'
    }
  },
  mounted () {
    this.initMap()
  },
  methods: {
    initMap () {
      const tempData = [
        {
          type: 'UrlTemplateImageryProvider',
          option: {
            url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
          }
        },
        {
          type: 'UrlTemplateImageryProvider',
          option: {
            url: 'https://webst03.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&style=7',
          }
        }
      ]
      const {
        viewer,
        material,
        graphics,
        math3d } = new initCesium(
          {
            cesiumGlobal: Cesium,
            containerId: 'cesiumContainer',
            viewerConfig: {
              infoBox: false,
              shouldAnimate: true,
            },
            extraConfig: {},
            MapImageryList: tempData
          })


      this.c_viewer = viewer;

      this.material = material;
      this.graphics = graphics;
      this.math3d = math3d;
      this.flyto();
      this.sky1('light');
      this.addListenerToScene();
    },
    flyto () {
      this.material.setView({
        position: Cesium.Cartesian3.fromDegrees(120.380788, 31.066719, 12136),
        orientation: {
          heading: Cesium.Math.toRadians(0.0),
          pitch: Cesium.Math.toRadians(-30),
          roll: Cesium.Math.toRadians(0.0)
        },
      })
    },
    sky1 (clicktype) {
      this.activeId = clicktype;
      skyObj = this.material.setOneGroundSkyBox();
      this.c_viewer.scene.skyBox = skyObj
      this.c_viewer.scene.skyAtmosphere.show = false
    },
    sky2 (clicktype) {
      this.activeId = clicktype;
      skyObj = this.material.setTwoGroundSkyBox()
      this.c_viewer.scene.skyBox = skyObj
      this.c_viewer.scene.skyAtmosphere.show = false
    },
    sky3 (clicktype) {
      this.activeId = clicktype;
      skyObj = this.material.setThreeGroundSkyBox();
      this.c_viewer.scene.skyBox = skyObj
      this.c_viewer.scene.skyAtmosphere.show = false
    },
    addListenerToScene () {

      this.c_viewer.scene.camera.moveEnd.addEventListener((move) => {
        let position = this.material.getCameraPosition()
        if (!position.height) return
        if (100000 <= position.height) {
          if (flag) {
            this.c_viewer.scene.skyBox = this.material.setOneSkyBox();
            this.c_viewer.scene.skyAtmosphere.show = true
            flag = false
          }
        } else if (100000 > position.height) {
          if (!flag) {
            this.c_viewer.scene.skyBox = skyObj
            this.c_viewer.scene.skyAtmosphere.show = false
            flag = true
          }

        }
      });
    }
  },
  beforeUnmount () {
    this.c_viewer = null;
    this.material = null;
    this.graphics = null;
  }
}

相关 API

安装:npm install cesium_dev_kit cesium