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

文章描述了如何在Cesium环境中使用SkyBox创建逼真的天空效果,通过监听摄像机高度变化,实现从晴天、晚霞到蓝天及默认星空的无缝切换,同时控制地球大气层的显示。
摘要由CSDN通过智能技术生成

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


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

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;
}

完整代码:


<template>
  <div class="btn">
    <el-form :model="person.data" label-width="140px">
      <el-form-item label="效果:">
        <div v-for="(item, index) in person.cameraData" :key="index">
          <el-button @click="item.callback">{{ item.name }}</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
  <Map />
</template>

<script setup>
import Map from "@/components/map/Map.vue";
import { nextTick, onMounted, reactive } from "vue";
import "./skybox_nearground.js";
const person = reactive({
  cameraData: [
    {
      name: "晴天",
      title: "",
      callback: () => changeView1(),
    },
    {
      name: "晚霞",
      title: "",
      callback: () => changeView2(),
    },
    {
      name: "蓝天",
      title: "",
      callback: () => changeView3(),
    },
    {
      name: "默认",
      title: "",
      callback: () => changeView4(),
    },
  ],
});

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; //显示大气层
    }
  });
});
function changeView1() {
  currSkyBox = qingtianSkybox;
}
function changeView2() {
  currSkyBox = wanxiaSkybox;
}
function changeView3() {
  currSkyBox = lantianSkybox;
}
function changeView4() {
  currSkyBox = defaultSkybox;
}
</script>
<style scoped lang='less'>
.btn {
  position: absolute;
  left: 300px;
  top: 30px;
  z-index: 999;
}
:deep(.el-form-item__label) {
  color: #fff;
}
</style>

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

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

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_63701303

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值