gisviewer-vue3-arcgis
Version:
在应用中引入私服的包, 项目根目录下 新建文件 .npmrc 在 .npmrc 中对包源进行配置:
577 lines (576 loc) • 20.6 kB
JavaScript
import * as v from "@arcgis/core/core/promiseUtils.js";
import * as y from "@arcgis/core/core/reactiveUtils.js";
import T from "@arcgis/core/geometry/SpatialReference";
import * as b from "@arcgis/core/views/3d/externalRenderers";
import * as a from "three";
import { DRACOLoader as C } from "three/examples/jsm/loaders/DRACOLoader";
import { GLTFLoader as f } from "three/examples/jsm/loaders/GLTFLoader";
import { MTLLoader as G } from "three/examples/jsm/loaders/MTLLoader";
import { OBJLoader as w } from "three/examples/jsm/loaders/OBJLoader";
import { toRaw as S } from "vue";
import { EVehiclePlateState as m } from "../../../types/index.mjs";
import B from "../../stores/index.mjs";
class O {
constructor(t) {
this.cameraHeightThreshold = 1e3, this.roughness = 0.2, this.metalness = 0.8, this.defaultMaterial = new a.MeshPhongMaterial({
color: 16777215
}), this.materialMap = /* @__PURE__ */ new Map([
[1, this.createCarMaterial(16777215)],
[2, this.createCarMaterial(7833753)],
[3, this.createCarMaterial(16766720)],
[4, this.createCarMaterial(16758465)],
[5, this.createCarMaterial(14423100)],
[6, this.createCarMaterial(3329330)],
[7, this.createCarMaterial(2003183)],
[8, this.createCarMaterial(16032864)],
[9, this.createCarMaterial(2105376)],
[10, this.createCarMaterial(9662683)],
[99, this.createCarMaterial(6908265)]
]), this.isInitialized = !1, this.showGroundVehicle = !0, this.showElevatedVehicle = !0, this.isPaused = !1, this.currentSpriteContent = m.None, this.historyPositionMap = /* @__PURE__ */ new Map(), this.vehicleObjectMap = /* @__PURE__ */ new Map(), this.tabVisible = !0, this.updateModel = !1, this.appDataStore = B.useAppDataStore, this.logTable = [
[
"ptcId",
"plateno",
"timestamp",
"localTimestamp",
"speed",
"longitude",
"latitude",
"ptcType",
"vehicleType",
"vehicleColor",
"plateColor",
"heading",
"fixAngle",
"roadLayer",
"step",
"receiveTimestamp",
"queueLength",
"status"
]
], this.view = t;
const s = S(this.appDataStore.mapConfig);
this.assetsRoot = s.assetsRoot, document.addEventListener(
"visibilitychange",
() => {
this.clearVehicles(), this.tabVisible = !document.hidden;
},
!1
), y.watch(
() => this.view.camera.position.z,
(e, i) => {
(i <= this.cameraHeightThreshold && e > this.cameraHeightThreshold || i > this.cameraHeightThreshold && e <= this.cameraHeightThreshold) && (this.updateModel = !0);
}
);
}
// private readonly vehiclePool = new VehiclePool();
createCarMaterial(t) {
return new a.MeshPhongMaterial({
color: t
// // emissive: 0xc3c3c3,
// roughness: this.roughness,
// metalness: this.metalness
});
}
getLog() {
return this.logTable;
}
async init() {
await v.eachAlways([
new Promise((t) => {
const s = new C();
s.setDecoderPath(`${this.assetsRoot}/js/libs/draco/`), s.setDecoderConfig({ type: "wasm" }), s.preload();
const e = new f();
e.setDRACOLoader(s), e.load(`${this.assetsRoot}/3DModels/carDraco.glb`, (i) => {
const o = i.scene, r = new a.Box3().setFromObject(o).getSize(new a.Vector3());
this.carModel = new a.Object3D(), this.carModel.rotation.x = a.MathUtils.degToRad(90), this.carModel.add(o), o.position.set(0, 0, r.z / 2), t();
});
}),
new Promise((t) => {
new f().load(`${this.assetsRoot}/3DModels/van.glb`, (e) => {
const i = e.scene, l = new a.Box3().setFromObject(i).getSize(new a.Vector3());
this.vanModel = new a.Object3D(), this.vanModel.rotation.x = a.MathUtils.degToRad(90), this.vanModel.add(i), i.position.set(0, 0, l.z / 2), t();
});
}),
new Promise((t) => {
new f().load(`${this.assetsRoot}/3DModels/truck.glb`, (e) => {
const i = e.scene;
i.scale.set(1.2, 1, 1.5);
const l = new a.Box3().setFromObject(i).getSize(new a.Vector3());
this.truckModel = new a.Object3D(), this.truckModel.rotation.x = a.MathUtils.degToRad(90), this.truckModel.add(i), i.position.set(0, 0, l.z / 2), t();
});
}),
new Promise((t) => {
new f().load(`${this.assetsRoot}/3DModels/bus.glb`, (e) => {
const i = e.scene, l = new a.Box3().setFromObject(i).getSize(new a.Vector3());
this.busModel = new a.Object3D(), this.busModel.rotation.x = a.MathUtils.degToRad(90), this.busModel.add(i), i.position.set(0, 0, l.z / 2), t();
});
}),
new Promise((t) => {
new G().load(
`${this.assetsRoot}/3DModels/bicycle.mtl`,
(e) => {
e.preload();
const i = new w();
i.setMaterials(e), i.load(
`${this.assetsRoot}/3DModels/bicycle.obj`,
(o) => {
this.bicycleModel = o, this.bicycleModel.rotation.x = a.MathUtils.degToRad(90), t();
}
);
}
);
}),
new Promise((t) => {
new w().load(`${this.assetsRoot}/3DModels/male.obj`, (e) => {
e.scale.set(0.01, 0.01, 0.01), this.passengerModel = e, this.passengerModel.rotation.x = a.MathUtils.degToRad(90), this.passengerModel.rotation.y = a.MathUtils.degToRad(180), t();
});
}),
new Promise((t) => {
this.bluePlateBG = new Image(), this.bluePlateBG.src = `${this.assetsRoot}/Images/PlateBG/blue.png`, this.bluePlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.yellowPlateBG = new Image(), this.yellowPlateBG.src = `${this.assetsRoot}/Images/PlateBG/yellow.png`, this.yellowPlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.whitePlateBG = new Image(), this.whitePlateBG.src = `${this.assetsRoot}/Images/PlateBG/white.png`, this.whitePlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.blackPlateBG = new Image(), this.blackPlateBG.src = `${this.assetsRoot}/Images/PlateBG/black.png`, this.blackPlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.neoYellowPlateBG = new Image(), this.neoYellowPlateBG.src = `${this.assetsRoot}/Images/PlateBG/neo_yellow.png`, this.neoYellowPlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.neoGreenPlateBG = new Image(), this.neoGreenPlateBG.src = `${this.assetsRoot}/Images/PlateBG/neo_green.png`, this.neoGreenPlateBG.onload = () => {
t();
};
}),
new Promise((t) => {
this.greyPlateBG = new Image(), this.greyPlateBG.src = `${this.assetsRoot}/Images/PlateBG/grey.png`, this.greyPlateBG.onload = () => {
t();
};
})
]), this.isInitialized = !0;
}
setInterpolate(t) {
this.clearVehicles(), this.tabVisible = t;
}
async setup(t) {
this.context = t, this.renderer = new a.WebGLRenderer({
context: t.gl,
premultipliedAlpha: !0,
logarithmicDepthBuffer: !0,
antialias: !0,
powerPreference: "high-performance"
}), this.renderer.shadowMap.enabled = !0, this.renderer.setPixelRatio(window.devicePixelRatio), this.renderer.setViewport(0, 0, this.view.width, this.view.height), this.renderer.autoClearDepth = !1, this.renderer.autoClearStencil = !1, this.renderer.autoClearColor = !1;
const s = this.renderer.setRenderTarget.bind(
this.renderer
);
this.renderer.setRenderTarget = (i) => {
s(i), i == null && t.bindRenderTarget();
}, this.scene = new a.Scene();
const { camera: e } = t;
this.camera = new a.PerspectiveCamera(
e.fovY,
e.aspect,
0.1,
1e5
), this.ambient = new a.AmbientLight(16777215, 2), this.scene.add(this.ambient), this.sun = new a.DirectionalLight(16777215, 2), this.scene.add(this.sun), t.resetWebGLState();
}
async render(t) {
var i;
const s = t.camera;
if (this.camera.position.set(s.eye[0], s.eye[1], s.eye[2]), this.camera.up.set(s.up[0], s.up[1], s.up[2]), this.camera.lookAt(
new a.Vector3(s.center[0], s.center[1], s.center[2])
), this.camera.projectionMatrix.fromArray(s.projectionMatrix), !this.isPaused && this.tabVisible)
for (const o of this.vehicleObjectMap.keys()) {
const l = (i = this.vehicleObjectMap.get(o)) == null ? void 0 : i.model;
if (!l)
continue;
const r = this.computeVehiclePosition(o);
if (r) {
const d = this.toRenderCoordinates(r);
l.position.set(d[0], d[1], d[2]), l.rotation.y = a.MathUtils.degToRad(-r[3]);
}
}
const e = t.sunLight;
this.sun.position.set(e.direction[0], e.direction[1], e.direction[2]), this.sun.intensity = e.diffuse.intensity, this.sun.color = new a.Color(
e.diffuse.color[0],
e.diffuse.color[1],
e.diffuse.color[2]
), this.ambient.intensity = e.ambient.intensity, this.ambient.color = new a.Color(
e.ambient.color[0],
e.ambient.color[1],
e.ambient.color[2]
), this.renderer.resetState(), t.bindRenderTarget(), this.renderer.render(this.scene, this.camera), b.requestRender(this.view), t.resetWebGLState();
}
/**
* 新增车辆
* */
async addVehicles(t) {
if (this.isPaused || !this.isInitialized || !this.tabVisible)
return;
const s = Date.now();
for (const e of t) {
if (this.historyPositionMap.has(e.vehicleId))
continue;
this.appDataStore.saveTrackLog && this.logTable.push([
e.ptcId,
e.plateNo,
e.timestamp,
e.localTimestamp,
e.speed,
e.x,
e.y,
e.ptcType,
e.vehicleType,
e.vehicleColor,
e.plateColor,
e.heading,
e.fixAngle,
e.roadLayer,
e.step,
s,
0,
0
]);
const { vehicleId: i, localTimestamp: o } = e, l = Number(e.x), r = Number(e.y), d = Number(e.heading);
this.historyPositionMap.set(i, [
{ pos: [l, r, 0], heading: d, time: o }
]);
const h = this.getVehicleModel(e);
h.name = i, h.visible = !1;
try {
const n = await this.createPlateSprite(e);
n && (h.add(n), n.position.set(0, 5, -4));
} catch (n) {
console.log("createPlateSprite error:", n);
}
this.scene.add(h), this.vehicleObjectMap.set(i, {
model: h,
data: e,
waitForDelete: !1,
isMoving: !1
});
}
}
/**
* 更新车辆
* */
async updateVehicles(t) {
if (this.isPaused || !this.isInitialized || !this.tabVisible)
return;
const s = [], e = Date.now();
for (const i of t) {
const { vehicleId: o, localTimestamp: l } = i, r = Number(i.x), d = Number(i.y);
let h = Number(i.heading);
const n = this.vehicleObjectMap.get(o), p = this.historyPositionMap.get(
o
);
if (!n || !p)
s.push(i);
else {
if (this.appDataStore.saveTrackLog && this.logTable.push([
i.ptcId,
i.plateNo,
i.timestamp,
i.localTimestamp,
i.speed,
i.x,
i.y,
i.ptcType,
i.vehicleType,
i.vehicleColor,
i.plateColor,
i.heading,
i.fixAngle,
i.roadLayer,
i.step,
e,
p.length,
1
]), this.updateModel || n.data.vehicleColor !== i.vehicleColor || n.data.vehicleType !== i.vehicleType) {
this.scene.remove(n.model), this.disposeModel(n.model), n.model = this.getVehicleModel(i), n.model.visible = !0;
const c = await this.createPlateSprite(i);
c && (n.model.add(c), c.position.set(0, 5, -4)), this.scene.add(n.model);
}
if (n.data.showName !== i.showName || n.data.plateColor !== i.plateColor) {
const c = n.model.getObjectByName("VehiclePlate");
c && (n.model.remove(c), this.disposeModel(c));
const g = await this.createPlateSprite(i);
g && (n.model.add(g), g.position.set(0, 5, -4));
}
if (n.data = i, this.tabVisible) {
const c = p[p.length - 1];
Math.abs(h - c.heading) >= 180 && (h > c.heading ? c.heading += 360 : h += 360), p.push({
pos: [r, d, 0],
heading: h,
time: l
});
} else {
this.historyPositionMap.set(o, [
{ pos: [r, d, 0], heading: h, time: l }
]);
const c = this.toRenderCoordinates([r, d, 0]);
n.model.position.set(c[0], c[1], c[2]), n.model.rotation.y = a.MathUtils.degToRad(-h);
}
}
}
await this.addVehicles(s), this.updateModel = !1;
}
toRenderCoordinates(t) {
const s = [0, 0, 0];
return b.toRenderCoordinates(
// @ts-ignore
this.view,
t,
0,
T.WGS84,
s,
0,
1
), s;
}
/**
* 删除车辆
* */
deleteVehicles(t) {
if (this.isPaused)
return;
const s = Date.now();
t.forEach((e) => {
if (this.appDataStore.saveTrackLog) {
const o = this.historyPositionMap.get(e);
this.logTable.push([
e,
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
s,
o ? o.length : 0,
2
]);
}
const i = this.vehicleObjectMap.get(e);
i && (this.tabVisible ? i.isMoving ? i.waitForDelete = !0 : this.deleteVehicle(i) : this.deleteVehicle(i));
});
}
deleteVehicle(t) {
t.model.visible = !1, this.disposeModel(t.model), this.scene.remove(t.model);
const s = t.data.vehicleId;
this.vehicleObjectMap.delete(s), this.historyPositionMap.delete(s);
}
async toggleTrafficInfo(t) {
t.name === "vehiclePlate" && (t.visible === !0 && this.currentSpriteContent === m.None ? await this.updatePanelContent(m.PlateNumber) : await this.updatePanelContent(
t.visible ? this.currentSpriteContent : m.None
));
}
togglePause(t) {
this.isPaused = t;
}
/**
* 清除全部车辆
* */
clearVehicles() {
for (const t of this.vehicleObjectMap.values())
this.disposeModel(t.model), this.scene.remove(t.model);
this.vehicleObjectMap.clear(), this.historyPositionMap.clear();
}
/**
* 设置号牌显示状态
* @param contentType
* @returns
*/
async updatePanelContent(t) {
this.currentSpriteContent = t;
for (const s of this.vehicleObjectMap.values()) {
const e = s.model.getObjectByName("VehiclePlate");
if (e && (s.model.remove(e), this.disposeModel(e)), t !== m.None) {
const i = await this.createPlateSprite(s.data);
i && (s.model.add(i), i.position.set(0, 5, -4));
}
}
}
toggleGroundVehicle(t) {
this.showGroundVehicle = t;
}
toggleElevatedVehicle(t) {
this.showElevatedVehicle = t;
}
/**
* 释放模型资源
* */
disposeModel(t) {
t.traverse((s) => {
if (s instanceof a.Mesh)
try {
s.geometry.dispose(), s.material.dispose();
} catch {
}
});
}
/**
* 计算车辆位置
* */
computeVehiclePosition(t) {
const s = this.historyPositionMap.get(t), e = this.vehicleObjectMap.get(t);
if (!e || !s || !e.isMoving && s.length <= 2)
return;
const i = Date.now();
e.isMoving = !0, e.data.roadLayer === "1" ? e.model.visible = this.showGroundVehicle : e.model.visible = this.showElevatedVehicle, e.segmentStartTime || (e.segmentStartTime = i, e.segmentTotalTime = s[1].time - s[0].time);
const o = i - e.segmentStartTime, l = Math.min(
o / e.segmentTotalTime,
1
);
if (l === 1)
if (s.shift(), s.length === 1) {
e.waitForDelete === !0 ? this.deleteVehicle(e) : (e.segmentStartTime = void 0, e.segmentTotalTime = void 0, e.model.visible = !1, e.isMoving = !1, console.log("hide vehicle", t));
return;
} else {
e.segmentStartTime = Date.now();
const r = o - e.segmentTotalTime;
return e.segmentTotalTime = s[1].time - s[0].time - r, Math.abs(s[1].heading - s[0].heading) > 180 && (s[1].heading > s[0].heading ? s[0].heading += 360 : s[1].heading += 360), s[0].pos.concat(s[0].heading);
}
else if (s.length >= 2) {
const r = s[0].pos[0] + (s[1].pos[0] - s[0].pos[0]) * l, d = s[0].pos[1] + (s[1].pos[1] - s[0].pos[1]) * l, h = s[0].heading + (s[1].heading - s[0].heading) * l;
return [r, d, 0, h];
} else
return;
}
/**
* 根据车辆类型、车身颜色获取模型
* */
getVehicleModel(t) {
if (this.view.camera.position.z >= this.cameraHeightThreshold) {
const s = new a.SphereGeometry(5, 32, 32), e = new a.MeshPhysicalMaterial({
color: 325253,
emissive: 0,
roughness: this.roughness,
metalness: this.metalness
});
return new a.Mesh(s, e);
} else {
let s;
if (t.ptcType === 2)
s = this.bicycleModel.clone();
else if (t.ptcType === 3)
s = this.passengerModel.clone();
else {
switch (t.vehicleType) {
case 10:
s = this.carModel.clone();
break;
case 20:
s = this.vanModel.clone();
break;
case 25:
s = this.truckModel.clone();
break;
case 50:
s = this.busModel.clone();
break;
default:
s = this.carModel.clone();
break;
}
const e = this.materialMap.get(t.vehicleColor) || this.defaultMaterial;
let i = !1;
s.traverse((o) => {
!i && o instanceof a.Mesh && (o.material = e, i = !0);
});
}
return s;
}
}
/**
* 创建号牌canvas
* */
createCanvas(t, s, e) {
const i = document.createElement("canvas"), o = t.width, l = t.height;
i.width = o, i.height = l;
const r = i.getContext("2d");
if (!r) {
console.log("canvas创建失败");
return;
}
return r.fillStyle = "rgba(0,0,0,0.0)", r.fillRect(0, 0, o, l), r.drawImage(t, 0, 0, o, l), r.beginPath(), r.translate(o / 2, l / 2), r.fillStyle = e, r.font = "bold 32px 宋体", r.textBaseline = "middle", r.textAlign = "center", r.fillText(s, 0, 0), i;
}
createPlateSprite(t) {
return new Promise((s, e) => {
var h, n;
const i = !t.plateNo || t.plateNo === "0" || t.plateNo === "000000";
if (this.currentSpriteContent === m.None || this.currentSpriteContent === m.PlateNumber && i) {
s(void 0);
return;
}
let o = new Image(), l = "", r = "";
if (this.currentSpriteContent === m.PlateNumber || this.currentSpriteContent === m.Mix)
if (i)
o = this.greyPlateBG, l = t.ptcId, r = "#ffffff";
else
switch (l = ((h = t.showName) == null ? void 0 : h.substring(0, 2)) + "•" + ((n = t.showName) == null ? void 0 : n.substring(2)), t.plateColor) {
case 1:
o = this.bluePlateBG, r = "#ffffff";
break;
case 2:
o = this.yellowPlateBG, r = "#000000";
break;
case 3:
o = this.whitePlateBG, r = "#000000";
break;
case 4:
o = this.blackPlateBG, r = "#ffffff";
break;
case 5:
o = this.neoYellowPlateBG, r = "#000000";
break;
case 6:
o = this.neoGreenPlateBG, r = "#000000";
break;
default:
o = this.greyPlateBG, l = t.plateNo, r = "#ffffff";
break;
}
else
this.currentSpriteContent === m.Id && (o = this.greyPlateBG, l = t.ptcId, r = "#ffffff");
const d = this.createCanvas(o, l, r);
if (d) {
const p = new a.CanvasTexture(d), c = new a.SpriteMaterial({
map: p,
transparent: !1
}), g = new a.Sprite(c), u = 0.05, M = d.width * u, P = d.height * u;
g.scale.set(M, P, 1), g.name = "VehiclePlate", s(g);
} else {
e("canvas创建失败");
return;
}
o.onerror = (p) => {
console.log(`号牌背景加载失败: ${o.src}`, p), e(p);
};
});
}
}
export {
O as default
};