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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let graphicLayer // 矢量图层对象,用于graphic绑定展示
let tilesetLayer // 3dtiles模型;添加模型选择
let tilesetFlat
 
export const mapOptions = {
  scene: {
    center: { lat: 34.215539, lng: 108.959582, alt: 817, heading: 2, pitch: -46 }
  }
}
 
export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  map.fixedLight = true // 固定光照,避免gltf模型随时间存在亮度不一致。
 
  globalNotify(
    "已知问题提示",
    `(1) 对3dtiles数据有要求,仅适用于无自带着色器的纹理格式模型。
     (2) 目前不支持所有3dtile数据,请替换url进行自测`
  )
 
  // 创建矢量数据图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  showDytDemo(true, [
    [108.959062, 34.220134, 397.3],
    [108.959802, 34.220147, 397.6],
    [108.959779, 34.219506, 398.7],
    [108.959106, 34.21953, 398.1]
  ])
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
export function showDytDemo(chkShowLine, positions) {
  removeLayer()
 
  // 加模型
  tilesetLayer = new mars3d.layer.TilesetLayer({
    name: "大雁塔",
    url: "//data.mars3d.cn/3dtiles/qx-dyt/tileset.json",
    position: { alt: -27 },
    maximumScreenSpaceError: 1,
    maximumMemoryUsage: 1024,
    flyTo: true
  })
  map.addLayer(tilesetLayer)
 
  createTilesetFlat(chkShowLine, positions)
}
 
export function showTehDemo(chkShowLine) {
  removeLayer()
 
  // 以下数据为cesiumlab v3处理,目前其材质有做偏移处理,不知道内部逻辑及具体值,无法平整压平。
  tilesetLayer = new mars3d.layer.TilesetLayer({
    name: "合肥天鹅湖",
    url: "//data.mars3d.cn/3dtiles/qx-teh/tileset.json",
    position: { lng: 117.218434, lat: 31.81807, alt: 163 },
    maximumScreenSpaceError: 16,
    maximumMemoryUsage: 1024,
    dynamicScreenSpaceError: true,
    cullWithChildrenBounds: false,
    skipLevelOfDetail: true,
    preferLeaves: true,
    center: { lat: 31.795308, lng: 117.21948, alt: 1820, heading: 0, pitch: -39 },
    flyTo: true
  })
  map.addLayer(tilesetLayer)
  createTilesetFlat(chkShowLine)
}
 
export function showQxShequDemo(chkShowLine) {
  removeLayer()
 
  tilesetLayer = new mars3d.layer.TilesetLayer({
    name: "县城社区",
    url: "//data.mars3d.cn/3dtiles/qx-shequ/tileset.json",
    position: { alt: 11.5 },
    maximumScreenSpaceError: 1,
    maximumMemoryUsage: 1024,
    dynamicScreenSpaceError: true,
    cullWithChildrenBounds: false,
    skipLevelOfDetail: true,
    preferLeaves: true,
    center: { lat: 28.439062, lng: 119.479517, alt: 484, heading: 4, pitch: -63 },
    flyTo: true
  })
  map.addLayer(tilesetLayer)
 
  createTilesetFlat(chkShowLine)
}
 
// 添加模型处理类,默认positions为大雁塔的压平位置
function createTilesetFlat(chkShowLine, positions) {
  if (tilesetFlat) {
    map.removeThing(tilesetFlat)
    tilesetFlat = null
  }
 
  // 模型压平处理类
  tilesetFlat = new mars3d.thing.TilesetFlat({
    layer: tilesetLayer,
    positions: positions
  })
  map.addThing(tilesetFlat)
 
  // 模型加载完成方法一
  tilesetLayer.readyPromise.then((e) => {
    tilesetFlat.list.forEach((item) => {
      const id = addTestLine(chkShowLine, item.positions)
      addTableItem({ id: id, item: item })
    })
  })
}
 
function removeLayer() {
  if (tilesetLayer) {
    map.removeLayer(tilesetLayer, true)
    tilesetLayer = null
  }
 
  if (tilesetFlat) {
    removeAll()
 
    map.removeThing(tilesetFlat)
    tilesetFlat = null
  }
}
 
// 添加矩形
export function btnDrawExtent(chkShowLine, height) {
  map.graphicLayer.clear()
  map.graphicLayer.startDraw({
    type: "rectangle",
    style: {
      color: "#007be6",
      opacity: 0.2,
      outline: false
    },
    success: function (graphic) {
      // 绘制成功后回调
      const positions = graphic.getOutlinePositions(false)
      map.graphicLayer.clear()
 
      const id = addTestLine(chkShowLine, positions)
      console.log("绘制坐标为", JSON.stringify(mars3d.PointTrans.cartesians2lonlats(positions))) // 方便测试拷贝坐标
 
      const item = tilesetFlat.addArea(positions, { height: height })
 
      addTableItem({ id: id, item: item })
    }
  })
}
// 绘制多边形
export function btnDraw(chkShowLine, height) {
  map.graphicLayer.clear()
  map.graphicLayer.startDraw({
    type: "polygon",
    style: {
      color: "#007be6",
      opacity: 0.5
    },
    success: function (graphic) {
      // 绘制成功后回调
      const positions = graphic.positionsShow
      map.graphicLayer.clear()
 
      const id = addTestLine(chkShowLine, positions)
      console.log("绘制坐标为", JSON.stringify(mars3d.PointTrans.cartesians2lonlats(positions))) // 方便测试拷贝坐标
 
      const item = tilesetFlat.addArea(positions, { height: height })
 
      addTableItem({ id: id, item: item })
    }
  })
}
// 清除
export function removeAll() {
  tilesetFlat.clear()
  map.graphicLayer.clear()
  graphicLayer.eachGraphic((graphic) => {
    graphicLayer.removeGraphic(graphic)
  })
}
 
// 改变压平的高度
export function changeFlatHeight(val) {
  tilesetFlat.updateHeight(val)
}
 
// 是否显示测试边界线
export function chkShowLine(val) {
  graphicLayer.eachGraphic((graphic) => {
    graphic.show = val
  })
}
 
function addTestLine(chkShowLine, positions) {
  const graphic = new mars3d.graphic.PolylineEntity({
    positions: positions,
    style: {
      closure: true,
      color: "#ffffff",
      opacity: 0.8,
      width: 2,
      clampToGround: true
    },
    show: chkShowLine
  })
  graphicLayer.addGraphic(graphic)
 
  return graphic.id
}
 
// 触发自定义事件 addItem
function addTableItem(data) {
  eventTarget.fire("addItem", { data })
}
 
export function showHideArea(id, selected) {
  if (selected) {
    tilesetFlat.showArea(id)
  } else {
    tilesetFlat.hideArea(id)
  }
}
 
// 定位至模型
export function flyToGraphic(item) {
  const graphic = tilesetFlat.getAreaById(item)
  map.flyToPositions(graphic.positions)
}
 
// 删除模型
export function deletedGraphic(key, id) {
  const graphic = tilesetFlat.getAreaById(key)
  tilesetFlat.removeArea(graphic)
 
  const graphicLine = graphicLayer.getGraphicById(id)
  graphicLayer.removeGraphic(graphicLine)
}