import { attachCctvVideo } from './videoPlayback.js';
import * as geo from '../../geo/index.js';
import {
CCTV_PROJECTION_OVERLAY_SOURCE_ID,
CCTV_PROJECTION_OVERLAY_SOURCE_OPTIONS,
PROJECTION_CANVAS_WIDTH,
PROJECTION_CANVAS_HEIGHT,
PROJECTION_TEXTURE_SWAP_MS,
} from './policy.js';
export function createProjection({
state: layerState,
services,
parts,
source,
}) {
const {
holdContinuousRender,
releaseContinuousRender,
governorRequestRender,
} = services.render;
let loopRevision = 0;
/**
* Build the protected label associated with one active monitor plane.
* @param {{cameraId: string, name: string, position: Cesium.Cartesian3|Function}} input
* @returns {Object} Shared-host presentation entry.
*/
function createCctvProjectionOverlayEntry({ cameraId, name, position }) {
return {
id: String(cameraId),
position,
variant: 'selected',
selected: true,
protected: true,
paintLane: 'selected',
collisionGroup: 'ambient-card',
priority: Number.MAX_SAFE_INTEGER - 1,
title: String(name || cameraId || 'CAMERA'),
details: [],
accent: '#6be8ff',
interactive: false,
gapPx: 6,
verticalOnly: true,
placement: 'above',
edgeFade: 'keyhole',
horizonCull: true,
terrainOcclusion: false,
};
}
/**
* Re-derives the monitor plane entity's placement (position, orientation,
* dimensions) + label from the record's current frustum geometry, so the plane
* always caps the wireframe exactly (corner rays terminate on its corners).
* No-op when the record has no plane runtime (idle neighbors have no plane).
* @param {Object} record - Camera record.
*/
function updatePlanePlacement(record) {
const runtime = record?.projection;
if (!runtime?.planeEntity) return;
const geometry =
record.frustumGeometry ||
parts.geometry.computeFrustumGeometry(
record.camera,
parts.ground.groundAltFor(record),
record.probeClampRangeM,
);
const positions =
record.frustumPositions || parts.geometry.frustumCartesians(geometry);
parts.renderer.updatePlane(
runtime,
geometry,
positions,
parts.model.planeOrientationFor(record.camera, positions.capCenter),
);
if (runtime.labelPosition) {
geo.cartesian3.clone(positions.label, runtime.labelPosition);
}
}
/**
* Clear the active monitor-plane label source and ownership marker.
*/
function clearProjectionOverlay() {
layerState._cctvOverlayHost.clearSource(CCTV_PROJECTION_OVERLAY_SOURCE_ID);
layerState._cctvOverlayHost.setVisible(
CCTV_PROJECTION_OVERLAY_SOURCE_ID,
false,
);
layerState._projectionOverlayOwnerId = null;
}
/**
* Shows/hides the monitor plane and its associated shared-host label.
* @param {Object} runtime - Projection runtime.
* @param {boolean} visible
*/
function setPlaneVisible(runtime, visible) {
if (!runtime) return;
runtime.planeVisible = !!visible;
if (runtime.planeEntity)
parts.renderer?.setPlaneVisible(runtime, !!visible);
if (visible && runtime.overlayEntry && runtime.cameraId) {
if (layerState._projectionOverlayOwnerId !== runtime.cameraId) {
layerState._cctvOverlayHost.setEntries(
CCTV_PROJECTION_OVERLAY_SOURCE_ID,
[runtime.overlayEntry],
CCTV_PROJECTION_OVERLAY_SOURCE_OPTIONS,
);
layerState._cctvOverlayHost.setVisible(
CCTV_PROJECTION_OVERLAY_SOURCE_ID,
true,
);
layerState._projectionOverlayOwnerId = runtime.cameraId;
}
} else if (layerState._projectionOverlayOwnerId === runtime.cameraId) {
clearProjectionOverlay();
}
}
/** Create the native monitor plane plus its cached host-label presentation. */
function createProjectionPlane(record, runtime, geometry, positions) {
runtime.labelPosition ||= geo.cartesian3.create();
geo.cartesian3.clone(positions.label, runtime.labelPosition);
runtime.cameraId = String(record.camera.id);
runtime.overlayEntry = createCctvProjectionOverlayEntry({
cameraId: runtime.cameraId,
name: record.camera.name,
position: () => runtime.labelPosition,
});
runtime.planeVisible = false;
runtime.planeEntity = parts.renderer.createPlane(
record,
runtime,
geometry,
positions,
parts.model.planeOrientationFor(record.camera, positions.capCenter),
);
return runtime.planeEntity;
}
/**
* Creates the projection runtime for a camera record: an offscreen canvas,
* the monitor plane plus associated host label, and either an
*
or