fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as mars3d from "mars3d"
import { $message } from "@mars/components/mars-ui/index"
 
export let map // mars3d.Map三维地图对象
let graphicLayer
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 30.178797, lng: 118.183551, alt: 210053, heading: 353, pitch: -49 },
    clock: {
      startTime: "2017/08/25 08:00:00",
      stopTime: "2017/08/25 08:01:30"
    }
  },
  control: {
    animation: true, // 是否创建 动画小器件,左下角仪表
    timeline: true // 是否显示 时间线控件
  }
}
 
export const eventTarget = new mars3d.BaseClass()
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  // 创建矢量数据图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  // 静态数据
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/apidemo/exercise-fixed.json" }).then(function (json) {
    graphicLayer.loadGeoJSON(json)
  })
 
  // 动态数据
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/apidemo/exercise-dynamic.json" }).then(function (arrData) {
    for (let i = 0; i < arrData.length; i++) {
      const data = arrData[i]
 
      // 动态坐标属性
      const property = new Cesium.SampledPositionProperty()
      data.path.forEach((item, index) => {
        const startTime = item.time
        let tempTime = Cesium.JulianDate.fromDate(new Date(startTime))
        tempTime = Cesium.JulianDate.addSeconds(tempTime, 0, new Cesium.JulianDate())
        property.addSample(tempTime, Cesium.Cartesian3.fromDegrees(...item.position))
      })
 
      switch (data.type) {
        case "plane":
          addPlane(property, data.team)
          break
        case "arrow":
          addAttackArrow(property, data.team)
          break
        case "missile":
          addMissile(property, data.team)
          break
        case "fire":
          addFire(property)
          break
        default:
          break
      }
    }
  })
 
  addWallPrimitive(
    [
      [118.67566, 32.349367],
      [119.291568, 31.835385],
      [118.374952, 31.373451]
    ],
    "#4755C9"
  )
 
  addWallPrimitive(
    [
      [116.44771, 31.324743],
      [116.553874, 32.231496],
      [117.649474, 32.017338]
    ],
    "#FF0004"
  )
 
  setMapView(22, () => {
    map.setCameraView({ lat: 31.252058, lng: 117.988745, alt: 95026, heading: 340, pitch: -49 })
  })
  setMapView(55, () => {
    $message("红方获得胜利")
  })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addWallPrimitive(positions, color) {
  const Square = new mars3d.graphic.WallPrimitive({
    positions: mars3d.graphic.GatheringPlace.getOutlinePositions(positions),
    style: {
      diffHeight: 5000,
      closure: true,
      material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.LineFlow, {
        image: "img/textures/fence.png",
        color: color,
        speed: 10, // 速度,建议取值范围1-100
        axisY: true
      })
    }
  })
  graphicLayer.addGraphic(Square)
}
 
// 【动态】飞机飞行路线
function addPlane(property, team) {
  const feijiPath = new mars3d.graphic.PathEntity({
    position: property,
    style: {
      width: 6,
      leadTime: 0, // 前方的路线不显示
      material: mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.LineFlow, {
        color: getTeamColor(team),
        repeat: new Cesium.Cartesian2(2.0, 1.0),
        image: "img/textures/line-gradual.png",
        speed: 15
      })
    },
    model: {
      url: "//data.mars3d.cn/gltf/mars/zhanji.glb",
      scale: 0.001,
      minimumPixelSize: 100,
      show: true,
      clampAnimations: true,
      distanceDisplayCondition: true,
      distanceDisplayCondition_near: 0,
      distanceDisplayCondition_far: 900000
    },
    billboard: {
      show: false,
      image: "img/icon/plane_blue.png",
      color: getTeamColor(team),
      scale: 0.2,
      distanceDisplayCondition: true,
      distanceDisplayCondition_near: 90000,
      distanceDisplayCondition_far: Number.MAX_VALUE
    }
  })
  graphicLayer.addGraphic(feijiPath)
}
 
// 【动态】进攻箭头标号
function addAttackArrow(property, team) {
  // property.forwardExtrapolationType = Cesium.ExtrapolationType.HOLD;
 
  const times = [].concat(property._property._times)
  const positions = [property.getValue(times.shift())]
  const graphicTriangle = new mars3d.graphic.AttackArrowPW({
    positions: new Cesium.CallbackProperty(function (time) {
      for (let index = 0, len = times.length; index < len; index++) {
        const item = times[index]
        if (item.secondsOfDay === Math.round(time.secondsOfDay)) {
          if (property.getValue(time)) {
            positions.push(property.getValue(time))
            times.shift()
            break
          }
        }
      }
 
      const point = property.getValue(time)
      if (point) {
        return positions.concat(point)
      } else {
        return positions
      }
    }, false),
    style: {
      color: getTeamColor(team),
      opacity: 0.6,
      outline: true,
      outlineWidth: 3,
      outlineColor: "#ffffff"
      // clampToGround: true,
    }
  })
  graphicLayer.addGraphic(graphicTriangle)
}
 
// 【动态】导弹飞行路线
function addMissile(property, team) {
  let missileImage
  if (team === "red") {
    missileImage = "img/icon/missile_red.png"
  } else {
    missileImage = "img/icon/missile_blue.png"
  }
 
  const graphic = new mars3d.graphic.BillboardEntity({
    position: property,
    orientation: new Cesium.VelocityOrientationProperty(property),
    style: {
      image: missileImage,
      scale: 0.5,
      alignedAxis: new Cesium.VelocityVectorProperty(property, true)
    },
    hasEdit: false
  })
  graphicLayer.addGraphic(graphic)
}
 
// 【动态】导弹爆炸之后的滞留
function addFire(property) {
  const fireImage = new mars3d.graphic.BillboardEntity({
    position: property,
    orientation: new Cesium.VelocityOrientationProperty(property),
    style: {
      image: "img/icon/fire.png",
      scale: 1.0,
      alignedAxis: new Cesium.VelocityVectorProperty(property, true)
    },
    hasEdit: false
  })
  graphicLayer.addGraphic(fireImage)
}
 
function getTeamColor(team) {
  if (team === "red") {
    return "#FF0000"
  } else {
    return "#0099FF"
  }
}
 
let num
function setMapView(time, callback) {
  map.on(mars3d.EventType.clockTick, function (event) {
    if (new Date(event.currentTime).getMinutes() === 0 && new Date(event.currentTime).getSeconds() === time) {
      clearTimeout(num)
      num = setTimeout(() => {
        callback()
      }, 2000)
    }
  })
}