MapD3.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div class="MapD3">
  3. <VciD3
  4. :before-init-qt="beforeInitQt"
  5. class="vci-size-100"
  6. @ready="ready"
  7. />
  8. <slot></slot>
  9. </div>
  10. </template>
  11. <script>
  12. // power by visual-ceiling, at 2024/7/8 上午11:29
  13. import VciD3 from "../VciD3.vue";
  14. import { mergeDeep } from "@vci/helper/src/object";
  15. import { getChunkPluginsEffect } from "../../src/plugins/chunk";
  16. import { createEnvEXR } from "../../src/creator/env/createEnvEXR";
  17. import {
  18. FileLoader,
  19. Mesh,
  20. MeshBasicMaterial,
  21. PlaneGeometry,
  22. SRGBColorSpace,
  23. TextureLoader,
  24. Vector2
  25. } from "three/webgpu";
  26. import { degToRad } from "three/src/math/MathUtils";
  27. import { createLoaderGltf } from "../../src/creator/loaders/createLoaderGltf";
  28. import { PluginRenderer2D } from "../../src/plugins/PluginRenderer2D";
  29. import { PluginRenderer3D } from "../../src/plugins/PluginRenderer3D";
  30. import { PluginThings } from "../../src/plugins/PluginThings";
  31. import World from "./things/World";
  32. import MixinMapQt from "./mixins/MixinMapQt";
  33. import { ThingEvents } from "../../src/events/ThingEvents";
  34. import Thing from "../../src/core/Thing";
  35. import CameraControls from "camera-controls";
  36. import { PluginNotification } from "@vci/vci/vue/v2/components/other/VpNotification";
  37. import { resourceRootAliyun } from "@vci/vci/config/api";
  38. import { PluginEdit } from "../../src/plugins/PluginEdit";
  39. export default {
  40. name: "MapD3",
  41. components: { VciD3 },
  42. mixins: [MixinMapQt],
  43. props: {
  44. debug: {
  45. type: Boolean,
  46. default: false
  47. },
  48. mode: {
  49. type: String,
  50. default: "2D"
  51. },
  52. autoAnimationIn: {
  53. type: Boolean,
  54. default: false
  55. },
  56. // china|world
  57. scope: {
  58. type: String,
  59. default: "china"
  60. },
  61. center: {
  62. type: Array,
  63. default: () => [107.88219877049586, 30.048056747015057]
  64. },
  65. mapUrl: {
  66. type: String,
  67. default: null
  68. },
  69. mapPosition: {
  70. type: Array,
  71. default: null
  72. },
  73. size: {
  74. type: Array,
  75. default: () => [1000e4, 1000e4]
  76. }
  77. },
  78. data() {
  79. return {};
  80. },
  81. methods: {
  82. beforeInitQt(option) {
  83. const loading = this.$children.find(vm => vm.$attrs.name === "loading");
  84. mergeDeep(option, {
  85. debug: this.debug,
  86. plugins: getChunkPluginsEffect(loading).concat([
  87. PluginRenderer2D,
  88. PluginRenderer3D,
  89. PluginThings
  90. ])
  91. });
  92. },
  93. async ready(qt) {
  94. this.setQt(qt);
  95. // 启用双击事件
  96. qt.events.enableDblClick = true;
  97. // 调整相机及控制器
  98. qt.camera.near = .01;
  99. qt.camera.far = 1e8;
  100. qt.camera.updateProjectionMatrix();
  101. qt.control.maxDistance = .4e8;
  102. qt.control.maxPolarAngle = degToRad(88);
  103. qt.control.setLookAt(
  104. 0, 1e3, 0,
  105. 0, 0, 0,
  106. false
  107. );
  108. // 载入资源
  109. const pluginLoadingManager = qt.plugin("PluginLoadingManager");
  110. !pluginLoadingManager && qt.log.warn("PluginLoadingManager未安装,资源载入进度");
  111. const loadingManager = pluginLoadingManager ? pluginLoadingManager.manager : undefined;
  112. // 启用资源缓存
  113. pluginLoadingManager && (pluginLoadingManager.enableCache = true);
  114. qt.loaderGltf = createLoaderGltf({ qt });
  115. const [
  116. dataGeoWorld,
  117. dataGeoChina
  118. ] = await Promise.all([
  119. new FileLoader(loadingManager).loadAsync(`${resourceRootAliyun}/geo/world.geojson`),
  120. new FileLoader(loadingManager).loadAsync(`${resourceRootAliyun}/geo/china.geojson`),
  121. createEnvEXR(qt, `${resourceRootAliyun}/textures/kloppenheim_06_puresky_2k.exr`)
  122. ]);
  123. // 调整环境贴图
  124. qt.scene.background = null;
  125. // 构建资源体系
  126. qt.assets = {
  127. models: {},
  128. geos: {
  129. world: JSON.parse(dataGeoWorld),
  130. china: JSON.parse(dataGeoChina)
  131. },
  132. textures: {}
  133. };
  134. // 创建世界
  135. qt.thingWorld = new World({
  136. qt,
  137. enableDisplayWorld: this.debug && this.scope !== "china",
  138. enableDisplayChina: this.debug
  139. });
  140. qt.thingWorld.loadingManager = loadingManager;
  141. qt.thingWorld.addEventListener(ThingEvents.Click, e => {
  142. const { detail } = e;
  143. const vec3Point = detail.targetRay.point;
  144. const lngLatWGS84 = qt.thingWorld.coordsToLngLat([vec3Point.x, vec3Point.z]);
  145. console.info(`当前鼠标拾取的经纬度(WGS84): `, lngLatWGS84.join(","));
  146. });
  147. // 添加大地坐标拾取功能
  148. window.addEventListener("keydown", e => {
  149. if(e.key === "Enter") {
  150. if(qt.thingWorld) {
  151. if(!qt.thingWorld.enableEvent) {
  152. qt.thingWorld.wakeUp();
  153. } else {
  154. qt.thingWorld.sleep();
  155. }
  156. PluginNotification.tip(`已${qt.thingWorld.enableEvent ? "开启" : "关闭"}鼠标拾取大地坐标`);
  157. }
  158. }
  159. });
  160. // 补丁: PluginEdit在大范围世界地图进行模型编辑时不能移动模型
  161. qt.addEventListener(PluginEdit.Events.AfterInEdit, e => {
  162. const { plugin: editor } = e.detail;
  163. const oldGeo = editor.controlTransform._plane.geometry;
  164. editor.controlTransform._plane.geometry = new PlaneGeometry(100e8, 100e8, 2, 2);
  165. oldGeo.dispose();
  166. });
  167. // 调整色调
  168. // 重置加载状态
  169. pluginLoadingManager.isLoaded = false;
  170. // 设置中心点
  171. qt.thingWorld.setOffsetFromLngLat(this.center);
  172. // 设置地图为2D
  173. if(this.mode === "2D") qt.control.minPolarAngle = qt.control.maxPolarAngle = 0;
  174. // 设置鼠标左键平移、右键旋转
  175. qt.control.mouseButtons.left = CameraControls.ACTION.TRUCK;
  176. qt.control.mouseButtons.right = this.mode === "2D" ? CameraControls.ACTION.NONE : CameraControls.ACTION.ROTATE;
  177. // 载入地图资源
  178. if(this.mapUrl) {
  179. const [map] = await Promise.all([
  180. new TextureLoader(qt.thingWorld.loadingManager).loadAsync(this.mapUrl)
  181. ]);
  182. // 创建图片地图
  183. this.createGraphicMap(map);
  184. }
  185. // 动画入场
  186. this.autoAnimationIn && await qt.plugin("PluginAnimation").animationInAsync();
  187. // 标记地图准备完成
  188. this.$emit("ready", qt);
  189. // 设置地图缩放层级
  190. await qt.control.setLookAt(
  191. 0, 1e7, 0,
  192. 0, 0, 0,
  193. true
  194. );
  195. },
  196. // 创建图片地图
  197. createGraphicMap(map) {
  198. const { qt } = this;
  199. // // 基准图配置
  200. // const size = new Vector2(1374, 1010)
  201. // size.multiplyScalar(5.01e3)
  202. const size = new Vector2().fromArray(this.size);
  203. map.colorSpace = SRGBColorSpace;
  204. const geo = new PlaneGeometry(size.x, size.y, 9, 9);
  205. geo.rotateX(degToRad(-90));
  206. geo.translate(0, this.debug ? 5 : 0, 0);
  207. const mtl = new MeshBasicMaterial({
  208. transparent: true,
  209. opacity: this.debug ? .2 : 1,
  210. map
  211. });
  212. const o3 = new Mesh(geo, mtl);
  213. qt.thingGraphicMap = new Thing({
  214. qt,
  215. enableEvent: false,
  216. position: this.mapPosition ? qt.thingWorld.lngLatToCoords(this.mapPosition, 0) : [0, 0, 0],
  217. o3
  218. });
  219. }
  220. }
  221. };
  222. </script>
  223. <style lang="scss" scoped>
  224. .MapD3 {
  225. position: relative;
  226. width: 100%;
  227. height: 100%;
  228. .VciD3 {
  229. z-index: 1;
  230. }
  231. }
  232. </style>