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
import * as mars3d from "mars3d"
import ThreeLayer from "./ThreeLayer"
 
export let map // mars3d.Map三维地图对象
let threeLayer
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 30.980053, lng: 117.375049, alt: 110976, heading: 357, pitch: -50 }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  threeLayer = new ThreeLayer()
  map.addLayer(threeLayer)
 
  init3DObject()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function init3DObject() {
  let minWGS84 = [117.142184, 31.869697]
  let maxWGS84 = [117.357015, 31.713898]
  const ce = Cesium.Cartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2 - 1, 200000)
 
  let geometry = new THREE.SphereGeometry(1, 32, 32)
  const sphere = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: 0xffffff, side: THREE.DoubleSide })) // 12面体
 
  // translate "up" in Three.js space so the "bottom" of the mesh is the handle
  sphere.scale.set(5000, 5000, 5000)
  sphere.uuid = "sphere"
  const sphereYup = new THREE.Group()
  sphereYup.add(sphere)
  threeLayer.scene.add(sphereYup) // don’t forget to add it to the Three.js scene manually
  sphereYup.position.set(ce.x, ce.y, ce.z)
 
  const arrXdObj = []
 
  let xdObj = new XDObject()
  xdObj.threeMesh = sphereYup
  xdObj.minWGS84 = minWGS84
  xdObj.maxWGS84 = maxWGS84
  arrXdObj.push(xdObj)
 
  geometry = new THREE.DodecahedronGeometry()
  const dodecahedronMesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial()) // 12面体
  dodecahedronMesh.scale.set(5000, 5000, 5000)
  dodecahedronMesh.position.z += 15000
  // translate "up" in Three.js space so the "bottom" of the mesh is the handle
  dodecahedronMesh.rotation.x = Math.PI / 2 // rotate mesh for Cesium's Y-up system
  dodecahedronMesh.uuid = "12面体"
 
  const dodecahedronMeshYup = new THREE.Group()
  dodecahedronMeshYup.add(dodecahedronMesh)
  threeLayer.scene.add(dodecahedronMeshYup) // don’t forget to add it to the Three.js scene manually
  dodecahedronMeshYup.position.set(ce.x, ce.y, ce.z)
 
  //    Assign Three.js object mesh to our object array
  xdObj = new XDObject()
  xdObj.threeMesh = dodecahedronMeshYup
  xdObj.minWGS84 = minWGS84
  xdObj.maxWGS84 = maxWGS84
  arrXdObj.push(xdObj)
 
  // 添加灯光,点光源
  const spotLight = new THREE.SpotLight(0xffffff)
  spotLight.position.set(0, 0, 50000)
  spotLight.castShadow = true // 设置光源投射阴影
  spotLight.intensity = 1
  sphereYup.add(spotLight)
 
  // 添加环境光
  const hemiLight = new THREE.HemisphereLight(0xff0000, 0xff0000, 1)
  sphereYup.add(hemiLight)
 
  const cartToVec = function (cart) {
    return new THREE.Vector3(cart.x, cart.y, cart.z)
  }
 
  // Configure Three.js meshes to stand against globe center position up direction
  for (const id in arrXdObj) {
    minWGS84 = arrXdObj[id].minWGS84
    maxWGS84 = arrXdObj[id].maxWGS84
    // convert lat/long center position to Cartesian3
    const center = Cesium.Cartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2)
 
    // get forward direction for orienting model
    const centerHigh = Cesium.Cartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2, 1)
 
    // use direction from bottom left to top left as up-vector
    const bottomLeft = cartToVec(Cesium.Cartesian3.fromDegrees(minWGS84[0], minWGS84[1]))
    const topLeft = cartToVec(Cesium.Cartesian3.fromDegrees(minWGS84[0], maxWGS84[1]))
    const latDir = new THREE.Vector3().subVectors(bottomLeft, topLeft).normalize()
 
    // configure entity position and orientation
    arrXdObj[id].threeMesh.position.copy(center)
    arrXdObj[id].threeMesh.lookAt(centerHigh)
    arrXdObj[id].threeMesh.up.copy(latDir)
  }
}
 
function XDObject() {
  this.threeMesh = null
  this.minWGS84 = null
  this.maxWGS84 = null
}