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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import * as mars3d from "mars3d"
import { TilesEditor } from "./TilesEditor.js"
 
export let map // mars3d.Map三维地图对象
export let tiles3dLayer
export let tilesEditor
 
// 自定义事件
export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  map.fixedLight = true // 固定光照,避免gltf模型随时间存在亮度不一致。
 
  // 鼠标拖拽编辑,定义在js/TilesEditor.js
  tilesEditor = new TilesEditor({
    map: map,
    moveImg: "img/icon/move.png",
    rotateImg: "img/icon/rotate.png"
  })
 
  tilesEditor.on("change", function (data) {
    tilesEditor.enabled = true
    eventTarget.fire("tilesEditor", { data })
  })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
// 查看构件
export function showCompTree(model) {
  querySceneTreeData(model)
    .then(function (scene) {
      const data = []
      if (scene.scenes) {
        for (let i = 0; i < scene.scenes.length; i++) {
          const node = scene.scenes[i]
          name2text(node)
          data.push(node)
        }
      } else {
        name2text(scene)
        data.push(scene)
      }
 
      eventTarget.fire("compTree", { data })
    })
    .catch(function (error) {
      console.log("加载JSON出错", error)
    })
}
 
export function compModelChange(nodeid, nodesphere) {
  if (nodesphere[3] <= 0) {
    return
  }
  // 构件节点位置
  let center = new Cesium.Cartesian3(nodesphere[0], nodesphere[1], nodesphere[2])
 
  // 获取构件节点位置,现对于原始矩阵变化后的新位置
  center = tiles3dLayer.getPositionByOrginMatrix(center)
 
  // 飞行过去
  const sphere = new Cesium.BoundingSphere(center, nodesphere[3])
  map.camera.flyToBoundingSphere(sphere, {
    offset: new Cesium.HeadingPitchRange(map.camera.heading, map.camera.pitch, nodesphere[3] * 1.5),
    duration: 0.5
  })
 
  // 设置tileset的样式
  tiles3dLayer.style = new Cesium.Cesium3DTileStyle({
    color: {
      conditions: [
        ["${id} ==='" + nodeid + "'", "rgb(255, 255, 255)"],
        ["true", "rgba(255, 200, 200,0.2)"]
      ]
    }
  })
}
export function checkedTree() {
  tiles3dLayer.tileset.style = undefined
}
 
/**
 *
 *
 * @param {string} url 面板中传入的url模型地址
 * @return {*} 无
 */
export function showModel(url) {
  removeLayer()
 
  if (!url) {
    alert("请输入图层URL!")
    return
  }
 
  tiles3dLayer = new mars3d.layer.TilesetLayer({
    url: url,
    // 高亮时的样式
    highlight: {
      type: mars3d.EventType.click, // 默认为鼠标移入高亮,也可以指定click单击高亮
      outlineEffect: true, // 采用OutlineEffect方式来高亮
      color: "#00FF00"
    },
    popup: "all",
    flyTo: true
  })
  map.addLayer(tiles3dLayer)
 
  // 加载完成事件
  tiles3dLayer.on(mars3d.EventType.load, function (event) {
    const data = event.tileset
 
    if (tiles3dLayer.transform) {
      tilesEditor.range = data.boundingSphere.radius * 0.9
      tilesEditor.heading = tiles3dLayer.rotation_z
      tilesEditor.position = tiles3dLayer.position
    } else {
      tilesEditor.enabled = false
    }
 
    // 触发自定义事件,更改面板中的值
    eventTarget.fire("tiles3dLayerLoad", { data, tiles3dLayer })
  })
}
 
// 异步求准确高度
export function getDefined(data) {
  const params = getConfig(data)
  // 求地面海拔 (异步)
  if (Cesium.defined(params.position) && Cesium.defined(params.position.alt)) {
    // 存在历史设置的高度时不用处理
  } else {
    mars3d.PointUtil.getSurfaceTerrainHeight(map.scene, tiles3dLayer.orginCenterPosition, {
      asyn: true, // 是否异步求准确高度
      callback: function (newHeight) {
        if (newHeight == null) {
          return
        }
        const offsetZ = Math.ceil(newHeight - tiles3dLayer.orginCenterPoint.alt + 1)
        console.log("地面海拔:" + newHeight.toFixed(2) + ",需要偏移" + offsetZ)
 
        data.txtZ = offsetZ
        tiles3dLayer.height = offsetZ
      }
    })
  }
}
 
export function editor(event, txtZ) {
  if (Cesium.defined(event.position)) {
    const pos = event.position
    const thisZ = txtZ
    const position = mars3d.PointUtil.setPositionsHeight(pos, thisZ)
 
    tilesEditor.position = position
    tiles3dLayer.center = position
 
    const point = mars3d.LngLatPoint.fromCartesian(position)
    eventTarget.fire("changePoition", { point })
  } else if (Cesium.defined(event.heading)) {
    tiles3dLayer.rotation_z = event.heading
    eventTarget.fire("changeHeading", { tiles3dLayer })
  }
}
/**
 * 通过面板修改模型,将面板中的参数进行修改
 *
 * @param {object} pannelData 面板改变的值
 * @return {object} params  模型的参数
 */
export function getConfig(pannelData) {
  let url, maximumScreenSpaceError
  let tf = false
  if (pannelData) {
    url = pannelData.txtModel
    maximumScreenSpaceError = mars3d.Util.formatNum(pannelData.maximumScreenSpaceError, 1)
  } else {
    url = "//data.mars3d.cn/3dtiles/max-fsdzm/tileset.json"
    maximumScreenSpaceError = 8
    tf = true
  }
 
  const params = {
    name: "模型名称",
    type: "3dtiles",
    url: url,
    maximumScreenSpaceError: maximumScreenSpaceError, // 【重要】数值加大,能让最终成像变模糊
    maximumMemoryUsage: 1024, // 【重要】内存分配变小有利于倾斜摄影数据回收,提升性能体验
    // center: map.getCameraView(),
    show: true
  }
  if (tf) {
    return params
  }
 
  const x = mars3d.Util.formatNum(pannelData.txtX, 6)
  if (x) {
    params.position = params.position || {}
    params.position.lng = x
  }
 
  const y = mars3d.Util.formatNum(pannelData.txtY, 6)
  if (y) {
    params.position = params.position || {}
    params.position.lat = y
  }
 
  const z = mars3d.Util.formatNum(pannelData.txtZ, 6)
  if (z) {
    params.position = params.position || {}
    params.position.alt = z
  }
 
  const rotation_x = mars3d.Util.formatNum(pannelData.rotationX, 1)
  if (rotation_x) {
    params.rotation = params.rotation || {}
    params.rotation.x = rotation_x
  }
 
  const rotation_y = mars3d.Util.formatNum(pannelData.rotationY, 1)
  if (rotation_y) {
    params.rotation = params.rotation || {}
    params.rotation.y = rotation_y
  }
 
  const rotation_z = mars3d.Util.formatNum(pannelData.rotationZ, 1)
  if (rotation_z) {
    params.rotation = params.rotation || {}
    params.rotation.z = rotation_z
  }
 
  const luminanceAtZenith = mars3d.Util.formatNum(pannelData.luminanceAtZenith, 1)
  if (luminanceAtZenith !== 0.2) {
    params.luminanceAtZenith = luminanceAtZenith
  }
 
  const scale = mars3d.Util.formatNum(pannelData.scale || 1, 1)
  if (scale > 0) {
    params.scale = scale
  }
 
  const axis = pannelData.axis
  params.axis = axis
 
  const isProxy = pannelData.chkProxy
  if (isProxy) {
    params.proxy = "//server.mars3d.cn/proxy/"
  }
 
  return params
}
 
// 修改更改后的参数
export function updateModel(pannelData) {
  // 获取参数
  const params = getConfig(pannelData)
 
  params.rotation = params.rotation || {}
  params.rotation.x = params.rotation.x || 0
  params.rotation.y = params.rotation.y || 0
  params.rotation.z = params.rotation.z || 0
 
  if (tiles3dLayer.transform) {
    tilesEditor.heading = tiles3dLayer.rotation_z
    tilesEditor.position = tiles3dLayer.position
  }
  tiles3dLayer.tileset.maximumScreenSpaceError = pannelData.maximumScreenSpaceError
  tiles3dLayer.tileset.luminanceAtZenith = pannelData.luminanceAtZenith
  tiles3dLayer.opacity = pannelData.opacity
  tiles3dLayer.setOptions(params)
  // 深度检测
  map.scene.globe.depthTestAgainstTerrain = pannelData.depthTestAgainstTerrain
  // 鼠标拖拽编辑
  tilesEditor.enabled = pannelData.tilesEditorEnabled
}
 
export function locate() {
  if (tiles3dLayer.tileset?.boundingSphere) {
    map.camera.flyToBoundingSphere(tiles3dLayer.tileset.boundingSphere, {
      offset: new Cesium.HeadingPitchRange(map.camera.heading, map.camera.pitch, tiles3dLayer.tileset.boundingSphere.radius * 2)
    })
  } else {
    map.flyToPoint(tiles3dLayer.position, {
      radius: tiles3dLayer.tileset.boundingSphere.radius * 2
    })
  }
}
 
// 保存GeoJSON
export function saveBookmark(params) {
  if (params.axis === "") {
    delete params.axis
  }
 
  mars3d.Util.downloadFile("3dtiles图层配置.json", JSON.stringify(getConfig(params)))
}
 
function removeLayer() {
  if (tiles3dLayer) {
    map.basemap = 2021 // 切换到默认卫星底图
 
    map.removeLayer(tiles3dLayer, true)
    tiles3dLayer = null
  }
}
 
// 取构件树数据
function querySceneTreeData(url) {
  const scenetree = url.substring(0, url.lastIndexOf("/") + 1) + "scenetree.json"
 
  return mars3d.Util.fetchJson({ url: scenetree })
}
function name2text(o) {
  o.text = o.name
 
  // 这块为了避免tree控件里的id不统一,所以加改变一下
  o.eleid = o.id
  o.id = undefined
 
  if ((!o.text || o.text.trim() === "") && o.type) {
    o.text = o.type
  }
 
  if (o.children) {
    for (let i = 0; i < o.children.length; i++) {
      name2text(o.children[i])
    }
  }
}