cesium设置近地天空盒 多种效果(附天空盒原图)

效果(天空盒原图已放云盘在文章最后):

cesium设置近地天空盒 多种效果(附天空盒原图)_第1张图片

cesium设置近地天空盒 多种效果(附天空盒原图)_第2张图片


 为了效果逼真设置了当达到一定高度时就恢复系统默认星空天空盒所,以设置了两个变量 一个是近地的天空盒子一个是当超过一定高度时用系统默认的

let currSkyBox; // 当前生效的Skybox
let defaultSkybox; // cesium自带的Skybox
// 晴天
const qingtianSkybox = new Cesium.SkyBox({
  sources: {
    positiveX: "../../imgs/天空盒2/qingtian/rightav9.jpg",
    negativeX: "../../imgs/天空盒2/qingtian/leftav9.jpg",
    positiveY: "../../imgs/天空盒2/qingtian/frontav9.jpg",
    negativeY: "../../imgs/天空盒2/qingtian/backav9.jpg",
    positiveZ: "../../imgs/天空盒2/qingtian/topav9.jpg",
    negativeZ: "../../imgs/天空盒2/qingtian/bottomav9.jpg",
  },
});
// 晚霞
const wanxiaSkybox = new Cesium.SkyBox({
  sources: {
    positiveX: "../../imgs/天空盒2/wanxia/SunSetRight.png",
    negativeX: "../../imgs/天空盒2/wanxia/SunSetLeft.png",
    positiveY: "../../imgs/天空盒2/wanxia/SunSetFront.png",
    negativeY: "../../imgs/天空盒2/wanxia/SunSetBack.png",
    positiveZ: "../../imgs/天空盒2/wanxia/SunSetUp.png",
    negativeZ: "../../imgs/天空盒2/wanxia/SunSetDown.png",
  },
});
// 蓝天
const lantianSkybox = new Cesium.SkyBox({
  sources: {
    positiveX: "../../imgs/天空盒2/lantian/Right.jpg",
    negativeX: "../../imgs/天空盒2/lantian/Left.jpg",
    positiveY: "../../imgs/天空盒2/lantian/Front.jpg",
    negativeY: "../../imgs/天空盒2/lantian/Back.jpg",
    positiveZ: "../../imgs/天空盒2/lantian/Up.jpg",
    negativeZ: "../../imgs/天空盒2/lantian/Down.jpg",
  },
});

监听场景高度变化

onMounted(() => {
  viewer.terrainProvider = Cesium.createWorldTerrain(); //开启地形
  window.swpecesium.cesiumViewer.setMapCenter({
    lat: 31.035943,
    lon: 103.650219,
    alt: 609,
    heading: 40,
    pitch: 10,
  });

  defaultSkybox = viewer.scene.skyBox; //先把系统默认的天空盒保存下来
  currSkyBox = qingtianSkybox; //默认近地时使用个晴天天空盒
  viewer.scene.preUpdate.addEventListener(() => {
    //获取相机高度
    let position = viewer.scene.camera.position;
    let cameraHeight = Cesium.Cartographic.fromCartesian(position).height;
    if (cameraHeight < 240000) {
      viewer.scene.skyBox = currSkyBox;
      viewer.scene.skyAtmosphere.show = false; //关闭地球大气层
    } else {
      viewer.scene.skyBox = defaultSkybox; //使用系统默认星空天空盒
      viewer.scene.skyAtmosphere.show = true; //显示大气层
    }
  });
});

界面中4个按钮切换不同场景 简单的赋值操作

function changeView1() {
  currSkyBox = qingtianSkybox;
}
function changeView2() {
  currSkyBox = wanxiaSkybox;
}
function changeView3() {
  currSkyBox = lantianSkybox;
}
function changeView4() {
  currSkyBox = defaultSkybox;
}

完整代码:





此处有个坑!!天空会倾斜,下篇文章讲解 :https://blog.csdn.net/m0_63701303/article/details/135619546

天空盒原图地址链接:https://pan.baidu.com/s/1xnQrcf1bFxcLDz2htxtHDA 
提取码:1234

你可能感兴趣的:(cesium,前端)