mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Read aircraft model placement from the app's mirror
Flights, military and local ADS-B now write their models' show and world matrix through setModel in src/data/modelMirror.js, and read them back through modelState: the fleet and tracked handoffs, the tracked visual and trail anchors, detection anchors and modelAnchorWorld. A matrix is copied into the app's record and into the model's own matrix, so the shared scratch can be passed directly and models still never share one matrix object. What Cesium measures (ready, computedScale) is still read from the model, and colour stays live as on billboards, because cyber sonar writes it too. Test seams mirror the models they install, as they do billboards.
This commit is contained in:
1 parent
6639c4bb80
commit
05904edfc9
15 files changed
+273
-88
No files matched your search
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @module modelMirror
|
||||
* @description The app's copy of what a layer pushed to its 3D models, so the layer
|
||||
* reads its own placement back from the app instead of from the renderer's objects.
|
||||
*
|
||||
* A layer writes the mirrored fields with {@link setModel} and reads them with
|
||||
* {@link modelState}. What the renderer measures (`ready`, `computedScale`) is
|
||||
* still read from the model. Colour stays out for the same reason as on
|
||||
* billboards: the cyber-sonar theme writes it too and reads the live value.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { createEntityStore } from './entityRecords.js';
|
||||
|
||||
const mirrors = createEntityStore();
|
||||
|
||||
/**
|
||||
* The app's record for `model`, made on first write with Cesium's defaults for a
|
||||
* model loaded without `show` or `modelMatrix` options (shown, at the identity).
|
||||
*/
|
||||
function recordFor(model) {
|
||||
const record = mirrors.write(model);
|
||||
if (!record.modelMatrix) {
|
||||
record.show = true;
|
||||
record.modelMatrix = Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start mirroring a model the app did not write itself (test seams), from its
|
||||
* current fields.
|
||||
* @param {Object} model
|
||||
* @returns {Object} The model.
|
||||
*/
|
||||
export function mirrorModel(model) {
|
||||
if (!model || typeof model !== 'object') return model;
|
||||
const record = recordFor(model);
|
||||
record.show = model.show ?? true;
|
||||
if (model.modelMatrix)
|
||||
Cesium.Matrix4.clone(model.modelMatrix, record.modelMatrix);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write one mirrored field to the model and to the app's copy. A matrix is copied
|
||||
* into both, so a shared scratch can be passed and reused straight after.
|
||||
* @param {Object} model
|
||||
* @param {'show'|'modelMatrix'} field
|
||||
* @param {*} value
|
||||
*/
|
||||
export function setModel(model, field, value) {
|
||||
if (field !== 'show' && field !== 'modelMatrix')
|
||||
throw new TypeError(`The model mirror does not hold ${field}`);
|
||||
const record = recordFor(model);
|
||||
if (field === 'modelMatrix') {
|
||||
Cesium.Matrix4.clone(value, record.modelMatrix);
|
||||
// Cesium clones the model's own matrix each frame, so write into it in place.
|
||||
if (model.modelMatrix) Cesium.Matrix4.clone(value, model.modelMatrix);
|
||||
else model.modelMatrix = Cesium.Matrix4.clone(value);
|
||||
} else {
|
||||
record[field] = value;
|
||||
model[field] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The app's copy of a model's mirrored fields, or null when nothing was written to
|
||||
* it yet (or there is no model).
|
||||
* @param {Object|null|undefined} model
|
||||
* @returns {{ show: boolean, modelMatrix: Cesium.Matrix4 }|null}
|
||||
*/
|
||||
export function modelState(model) {
|
||||
return mirrors.read(model);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { mirrorModel, modelState, setModel } from './modelMirror.js';
|
||||
|
||||
function fakeModel() {
|
||||
return { show: true, modelMatrix: Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY) };
|
||||
}
|
||||
|
||||
test('a model has no mirror until the app writes to it', () => {
|
||||
assert.equal(modelState(fakeModel()), null);
|
||||
assert.equal(modelState(null), null);
|
||||
});
|
||||
|
||||
test('the first write starts from Cesium defaults for the other fields', () => {
|
||||
const model = fakeModel();
|
||||
setModel(model, 'show', false);
|
||||
assert.equal(model.show, false);
|
||||
assert.equal(modelState(model).show, false);
|
||||
assert.ok(Cesium.Matrix4.equals(modelState(model).modelMatrix, Cesium.Matrix4.IDENTITY));
|
||||
});
|
||||
|
||||
test('a matrix is copied into both the model and the mirror, never shared', () => {
|
||||
const model = fakeModel();
|
||||
const own = model.modelMatrix;
|
||||
const scratch = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(1, 2, 3));
|
||||
setModel(model, 'modelMatrix', scratch);
|
||||
assert.equal(model.modelMatrix, own, "written into the model's own matrix");
|
||||
assert.ok(Cesium.Matrix4.equals(model.modelMatrix, scratch));
|
||||
assert.notEqual(modelState(model).modelMatrix, scratch);
|
||||
Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(9, 9, 9), scratch);
|
||||
assert.deepEqual(
|
||||
Cesium.Matrix4.getTranslation(modelState(model).modelMatrix, new Cesium.Cartesian3()),
|
||||
new Cesium.Cartesian3(1, 2, 3),
|
||||
'reusing the scratch moves neither copy',
|
||||
);
|
||||
assert.ok(!Cesium.Matrix4.equals(model.modelMatrix, scratch));
|
||||
});
|
||||
|
||||
test('the mirror answers from the app, not from the model', () => {
|
||||
const model = fakeModel();
|
||||
setModel(model, 'show', true);
|
||||
model.show = false; // a write that bypassed the mirror
|
||||
assert.equal(modelState(model).show, true);
|
||||
});
|
||||
|
||||
test("a model without its own matrix yet gets a copy, not the caller's", () => {
|
||||
const model = { show: true };
|
||||
const matrix = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(1, 2, 3));
|
||||
setModel(model, 'modelMatrix', matrix);
|
||||
assert.notEqual(model.modelMatrix, matrix);
|
||||
assert.ok(Cesium.Matrix4.equals(model.modelMatrix, matrix));
|
||||
});
|
||||
|
||||
test('a model the app did not write can be mirrored from its fields', () => {
|
||||
const matrix = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(4, 5, 6));
|
||||
const model = mirrorModel({ show: false, modelMatrix: matrix });
|
||||
assert.equal(modelState(model).show, false);
|
||||
assert.ok(Cesium.Matrix4.equals(modelState(model).modelMatrix, matrix));
|
||||
assert.notEqual(modelState(model).modelMatrix, matrix);
|
||||
assert.equal(modelState(mirrorModel({})).show, true, 'Cesium shows by default');
|
||||
assert.equal(mirrorModel(null), null);
|
||||
});
|
||||
|
||||
test('fields outside the mirror are refused', () => {
|
||||
assert.throws(() => setModel({}, 'scale', 2), TypeError);
|
||||
});
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
trailHeadStart,
|
||||
modelAnchorWorld,
|
||||
} from './modelVisualAnchor.js';
|
||||
import { mirrorModel } from './modelMirror.js';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
|
||||
@@ -797,11 +798,11 @@ test('the trail anchor rides the rendered longitudinal axis at every heading', (
|
||||
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4(),
|
||||
);
|
||||
for (const [url, anchor] of Object.entries(MODEL_TRAIL_ANCHOR_NATIVE)) {
|
||||
const model = {
|
||||
const model = mirrorModel({
|
||||
modelMatrix,
|
||||
computedScale: 1,
|
||||
sceneGraph: { components: { transform: rootTransform } },
|
||||
};
|
||||
});
|
||||
const origin = Cesium.Matrix4.getTranslation(modelMatrix, new Cesium.Cartesian3());
|
||||
const world = modelAnchorWorld(model, anchor, new Cesium.Cartesian3());
|
||||
// Reference axes derived INDEPENDENTLY, from the ENU frame and the same
|
||||
@@ -860,11 +861,11 @@ test('a hovering rotorcraft anchors aft of its own hull, whatever its heading',
|
||||
const anchor = MODEL_TRAIL_ANCHOR_NATIVE['/models/bell206.glb'];
|
||||
for (const headingDeg of [0, 137.5, 271.9]) {
|
||||
const hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(headingDeg), 0, 0);
|
||||
const model = {
|
||||
const model = mirrorModel({
|
||||
modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
|
||||
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4()),
|
||||
computedScale: 1,
|
||||
};
|
||||
});
|
||||
const origin = Cesium.Matrix4.getTranslation(model.modelMatrix, new Cesium.Cartesian3());
|
||||
const world = modelAnchorWorld(model, anchor, new Cesium.Cartesian3());
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(origin, Cesium.Ellipsoid.WGS84, new Cesium.Matrix4());
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { modelState } from './modelMirror.js';
|
||||
/**
|
||||
* Model-space visual centres measured from the shipped GLBs' scene-space AABBs.
|
||||
*
|
||||
@@ -112,7 +113,8 @@ const _chainScratch = new Cesium.Matrix4();
|
||||
* @returns {{x:number,y:number,z:number}|null} World position of the anchor.
|
||||
*/
|
||||
export function modelAnchorWorld(model, nativeAnchor, result) {
|
||||
if (!model || !model.modelMatrix || !nativeAnchor || !result) return null;
|
||||
const modelMatrix = modelState(model)?.modelMatrix;
|
||||
if (!modelMatrix || !nativeAnchor || !result) return null;
|
||||
const scale = Number.isFinite(model.computedScale) ? model.computedScale : 1;
|
||||
_anchorScratch.x = (nativeAnchor[0] || 0) * scale;
|
||||
_anchorScratch.y = (nativeAnchor[1] || 0) * scale;
|
||||
@@ -122,7 +124,7 @@ export function modelAnchorWorld(model, nativeAnchor, result) {
|
||||
// baked root transform is carried too.
|
||||
const components = model?.sceneGraph?.components?.transform;
|
||||
Cesium.Matrix4.multiplyTransformation(
|
||||
model.modelMatrix,
|
||||
modelMatrix,
|
||||
components || Cesium.Matrix4.IDENTITY,
|
||||
_chainScratch,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user