* chore(merge): resolve conflicts, fix mockWs error, and apply refactor standards * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix(ci): Stabilize github actions and resolve test failures - fix(tests): update WsClient mock to use standard function for Vitest constructor compatibility - fix(db): correct PrismaClient import path to use generated output directory - fix(e2e): add predev database boot step to playwright CI workflow - chore: sync pnpm-lock.yaml after merge conflict resolution * refactor(plugins): decouple GeoJSON plugin and implement data bus lifecycle - feat(sdk): [Minor] Added dynamicPluginCreate, dynamicPluginRemove, and pluginUnregistered to DataBusEvents. - feat(core): [Minor] Implemented unregisterPlugin in PluginManager.ts for automated IndexedDB caching cleanup and polling teardown. - feat(core): [Minor] Added removeLayer to layersSlice.ts to support safe removal of plugin states. - refactor(plugins): [Patch] Refactored useGeoJsonImport.ts and ImportedLayerList.tsx to decouple from direct manager imports, utilizing DataBus for lifecycle events. - refactor(core): [Patch] Added pluginUnregistered listener to DataBusSubscriber.tsx to handle state and cache cleanup synchronously via event. * fix(types): resolve PluginManager type errors * refactor(core): decouple PluginManager and fix rendering invariants - refactor(core): Replace Zustand mutations in PluginManager with DataBus events (Patch) - refactor(sdk): Add layerLoadingChanged, pluginError, pluginUnregistered to DataBusEvents (Patch) - refactor(ui): Subscribe to new DataBus events in DataBusSubscriber (Patch) - fix(plugins): Remove size from BaseAviationPlugin to fix model rendering clipping (Patch) - fix(plugins): Migrate BaseIncidentPlugin from size to iconScale to fix billboard clipping (Patch) * chore: resolve lockfile conflict --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
234 lines
9.4 KiB
TypeScript
234 lines
9.4 KiB
TypeScript
import {
|
|
Cartesian3, Cartographic, Color, Ellipsoid,
|
|
PointPrimitiveCollection, BillboardCollection, LabelCollection, PolylineCollection,
|
|
VerticalOrigin, DistanceDisplayCondition, NearFarScalar, HeightReference,
|
|
} from "cesium";
|
|
import type { Viewer as CesiumViewer } from "cesium";
|
|
import type { GeoEntity, CesiumEntityOptions } from "@/core/plugins/PluginTypes";
|
|
import { globalChunkedProcessor } from "./ChunkedProcessor";
|
|
import {
|
|
scratchPosition, getEntityColor, getCachedColor,
|
|
markAnimatablesDirty, getStableAnimatables,
|
|
} from "./renderCaches";
|
|
import {
|
|
updateExistingItem, createNewItem, cleanupRemovedEntities, getDefaultDotIcon
|
|
} from "./primitiveOps";
|
|
import { rebuildStacks, calculateGridSizeDegrees } from "./StackManager";
|
|
|
|
// Re-export for existing consumers
|
|
export {
|
|
getEntityColor, getCachedColor, markAnimatablesDirty, getStableAnimatables
|
|
} from "./renderCaches";
|
|
|
|
export interface AnimatableItem {
|
|
primitive: any;
|
|
labelPrimitive?: any;
|
|
entity: GeoEntity;
|
|
posRef: Cartesian3;
|
|
options: CesiumEntityOptions;
|
|
basePosition?: Cartesian3;
|
|
velocityVector?: Cartesian3;
|
|
baseColor?: Color;
|
|
baseOutlineColor?: Color;
|
|
lastHighlightState?: 'normal' | 'hovered' | 'selected';
|
|
/** Set by LOD hook — when true, billboard is hidden because a 3D model replaced it */
|
|
_modelPromoted?: boolean;
|
|
/** Set by AnimationLoop - true if hidden by mathematical horizon culling */
|
|
_occluded?: boolean;
|
|
polylinePrimitive?: any; // Reference to the trail polyline in the collection
|
|
trailPositions?: Cartesian3[]; // Cached position array to avoid GC pressure
|
|
_lastHistoryTs?: number; // Timestamp of the latest history point processed
|
|
promotedModel?: any; // Reference to 3D model when promoted
|
|
}
|
|
|
|
/** Global render kickstarter for deeply nested async operations. */
|
|
export let globalRequestRender: () => void = () => { };
|
|
|
|
export interface PrimitiveCollections {
|
|
points: PointPrimitiveCollection;
|
|
billboards: BillboardCollection;
|
|
labels: LabelCollection;
|
|
polylines: PolylineCollection;
|
|
}
|
|
|
|
const collectionsByViewer = new WeakMap<CesiumViewer, PrimitiveCollections>();
|
|
|
|
/** Initialize primitive collections on the viewer. */
|
|
export function initPrimitiveCollections(viewer: CesiumViewer): void {
|
|
if (!viewer?.scene?.primitives) {
|
|
console.warn("[EntityRenderer] initPrimitiveCollections called before viewer.scene was ready — skipping.");
|
|
return;
|
|
}
|
|
const points = new PointPrimitiveCollection();
|
|
points.blendOption = 2; // TRANSLUCENT
|
|
viewer.scene.primitives.add(points);
|
|
|
|
const billboards = new BillboardCollection({ scene: viewer.scene });
|
|
billboards.blendOption = 2; // TRANSLUCENT
|
|
viewer.scene.primitives.add(billboards);
|
|
|
|
const labels = new LabelCollection({ scene: viewer.scene });
|
|
viewer.scene.primitives.add(labels);
|
|
|
|
const polylines = new PolylineCollection();
|
|
viewer.scene.primitives.add(polylines);
|
|
|
|
collectionsByViewer.set(viewer, {
|
|
points, billboards, labels, polylines
|
|
});
|
|
|
|
globalRequestRender = () => {
|
|
if (viewer && !viewer.isDestroyed()) {
|
|
viewer.scene.requestRender();
|
|
}
|
|
};
|
|
}
|
|
|
|
/** Get the primitive collections for a viewer. Returns an empty shape if init hasn't run. */
|
|
export function getCollections(viewer: CesiumViewer): Partial<PrimitiveCollections> {
|
|
return collectionsByViewer.get(viewer) ?? {};
|
|
}
|
|
|
|
/** Creates a label primitive for an item (lazy evaluation). */
|
|
export function createLabel(item: AnimatableItem, labels: LabelCollection): void {
|
|
if (!item.options.labelText || item.labelPrimitive) return;
|
|
const clickId = { _wwvEntity: item.entity };
|
|
item.labelPrimitive = labels.add({
|
|
position: item.posRef,
|
|
text: item.options.labelText,
|
|
font: item.options.labelFont || "12px Inter, sans-serif",
|
|
fillColor: Color.WHITE,
|
|
outlineColor: Color.BLACK,
|
|
outlineWidth: 2,
|
|
verticalOrigin: VerticalOrigin.BOTTOM,
|
|
pixelOffset: { x: 0, y: -12 } as any,
|
|
show: true,
|
|
id: clickId,
|
|
// WARNING: Do NOT use heightReference: HeightReference.CLAMP_TO_GROUND here.
|
|
// It causes severe lag/performance drops with thousands of dynamic entities.
|
|
disableDepthTestDistance: item.options.disableDepthTestDistance ?? Number.POSITIVE_INFINITY,
|
|
translucencyByDistance: new NearFarScalar(1e3, 1.0, 5e5, 0.0),
|
|
distanceDisplayCondition: item.options.distanceDisplayCondition
|
|
? new DistanceDisplayCondition(item.options.distanceDisplayCondition.near, item.options.distanceDisplayCondition.far)
|
|
: undefined,
|
|
});
|
|
}
|
|
|
|
/** Removes a label primitive for an item. */
|
|
export function removeLabel(item: AnimatableItem, labels: LabelCollection): void {
|
|
if (!item.labelPrimitive) return;
|
|
labels.remove(item.labelPrimitive);
|
|
item.labelPrimitive = undefined;
|
|
}
|
|
|
|
/** Core rendering logic for a single entity. */
|
|
function renderSingleEntity(
|
|
{ entity, options }: { entity: GeoEntity; options: CesiumEntityOptions },
|
|
existingMap: Map<string, AnimatableItem>,
|
|
points: PointPrimitiveCollection,
|
|
billboards: BillboardCollection,
|
|
labels: LabelCollection,
|
|
currentIds: Set<string>
|
|
) {
|
|
currentIds.add(entity.id);
|
|
Cartesian3.fromDegrees(entity.longitude, entity.latitude, entity.altitude || 0, Ellipsoid.WGS84, scratchPosition);
|
|
|
|
const baseColor = getEntityColor(options);
|
|
|
|
// Auto-upgrade missing icons to SVG Billboards so pixelOffsets (spiderifier) will work.
|
|
// Only clone when the auto-SVG branch actually mutates — otherwise pass `options` through.
|
|
let effectiveOptions: CesiumEntityOptions = options;
|
|
if (!options.iconUrl && options.type !== "model") {
|
|
const baseOutlineColor = getCachedColor(options.outlineColor) || Color.BLACK;
|
|
const outWidth = options.outlineWidth || 1;
|
|
const pSize = options.size || (typeof window !== "undefined" && window.matchMedia("(pointer: coarse)").matches ? 12 : 8);
|
|
effectiveOptions = {
|
|
...options,
|
|
iconUrl: getDefaultDotIcon(baseColor, baseOutlineColor, outWidth, pSize),
|
|
iconScale: 1.0,
|
|
};
|
|
(effectiveOptions as any)._isAutoSVG = true;
|
|
}
|
|
|
|
const clickId = { _wwvEntity: entity };
|
|
let item = existingMap.get(entity.id);
|
|
|
|
if (item && item.options.type !== effectiveOptions.type) {
|
|
if (item.options.iconUrl) billboards.remove(item.primitive); else points.remove(item.primitive);
|
|
if (item.labelPrimitive) labels.remove(item.labelPrimitive);
|
|
existingMap.delete(entity.id);
|
|
item = undefined;
|
|
}
|
|
|
|
if (item) {
|
|
updateExistingItem(item, entity, effectiveOptions, baseColor);
|
|
} else {
|
|
createNewItem(entity, effectiveOptions, baseColor, clickId, existingMap, points, billboards);
|
|
}
|
|
}
|
|
|
|
/** Chunked rendering for large datasets (10k+). */
|
|
export async function renderEntitiesChunked(
|
|
viewer: CesiumViewer,
|
|
visibleEntities: Array<{ entity: GeoEntity; options: CesiumEntityOptions }>,
|
|
existingMap: Map<string, AnimatableItem>,
|
|
onChunkProcessed?: () => void
|
|
): Promise<AnimatableItem[]> {
|
|
const {
|
|
points, billboards, labels, polylines
|
|
} = getCollections(viewer);
|
|
if (!points || !billboards || !labels) return getStableAnimatables(existingMap);
|
|
const currentIds = new Set<string>();
|
|
const completed = await globalChunkedProcessor.processChunked(visibleEntities, 500, (chunk) => {
|
|
if (viewer.isDestroyed()) return;
|
|
for (let i = 0; i < chunk.length; i++) renderSingleEntity(chunk[i], existingMap, points, billboards, labels, currentIds);
|
|
if (onChunkProcessed) onChunkProcessed();
|
|
});
|
|
|
|
if (!completed || viewer.isDestroyed()) {
|
|
return getStableAnimatables(existingMap);
|
|
}
|
|
|
|
cleanupRemovedEntities(existingMap, currentIds, points, billboards, labels, polylines);
|
|
markAnimatablesDirty();
|
|
|
|
let altitude = 1000000;
|
|
if (viewer.camera && viewer.camera.positionCartographic) {
|
|
altitude = viewer.camera.positionCartographic.height;
|
|
} else if (viewer.camera && viewer.camera.positionWC) {
|
|
const carto = Cartographic.fromCartesian(viewer.camera.positionWC);
|
|
if (carto) altitude = carto.height;
|
|
}
|
|
|
|
rebuildStacks(existingMap, calculateGridSizeDegrees(altitude), true);
|
|
return getStableAnimatables(existingMap);
|
|
}
|
|
|
|
/** Synchronous render for smaller datasets or instant updates. */
|
|
export function renderEntities(
|
|
viewer: CesiumViewer,
|
|
visibleEntities: Array<{ entity: GeoEntity; options: CesiumEntityOptions }>,
|
|
existingMap: Map<string, AnimatableItem>
|
|
): AnimatableItem[] {
|
|
globalChunkedProcessor.cancel();
|
|
const {
|
|
points, billboards, labels, polylines
|
|
} = getCollections(viewer);
|
|
if (!points || !billboards || !labels) return getStableAnimatables(existingMap);
|
|
const currentIds = new Set<string>();
|
|
for (const item of visibleEntities) renderSingleEntity(item, existingMap, points, billboards, labels, currentIds);
|
|
cleanupRemovedEntities(existingMap, currentIds, points, billboards, labels, polylines);
|
|
markAnimatablesDirty();
|
|
|
|
let altitude = 1000000;
|
|
if (viewer.camera && viewer.camera.positionCartographic) {
|
|
altitude = viewer.camera.positionCartographic.height;
|
|
} else if (viewer.camera && viewer.camera.positionWC) {
|
|
const carto = Cartographic.fromCartesian(viewer.camera.positionWC);
|
|
if (carto) altitude = carto.height;
|
|
}
|
|
|
|
rebuildStacks(existingMap, calculateGridSizeDegrees(altitude), true);
|
|
return getStableAnimatables(existingMap);
|
|
}
|