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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import * as mars3d from "mars3d"
import { $message } from "@mars/components/mars-ui/index"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
 
let selectedView
let videoElement
 
// 事件对象,用于抛出事件给面板
export const eventTarget = new mars3d.BaseClass()
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 31.843062, lng: 117.205439, alt: 150, heading: 178, pitch: -75 },
    globe: {
      depthTestAgainstTerrain: true // 不加无法投射到地形上
    }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录首次创建的map
 
  // 添加参考三维模型
  const tiles3dLayer = new mars3d.layer.TilesetLayer({
    name: "合肥国家大学科技园",
    url: "//data.mars3d.cn/3dtiles/qx-hfdxy/tileset.json",
    position: { alt: -24 },
    maximumScreenSpaceError: 1,
    maximumMemoryUsage: 1024
  })
  map.addLayer(tiles3dLayer)
 
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  createVideoDom()
 
  // 加一些演示数据
  addDemoGraphic1()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
// 加载已配置好的视频(此参数为界面上“打印参数”按钮获取的)
function addDemoGraphic1() {
  const video3D = new mars3d.graphic.Video3D({
    position: [117.205457, 31.842984, 63.9],
    style: {
      container: videoElement,
      maskImage: "img/textures/video-mask.png", // 羽化视频四周,融合更美观
      angle: 46.3,
      angle2: 15.5,
      heading: 178.5,
      pitch: -49.5,
      showFrustum: true
    },
    attr: { remark: "示例1" }
  })
  graphicLayer.addGraphic(video3D)
 
  selectedView = video3D // 记录下
  eventTarget.fire("loadVideo", {
    value: {
      cameraAngle: selectedView.angle,
      cameraAngle2: selectedView.angle2,
      heading: selectedView.heading,
      pitchValue: selectedView.pitch,
      distanceValue: selectedView.distance,
      opcity: selectedView.opacity,
      ckdFrustum: selectedView.showFrustum
    }
  })
}
 
const flvUrl = "https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv"
function createVideoDom() {
  videoElement = mars3d.DomUtil.create("video", "", document.body)
  videoElement.setAttribute("muted", "muted")
  videoElement.setAttribute("autoplay", "autoplay")
  videoElement.setAttribute("loop", "loop")
  videoElement.setAttribute("crossorigin", "")
  videoElement.setAttribute("controls", "")
  videoElement.style.display = "none"
 
  // 加FLV演示数据
  if (window.flvjs.isSupported()) {
    const flvPlayer = window.flvjs.createPlayer({
      type: "flv",
      url: flvUrl
    })
    flvPlayer.attachMediaElement(videoElement)
    flvPlayer.load()
    flvPlayer.play()
  } else {
    $message("不支持flv格式视频")
  }
 
  setTimeout(() => {
    try {
      if (videoElement.paused) {
        globalMsg("当前浏览器已限制自动播放,请单击播放按钮")
        videoElement.play()
      }
    } catch (e) {
      // 规避浏览器权限异常
        globalMsg("当前浏览器已限制自动播放,请单击播放按钮")
    }
  }, 3000)
}
 
export function onChangeAngle(value) {
  if (selectedView) {
    selectedView.angle = value
  }
}
 
export function onChangeAngle2(value) {
  if (selectedView) {
    selectedView.angle2 = value
  }
}
 
export function onChangeDistance(value) {
  if (selectedView) {
    selectedView.distance = value
  }
}
 
export function onChangeHeading(value) {
  if (selectedView) {
    selectedView.heading = value
  }
}
 
export function onClickSelView() {
  if (!selectedView) {
    return
  }
 
  map.graphicLayer.startDraw({
    type: "point",
    success: (graphic) => {
      const point = graphic.point
      graphic.remove() // 删除绘制的点
 
      selectedView.targetPosition = point
    }
  })
}
 
export function onChangePitch(value) {
  if (selectedView) {
    selectedView.pitch = value
  }
}
 
// 透明度
export function onChangeOpacity(value) {
  if (selectedView) {
    selectedView.opacity = value
  }
}
 
// 添加投射视频
export function addVideo(data) {
  // 开始绘制
  graphicLayer.startDraw({
    type: "video3D",
    style: {
      container: videoElement,
      angle: data.cameraAngle,
      angle2: data.cameraAngle2,
      heading: data.heading,
      pitch: data.pitchValue,
      distance: data.distanceValue,
      showFrustum: data.ckdFrustum
    },
    success: function (graphic) {
      console.log("绘制完成", graphic)
 
      selectedView = graphic // 记录下
    }
  })
}
 
// 按当前视角投射视频
export function addThisCamera(data) {
  // 取屏幕中心点
  const targetPosition = map.getCenter({ format: false })
  if (!targetPosition) {
    return
  }
 
  const cameraPosition = Cesium.clone(map.camera.position)
 
  // 构造投射体
  const video3D = new mars3d.graphic.Video3D({
    position: cameraPosition,
    targetPosition: targetPosition,
    style: {
      container: videoElement,
      angle: data.cameraAngle,
      angle2: data.cameraAngle2,
      heading: data.heading,
      pitch: data.pitchValue,
      distance: data.distanceValue,
      opacity: data.opcity,
      showFrustum: data.showFrustum
    }
  })
  graphicLayer.addGraphic(video3D)
 
  selectedView = video3D // 记录下
}
 
// 清除
export function clear() {
  graphicLayer.clear()
  selectedView = null
}
 
// 播放暂停
export function playOrpause() {
  if (!selectedView) {
    return
  }
  selectedView.play = !selectedView.play
}
 
// 定位至视频位置
export function locate() {
  if (selectedView) {
    selectedView.setView()
  }
}
 
// 打印参数
export function printParameters() {
  if (!selectedView) {
    return
  }
 
  const params = JSON.stringify(selectedView.toJSON())
  console.log("Video3D构造参数为", params)
}
 
// 线框是否显示
export function showFrustum(ckd) {
  if (!selectedView) {
    return
  }
  selectedView.showFrustum = ckd
}
 
// 视频位置
export function selCamera() {
  if (!selectedView) {
    return
  }
  map.graphicLayer.startDraw({
    type: "point",
    success: (graphic) => {
      const point = graphic.point
      graphic.remove() // 删除绘制的点
 
      selectedView.position = point
    }
  })
}