* 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>
187 lines
7.3 KiB
TypeScript
187 lines
7.3 KiB
TypeScript
/**
|
|
* LOD hook: renders nearby model-type entities as 3D glTF models,
|
|
* while distant ones fall back to the billboard/point pipeline.
|
|
*
|
|
* Strategy: All model-type entities ALSO go through useEntityRendering
|
|
* as billboards (fallback). This hook promotes nearby ones to 3D models
|
|
* and hides their billboard, capping GPU model count to ~MAX_MODELS.
|
|
*/
|
|
import { useEffect, useRef } from "react";
|
|
import {
|
|
Cartesian3,
|
|
Color,
|
|
HeadingPitchRoll,
|
|
Math as CesiumMath,
|
|
Model,
|
|
Transforms,
|
|
Matrix4,
|
|
} from "cesium";
|
|
import type { Viewer as CesiumViewer } from "cesium";
|
|
import type { GeoEntity, CesiumEntityOptions } from "@/core/plugins/PluginTypes";
|
|
import type { AnimatableItem } from "../EntityRenderer";
|
|
|
|
/** Maximum concurrent 3D models to keep GPU happy */
|
|
const MAX_MODELS = 30;
|
|
/** Distance threshold in meters — closer than this gets a 3D model */
|
|
const MODEL_DISTANCE_M = 80_000; // 80km
|
|
|
|
/** Scratch objects reused per LOD update to avoid heap allocations */
|
|
const lodScratchHPR = new HeadingPitchRoll();
|
|
const lodScratchMatrix = new Matrix4();
|
|
|
|
interface ActiveModel {
|
|
model: any;
|
|
entityId: string;
|
|
distance: number;
|
|
}
|
|
|
|
function buildModelMatrix(position: Cartesian3, heading: number, scale: number, headingOffset: number = 0): Matrix4 {
|
|
lodScratchHPR.heading = CesiumMath.toRadians(heading + headingOffset);
|
|
lodScratchHPR.pitch = 0;
|
|
lodScratchHPR.roll = 0;
|
|
Transforms.headingPitchRollToFixedFrame(
|
|
position,
|
|
lodScratchHPR,
|
|
undefined,
|
|
undefined,
|
|
lodScratchMatrix
|
|
);
|
|
Matrix4.multiplyByUniformScale(lodScratchMatrix, scale, lodScratchMatrix);
|
|
// Return a clone since the caller stores this in the model
|
|
return Matrix4.clone(lodScratchMatrix);
|
|
}
|
|
|
|
export function useModelRendering(
|
|
viewer: CesiumViewer | null,
|
|
isReady: boolean,
|
|
animatablesMapRef: React.MutableRefObject<Map<string, AnimatableItem>>
|
|
) {
|
|
const activeModelsRef = useRef<Map<string, ActiveModel>>(new Map());
|
|
const loadingRef = useRef<Set<string>>(new Set());
|
|
|
|
useEffect(() => {
|
|
if (!viewer || !isReady || viewer.isDestroyed()) return;
|
|
|
|
const activeModels = activeModelsRef.current;
|
|
const loading = loadingRef.current;
|
|
|
|
const updateLOD = () => {
|
|
if (viewer.isDestroyed()) return;
|
|
const camPos = viewer.camera.positionWC;
|
|
const animatables = animatablesMapRef.current;
|
|
if (!animatables) return;
|
|
|
|
// 1. Score all model-type entities by distance
|
|
const candidates: Array<{ id: string; item: AnimatableItem; distance: number }> = [];
|
|
|
|
for (const [id, item] of animatables.entries()) {
|
|
if (item.options.type !== "model" || !item.options.modelUrl) continue;
|
|
const dist = Cartesian3.distance(camPos, item.posRef);
|
|
if (dist < MODEL_DISTANCE_M) {
|
|
candidates.push({ id, item, distance: dist });
|
|
}
|
|
}
|
|
|
|
// Sort by distance (closest first) and cap at MAX_MODELS
|
|
candidates.sort((a, b) => a.distance - b.distance);
|
|
const promoted = candidates.slice(0, MAX_MODELS);
|
|
const promotedIds = new Set(promoted.map((c) => c.id));
|
|
|
|
// 2. Remove models that are no longer promoted
|
|
for (const [id, active] of activeModels.entries()) {
|
|
if (!promotedIds.has(id)) {
|
|
try { viewer.scene.primitives.remove(active.model); } catch { /**/ }
|
|
activeModels.delete(id);
|
|
// Show billboard again and clear promoted flag
|
|
const item = animatables.get(id);
|
|
if (item) {
|
|
item._modelPromoted = false;
|
|
item.promotedModel = undefined;
|
|
if (item.primitive) item.primitive.show = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Create/update promoted models
|
|
for (const { id, item, distance } of promoted) {
|
|
const pos = item.posRef;
|
|
const heading = item.entity.heading || 0;
|
|
const scale = item.options.modelScale || 1.0;
|
|
const offset = item.options.modelHeadingOffset || 0;
|
|
|
|
const existing = activeModels.get(id);
|
|
if (existing) {
|
|
// Update transform
|
|
const newMatrix = buildModelMatrix(pos, heading, scale, offset);
|
|
Matrix4.clone(newMatrix, existing.model.modelMatrix);
|
|
existing.distance = distance;
|
|
// Keep billboard hidden via flag
|
|
item._modelPromoted = true;
|
|
item.promotedModel = existing.model;
|
|
if (item.primitive) item.primitive.show = false;
|
|
continue;
|
|
}
|
|
|
|
// Skip if already loading
|
|
if (loading.has(id)) {
|
|
item._modelPromoted = true;
|
|
if (item.primitive) item.primitive.show = false;
|
|
continue;
|
|
}
|
|
|
|
// Load new model
|
|
loading.add(id);
|
|
item._modelPromoted = true;
|
|
if (item.primitive) item.primitive.show = false;
|
|
|
|
Model.fromGltfAsync({
|
|
url: item.options.modelUrl!,
|
|
modelMatrix: buildModelMatrix(pos, heading, scale, offset),
|
|
minimumPixelSize: item.options.modelMinPixelSize ?? 24,
|
|
maximumScale: 20000,
|
|
color: item.options.color ? Color.fromCssColorString(item.options.color) : undefined,
|
|
silhouetteColor: Color.CYAN,
|
|
silhouetteSize: 0,
|
|
id: { _wwvEntity: item.entity },
|
|
}).then((model) => {
|
|
loading.delete(id);
|
|
if (!viewer || viewer.isDestroyed()) return;
|
|
// Re-check still promoted
|
|
if (!animatables.has(id)) return;
|
|
viewer.scene.primitives.add(model);
|
|
activeModels.set(id, { model, entityId: id, distance });
|
|
|
|
const refreshedItem = animatables.get(id);
|
|
if (refreshedItem) refreshedItem.promotedModel = model;
|
|
}).catch(() => {
|
|
loading.delete(id);
|
|
// Show billboard again on failure
|
|
item._modelPromoted = false;
|
|
item.promotedModel = undefined;
|
|
if (item.primitive) item.primitive.show = true;
|
|
});
|
|
}
|
|
};
|
|
|
|
// Run LOD check at a lower frequency (every 10 frames)
|
|
let frameCount = 0;
|
|
const onPreUpdate = () => {
|
|
if (frameCount++ % 10 === 0) updateLOD();
|
|
};
|
|
|
|
viewer.scene.preUpdate.addEventListener(onPreUpdate);
|
|
|
|
return () => {
|
|
if (!viewer.isDestroyed()) {
|
|
viewer.scene.preUpdate.removeEventListener(onPreUpdate);
|
|
}
|
|
// Cleanup all active models
|
|
for (const [, active] of activeModels.entries()) {
|
|
try { viewer.scene.primitives.remove(active.model); } catch { /**/ }
|
|
}
|
|
activeModels.clear();
|
|
loading.clear();
|
|
};
|
|
}, [viewer, isReady, animatablesMapRef]);
|
|
}
|