Files
zhengte.babylonjs-sdk/src/babylonjs/AppHotspot.ts
2026-04-21 14:58:22 +08:00

174 lines
4.1 KiB
TypeScript

import { Vector3 } from '@babylonjs/core'
import { Monobehiver } from '../base/Monobehiver'
import { HotSpot, HotspotPrams, Point } from '../hotspot'
// import { userSellingPointStore } from '@/stores/zguiy'
import type { MainApp } from './MainApp'
import { Dictionary } from '../utils/Dictionary'
import { EventBridge } from '../event/bridge'
export class AppHotspot extends Monobehiver {
hotSpot!: HotSpot
sllingPointStore: any
//偏移量
offset: number = 0.7
yundong: boolean = false
hotspotDic: Dictionary<HotSpot> = new Dictionary()
constructor(mainApp: MainApp) {
super(mainApp)
// this.sllingPointStore = userSellingPointStore()
}
Awake() {
const hotspot = new HotSpot(this.mainApp)
hotspot.Awake()
this.hotSpot = hotspot;
// 注意:需要从外部传入热点列表,或者从配置中读取
// this.initHotSpot(hotSpotList)
}
render(hotSpotList: Array<any>) {
// 确保 hotSpot 已初始化
if (!this.hotSpot) {
this.Awake();
}
this.initHotSpot(hotSpotList);
}
initHotSpot(hotSpotList: Array<any>) {
hotSpotList.forEach((hotspot: any) => {
this.createHotspot(hotspot)
});
}
createHotspot(hotspot: any) {
// 检查必要的数据
if (!hotspot) {
console.warn('热点数据为空');
return;
}
console.log('热点原始数据:', hotspot);
let position: Vector3;
// 使用 offset 作为 position
if (hotspot.offset) {
if (Array.isArray(hotspot.offset)) {
console.log('offset 数组:', hotspot.offset);
position = new Vector3(
hotspot.offset[0] ?? 0,
hotspot.offset[1] ?? 0,
hotspot.offset[2] ?? 0
);
} else {
position = new Vector3(
hotspot.offset.x ?? 0,
hotspot.offset.y ?? 0,
hotspot.offset.z ?? 0
);
}
} else if (hotspot.position) {
// 兼容 position 字段
if (Array.isArray(hotspot.position)) {
position = new Vector3(
hotspot.position[0] ?? 0,
hotspot.position[1] ?? 0,
hotspot.position[2] ?? 0
);
} else {
position = new Vector3(
hotspot.position.x ?? 0,
hotspot.position.y ?? 0,
hotspot.position.z ?? 0
);
}
} else {
console.warn('热点数据缺少 position 或 offset 字段:', hotspot);
return;
}
console.log('创建热点:', hotspot.name, 'position:', position, 'x:', position.x, 'y:', position.y, 'z:', position.z);
const disposition = Vector3.Zero();
this.hotSpot.Point_Event(
new HotspotPrams(
position,
disposition,
() => {
},
async (p: Point) => {
console.log('热点被点击:', hotspot.name, hotspot.payload)
// 触发热点点击事件
EventBridge.hotspotClick({
id: hotspot.id,
name: hotspot.name,
meshName: hotspot.meshName,
payload: hotspot.payload
})
},
hotspot.icon,
hotspot.radius
)
)
}
clean() {
// 首先隐藏所有热点
this.visible(false);
// 如果存在热点池
if (this.hotSpot && this.hotSpot._point_Pool && this.hotSpot._point_Pool.points) {
// 遍历所有热点
for (let i = 0; i < this.hotSpot._point_Pool.points.length; i++) {
const point = this.hotSpot._point_Pool.points[i];
// 清除事件监听器
if (point.img && point.onCallBack) {
point.img.removeEventListener('mousedown', point.onCallBack);
}
// 从DOM中移除注释元素
if (point.annotation && point.annotation.parentNode) {
point.annotation.parentNode.removeChild(point.annotation);
}
// 释放sprite资源
if (point.sprite) {
point.sprite.dispose();
}
}
// 清空热点池
this.hotSpot._point_Pool.points = [];
}
console.log('热点资源已释放');
}
visible(visible: boolean) {
console.log(visible);
if (this.hotSpot) {
this.hotSpot.Enable_All(visible)
}
}
}