| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <template>
- <div class="MapD3">
- <VciD3
- :before-init-qt="beforeInitQt"
- class="vci-size-100"
- @ready="ready"
- />
- <slot></slot>
- </div>
- </template>
- <script>
- // power by visual-ceiling, at 2024/7/8 上午11:29
- import VciD3 from "../VciD3.vue";
- import { mergeDeep } from "@vci/helper/src/object";
- import { getChunkPluginsEffect } from "../../src/plugins/chunk";
- import { createEnvEXR } from "../../src/creator/env/createEnvEXR";
- import {
- FileLoader,
- Mesh,
- MeshBasicMaterial,
- PlaneGeometry,
- SRGBColorSpace,
- TextureLoader,
- Vector2
- } from "three/webgpu";
- import { degToRad } from "three/src/math/MathUtils";
- import { createLoaderGltf } from "../../src/creator/loaders/createLoaderGltf";
- import { PluginRenderer2D } from "../../src/plugins/PluginRenderer2D";
- import { PluginRenderer3D } from "../../src/plugins/PluginRenderer3D";
- import { PluginThings } from "../../src/plugins/PluginThings";
- import World from "./things/World";
- import MixinMapQt from "./mixins/MixinMapQt";
- import { ThingEvents } from "../../src/events/ThingEvents";
- import Thing from "../../src/core/Thing";
- import CameraControls from "camera-controls";
- import { PluginNotification } from "@vci/vci/vue/v2/components/other/VpNotification";
- import { resourceRootAliyun } from "@vci/vci/config/api";
- import { PluginEdit } from "../../src/plugins/PluginEdit";
- export default {
- name: "MapD3",
- components: { VciD3 },
- mixins: [MixinMapQt],
- props: {
- debug: {
- type: Boolean,
- default: false
- },
- mode: {
- type: String,
- default: "2D"
- },
- autoAnimationIn: {
- type: Boolean,
- default: false
- },
- // china|world
- scope: {
- type: String,
- default: "china"
- },
- center: {
- type: Array,
- default: () => [107.88219877049586, 30.048056747015057]
- },
- mapUrl: {
- type: String,
- default: null
- },
- mapPosition: {
- type: Array,
- default: null
- },
- size: {
- type: Array,
- default: () => [1000e4, 1000e4]
- }
- },
- data() {
- return {};
- },
- methods: {
- beforeInitQt(option) {
- const loading = this.$children.find(vm => vm.$attrs.name === "loading");
- mergeDeep(option, {
- debug: this.debug,
- plugins: getChunkPluginsEffect(loading).concat([
- PluginRenderer2D,
- PluginRenderer3D,
- PluginThings
- ])
- });
- },
- async ready(qt) {
- this.setQt(qt);
- // 启用双击事件
- qt.events.enableDblClick = true;
- // 调整相机及控制器
- qt.camera.near = .01;
- qt.camera.far = 1e8;
- qt.camera.updateProjectionMatrix();
- qt.control.maxDistance = .4e8;
- qt.control.maxPolarAngle = degToRad(88);
- qt.control.setLookAt(
- 0, 1e3, 0,
- 0, 0, 0,
- false
- );
- // 载入资源
- const pluginLoadingManager = qt.plugin("PluginLoadingManager");
- !pluginLoadingManager && qt.log.warn("PluginLoadingManager未安装,资源载入进度");
- const loadingManager = pluginLoadingManager ? pluginLoadingManager.manager : undefined;
- // 启用资源缓存
- pluginLoadingManager && (pluginLoadingManager.enableCache = true);
- qt.loaderGltf = createLoaderGltf({ qt });
- const [
- dataGeoWorld,
- dataGeoChina
- ] = await Promise.all([
- new FileLoader(loadingManager).loadAsync(`${resourceRootAliyun}/geo/world.geojson`),
- new FileLoader(loadingManager).loadAsync(`${resourceRootAliyun}/geo/china.geojson`),
- createEnvEXR(qt, `${resourceRootAliyun}/textures/kloppenheim_06_puresky_2k.exr`)
- ]);
- // 调整环境贴图
- qt.scene.background = null;
- // 构建资源体系
- qt.assets = {
- models: {},
- geos: {
- world: JSON.parse(dataGeoWorld),
- china: JSON.parse(dataGeoChina)
- },
- textures: {}
- };
- // 创建世界
- qt.thingWorld = new World({
- qt,
- enableDisplayWorld: this.debug && this.scope !== "china",
- enableDisplayChina: this.debug
- });
- qt.thingWorld.loadingManager = loadingManager;
- qt.thingWorld.addEventListener(ThingEvents.Click, e => {
- const { detail } = e;
- const vec3Point = detail.targetRay.point;
- const lngLatWGS84 = qt.thingWorld.coordsToLngLat([vec3Point.x, vec3Point.z]);
- console.info(`当前鼠标拾取的经纬度(WGS84): `, lngLatWGS84.join(","));
- });
- // 添加大地坐标拾取功能
- window.addEventListener("keydown", e => {
- if(e.key === "Enter") {
- if(qt.thingWorld) {
- if(!qt.thingWorld.enableEvent) {
- qt.thingWorld.wakeUp();
- } else {
- qt.thingWorld.sleep();
- }
- PluginNotification.tip(`已${qt.thingWorld.enableEvent ? "开启" : "关闭"}鼠标拾取大地坐标`);
- }
- }
- });
- // 补丁: PluginEdit在大范围世界地图进行模型编辑时不能移动模型
- qt.addEventListener(PluginEdit.Events.AfterInEdit, e => {
- const { plugin: editor } = e.detail;
- const oldGeo = editor.controlTransform._plane.geometry;
- editor.controlTransform._plane.geometry = new PlaneGeometry(100e8, 100e8, 2, 2);
- oldGeo.dispose();
- });
- // 调整色调
- // 重置加载状态
- pluginLoadingManager.isLoaded = false;
- // 设置中心点
- qt.thingWorld.setOffsetFromLngLat(this.center);
- // 设置地图为2D
- if(this.mode === "2D") qt.control.minPolarAngle = qt.control.maxPolarAngle = 0;
- // 设置鼠标左键平移、右键旋转
- qt.control.mouseButtons.left = CameraControls.ACTION.TRUCK;
- qt.control.mouseButtons.right = this.mode === "2D" ? CameraControls.ACTION.NONE : CameraControls.ACTION.ROTATE;
- // 载入地图资源
- if(this.mapUrl) {
- const [map] = await Promise.all([
- new TextureLoader(qt.thingWorld.loadingManager).loadAsync(this.mapUrl)
- ]);
- // 创建图片地图
- this.createGraphicMap(map);
- }
- // 动画入场
- this.autoAnimationIn && await qt.plugin("PluginAnimation").animationInAsync();
- // 标记地图准备完成
- this.$emit("ready", qt);
- // 设置地图缩放层级
- await qt.control.setLookAt(
- 0, 1e7, 0,
- 0, 0, 0,
- true
- );
- },
- // 创建图片地图
- createGraphicMap(map) {
- const { qt } = this;
- // // 基准图配置
- // const size = new Vector2(1374, 1010)
- // size.multiplyScalar(5.01e3)
- const size = new Vector2().fromArray(this.size);
- map.colorSpace = SRGBColorSpace;
- const geo = new PlaneGeometry(size.x, size.y, 9, 9);
- geo.rotateX(degToRad(-90));
- geo.translate(0, this.debug ? 5 : 0, 0);
- const mtl = new MeshBasicMaterial({
- transparent: true,
- opacity: this.debug ? .2 : 1,
- map
- });
- const o3 = new Mesh(geo, mtl);
- qt.thingGraphicMap = new Thing({
- qt,
- enableEvent: false,
- position: this.mapPosition ? qt.thingWorld.lngLatToCoords(this.mapPosition, 0) : [0, 0, 0],
- o3
- });
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .MapD3 {
- position: relative;
- width: 100%;
- height: 100%;
- .VciD3 {
- z-index: 1;
- }
- }
- </style>
|