cesium 实现三维无人机航拍过程实景效果

需求背景

需要实现一个动态的三维无人机航拍过程实景效果
代码开发中,迭代功能待续...

解决效果

cesium 实现三维无人机航拍过程实景效果

index.vue

javascript 复制代码
<template>
  <div>
    <el-button class="btn" @click="start">开始</el-button>
  </div>
</template>

<script>
let lineEntity,
    lineDatasource = new Cesium.CustomDataSource("line-polygun"),
    wrjModelDatasource = new Cesium.CustomDataSource("wrj"),
    wrjEntity,
    wrjLineEntity,
    curPosition,
    lineArr =[]

export default {
  data() {
    return {
      // 飞行区域边界线坐标
      coordinates: [[116.069898, 31.303655], [116.098708, 31.322126], [116.108063, 31.311256], [116.079317, 31.292959], [116.069898, 31.303655]],
      // 飞行路线
      points: [[116.069898, 31.303655, 200], [116.098708, 31.322126, 200], [116.108063, 31.311256, 200], [116.079317, 31.292959, 200]],
      // 当前飞行位置
      curRuningArr_i: 0,
      curRuningArr: [],
    }
  },
  mounted() {
    const viewer = window.dasViewer;
    viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider()
    viewer.dataSources.add(lineDatasource);
    viewer.dataSources.add(wrjModelDatasource);
    this.initwork()
  },
  destory() {
    lineDatasource.entities.removeAll()
    viewer.dataSources.remove(lineDatasource);
    wrjModelDatasource.entities.removeAll()
    viewer.dataSources.remove(wrjModelDatasource);
  },
  methods: {
    initwork() {
      const viewer = window.dasViewer;
      const pos = Cesium.Cartesian3.fromDegreesArray(this.coordinates.flat())
      const entity = lineDatasource.entities.add({
        polyline: {
          positions: pos,
          width: 1.5,
          material: Cesium.Color.fromCssColorString("#C0C0C0").withAlpha(0.5),
          // disableDepthTestDistance: Number.POSITIVE_INFINITY, //解决遮挡问题
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        }
      })
      viewer.flyTo(entity)
      this.addModel()
    },
    addModel() {
      const viewer = window.dasViewer;
      const positions = Cesium.Cartesian3.fromDegreesArrayHeights(this.points.flat())
      wrjEntity = wrjModelDatasource.entities.add({
        position: Cesium.Cartesian3.fromDegrees(116.069898, 31.303655, 200),
        model: {
          uri: process.env.VUE_APP_MODEL_API + '/wrj.glb',
          scale: 100,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.CENTER,
        },
      })
      wrjLineEntity = wrjModelDatasource.entities.add({

        polyline: {
          positions: positions,
          width: 1.5,
          material: Cesium.Color.fromCssColorString("red").withAlpha(1),
          heightReference: Cesium.HeightReference.NONE,
        }
      })
      // viewer.scene.postRender.addEventListener(()=>{
      //   wrjLineEntity.polyline.positions = lineArr
      // });
    },
    start() {
      let runQueue = this.points.map((_, i) => ([this.points[i], this.points[i + 1]]))
      runQueue.pop()
      runQueue = runQueue.map(pos => ({
        pos,
        startCartesian3: Cesium.Cartesian3.fromDegrees(pos[0][0], pos[0][1], pos[0][2]), // 该路径起始点
        cartesian3Pos: pos.map(item => Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2])) // 该路径起始点和目标点
      }))
      this.runRecursion(0, runQueue)
    },
    runRecursion(i, runArr, callback) {
      const self = this
      const speed = 700 // todo 默认速度为500m/s
      const cartesian3Pos = runArr[i].cartesian3Pos
      lineArr = runArr.slice(0, Math.max(1, i + 1)).map(item => item.startCartesian3).flat()
      self.curRuningArr_i = i
      self.curRuningArr = runArr
      self.runFn(cartesian3Pos, lineArr, speed, () => {
        if (++i < runArr.length) self.runRecursion(i, runArr, callback)
      })
    },
    runFn([startPosition, targetPosition], lineArr, speed, callback) { // [startPosition 初始点位 targetPosition 目标点位] lineArr 路径线点位 speed 速度
      const subtract = Cesium.Cartesian3.subtract(startPosition, targetPosition, new Cesium.Cartesian3());
      const meter = Cesium.Cartesian3.magnitude(subtract) // 得出距离多少米
      const step = meter / speed
      const startTime = Cesium.JulianDate.now()
      curPosition = new Cesium.Cartesian3()
      wrjEntity.position = new Cesium.CallbackProperty(() => {
        const elapsedTime = Cesium.JulianDate.secondsDifference(Cesium.JulianDate.now(), startTime);
        const ratio = elapsedTime / step;
        if (ratio >= 1.0) {
          callback()
          return targetPosition.clone()
        } else {
          return Cesium.Cartesian3.lerp(startPosition, targetPosition, ratio, curPosition)
        }
      }, false);
    },
  }
}
</script>

<style lang="less" scoped>
.btn {
  position: fixed;
  top: 20px;
  left: 20px;
}
</style>
相关推荐
创小董13 小时前
高海拔低温地区无人机大载重吊运技术详解
无人机
汪洪墩14 小时前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium
创小董20 小时前
垂起固定翼无人机大面积森林草原巡检技术详解
无人机
supermapsupport20 小时前
iClent3D for Cesium 实现无人机巡检飞行效果
gis·cesium·supermap·webgis
不浪brown1 天前
仅需3行代码!带你做一个3D卫星轨道飞行动画
cesium
IT猿手1 天前
基于PWLCM混沌映射的麋鹿群优化算法(Elk herd optimizer,EHO)的多无人机协同路径规划,MATLAB代码
算法·elk·机器学习·matlab·无人机·聚类·强化学习
创小董1 天前
无人机飞防高效率喷洒技术详解
无人机
云卓SKYDROID2 天前
反无人机防御系统概述!
无人机·科普·高科技·云卓科技
EasyDSS2 天前
视频直播点播平台EasyDSS与无人机技术的森林防火融合应用
音视频·无人机
IT猿手3 天前
SDMTSP:黑翅鸢算法(Black-winged kite algorithm,BKA)求解单仓库多旅行商问题,可以更改数据集和起点(MATLAB代码)
人工智能·深度学习·机器学习·matlab·无人机·智能优化算法