import type { Device } from '@antv/g-device-api'; import type { ProceduralGeometry } from '@antv/g-plugin-3d'; let DEVICE: Device; const GEOMETRY_CACHE = new Map(); /** * 创建几何体 * * Create geometry * @param type - 几何体类型 geometry type * @param device - 设备对象 device object * @param Ctor - 几何体构造函数 geometry constructor * @param style - 几何体样式 geometry style * @returns 几何体对象 geometry object */ export function createGeometry>( type: string, device: Device, Ctor: new (...args: any[]) => T, style: Record, ) { if (!DEVICE) DEVICE = device; else if (DEVICE !== device) { DEVICE = device; GEOMETRY_CACHE.clear(); } const cacheKey = type + '|' + Object.entries(style) .sort(([a], [b]) => a.localeCompare(b)) .map(([k, v]) => `${k}:${v}`) .join(','); if (GEOMETRY_CACHE.has(cacheKey)) { return GEOMETRY_CACHE.get(cacheKey) as T; } const geometry = new Ctor(device, style); GEOMETRY_CACHE.set(cacheKey, geometry); return geometry; }