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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let primitive
export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  map.fixedLight = true // 固定光照,避免gltf模型随时间存在亮度不一致。
  addRockets()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
let model
 
function addRockets() {
  // // 绑定到UI界面控制参数
  // const viewModel = {
  //   articulations: [],
  //   stages: [],
  //   selectedArticulation: undefined
  // }
 
  // 创建Graphic图层
  const graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  primitive = new mars3d.graphic.ModelPrimitive({
    position: [113.693, 31.243, 220000],
    style: {
      url: "//data.mars3d.cn/gltf/sample/launchvehicle/launchvehicle.glb",
      scale: 20,
      minimumPixelSize: 128,
      heading: 0
    }
  })
  graphicLayer.addGraphic(primitive)
 
  // gltf模型加载完成事件
  primitive.on(mars3d.EventType.load, (event) => {
    model = event.model
 
    // 缩放区域到模型所在位置
    const controller = map.scene.screenSpaceCameraController
    const r = 2.0 * Math.max(model.boundingSphere.radius, map.camera.frustum.near)
    controller.minimumZoomDistance = r * 0.2
 
    const center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, Cesium.Cartesian3.ZERO, new Cesium.Cartesian3())
    const heading = Cesium.Math.toRadians(0.0)
    const pitch = Cesium.Math.toRadians(-10.0)
    map.camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 1.5))
 
    // 设置参数效果
    model.setArticulationStage("LaunchVehicle Pitch", -60) // 火箭整体Pitch角度
    model.setArticulationStage("SRBFlames Size", 1) // 助推器火焰大小
    model.applyArticulations()
 
    // 读取gltf内的构件信息
    const articulationsByName = model._runtime.articulationsByName
    const articulations = Object.keys(articulationsByName).map(function (articulationName) {
      return {
        name: articulationName,
        name_cn: namesCN[articulationName] || articulationName, // 汉化
        stages: articulationsByName[articulationName].stages.map(function (stage) {
          const stageModel = {
            name: stage.name,
            name_cn: namesCN[stage.name] || stage.name, // 汉化
            minimum: stage.minimumValue,
            maximum: stage.maximumValue,
            current: stage.currentValue
          }
          return stageModel
        })
      }
    })
    console.log("完成gltf内的构件信息读取", articulations)
 
    eventTarget.fire("loadGltfModel", { articulations })
  })
}
 
// 设置参数效果
export function setArticulationStage(groupName, stageName, current) {
  const name = groupName + " " + stageName
  model.setArticulationStage(name, Number(current))
  model.applyArticulations()
}
 
// 汉化属性名称
const namesCN = {
  // 属性分组
  LaunchVehicle: "火箭整体",
  Fairing: "整流罩",
 
  UpperStage: "二级部分",
  UpperStageEngines: "二级发动机",
  UpperStageFlames: "二级火焰",
 
  InterstageAdapter: "级间段",
 
  Booster: "一级部分",
  BoosterEngines: "一级发动机",
  BoosterFlames: "一级火焰",
 
  SRBs: "助推器",
  SRBFlames: "助推器火焰",
 
  // 属性
  MoveX: "X方向移动",
  MoveY: "Y方向移动",
  MoveZ: "Z方向移动",
  Yaw: "Yaw角度",
  Pitch: "Pitch角度",
  Roll: "Roll角度",
  Size: "大小",
  Separate: "分离",
  Drop: "下降",
  Open: "打开",
  Rotate: "旋转"
}