mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Compute aircraft model anchors and drawn scale without Cesium
Model visual anchors now turn glTF axes into the placed frame with plain math, and a new modelDrawnScale reproduces the scale a model is drawn at under its pixel floor from the camera pose and viewport, so callers no longer need a Cesium model's computedScale. modelVisualAnchor.js comes off the Cesium allow-list.
This commit is contained in:
1 parent
3cce1aaa3e
commit
eed3b83858
5 files changed
+187
-108
No files matched your search
@@ -31,8 +31,9 @@ import {
|
|||||||
trailAnchorForModel,
|
trailAnchorForModel,
|
||||||
trailHeadStart,
|
trailHeadStart,
|
||||||
modelAnchorWorld,
|
modelAnchorWorld,
|
||||||
|
modelDrawnScale,
|
||||||
|
gltfToPlacedAxes,
|
||||||
} from './modelVisualAnchor.js';
|
} from './modelVisualAnchor.js';
|
||||||
import { createModelRecord } from './modelRecord.js';
|
|
||||||
|
|
||||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||||||
|
|
||||||
@@ -771,40 +772,21 @@ test('the trail head grows continuously across the envelope, never in one step',
|
|||||||
// property directly — the anchor lies in the model's longitudinal/vertical
|
// property directly — the anchor lies in the model's longitudinal/vertical
|
||||||
// plane, with NO lateral component.
|
// plane, with NO lateral component.
|
||||||
//
|
//
|
||||||
// The mock carries a NON-IDENTITY `sceneGraph.components.transform`, because
|
// The anchor is drawn at a scale other than 1 here, because a scale of 1
|
||||||
// the chain has a third link and a mock without one cannot tell whether the
|
// leaves both numbers the pin asserts at the raw table values, which is the
|
||||||
// code reads it: Cesium renders `modelMatrix x components.transform x
|
// answer a dropped scale also gives.
|
||||||
// axisCorrection`, and an asset with a baked root transform has to ride all
|
const TRAIL_DRAWN_SCALE = 2;
|
||||||
// three. A scale and an offset ALONG the model's own axes is the strongest
|
|
||||||
// choice the property survives — it changes both numbers the pin asserts
|
|
||||||
// (identity would leave them at the raw table values, which is the answer a
|
|
||||||
// dropped link also gives) while leaving lateral zero, which is the regression.
|
|
||||||
const TRAIL_ROOT_SCALE = 2;
|
|
||||||
const TRAIL_ROOT_OFFSET = Object.freeze({ lengthwise: 5, vertical: -1 });
|
|
||||||
test('the trail anchor rides the rendered longitudinal axis at every heading', () => {
|
test('the trail anchor rides the rendered longitudinal axis at every heading', () => {
|
||||||
const position = Cesium.Cartesian3.fromDegrees(-97.7, 30.2, 3000);
|
const position = Cesium.Cartesian3.fromDegrees(-97.7, 30.2, 3000);
|
||||||
const sub = (a, b) => Cesium.Cartesian3.subtract(a, b, new Cesium.Cartesian3());
|
const sub = (a, b) => Cesium.Cartesian3.subtract(a, b, new Cesium.Cartesian3());
|
||||||
// Cesium-model-local: x is lateral, y lengthwise, z vertical once the axis
|
|
||||||
// correction has mapped raw glTF [x, y, z] -> [z, x, y].
|
|
||||||
const rootTransform = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
|
|
||||||
new Cesium.Cartesian3(0, TRAIL_ROOT_OFFSET.lengthwise, TRAIL_ROOT_OFFSET.vertical),
|
|
||||||
Cesium.Quaternion.IDENTITY,
|
|
||||||
new Cesium.Cartesian3(TRAIL_ROOT_SCALE, TRAIL_ROOT_SCALE, TRAIL_ROOT_SCALE),
|
|
||||||
new Cesium.Matrix4(),
|
|
||||||
);
|
|
||||||
for (const headingDeg of [0, 45, 90, 180, 270, 315]) {
|
for (const headingDeg of [0, 45, 90, 180, 270, 315]) {
|
||||||
const hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(headingDeg), 0, 0);
|
const hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(headingDeg), 0, 0);
|
||||||
const modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
|
const modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
|
||||||
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4(),
|
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4(),
|
||||||
);
|
);
|
||||||
for (const [url, anchor] of Object.entries(MODEL_TRAIL_ANCHOR_NATIVE)) {
|
for (const [url, anchor] of Object.entries(MODEL_TRAIL_ANCHOR_NATIVE)) {
|
||||||
const record = createModelRecord({
|
|
||||||
modelMatrix,
|
|
||||||
computedScale: 1,
|
|
||||||
sceneGraph: { components: { transform: rootTransform } },
|
|
||||||
});
|
|
||||||
const origin = Cesium.Matrix4.getTranslation(modelMatrix, new Cesium.Cartesian3());
|
const origin = Cesium.Matrix4.getTranslation(modelMatrix, new Cesium.Cartesian3());
|
||||||
const world = modelAnchorWorld(record, anchor, new Cesium.Cartesian3());
|
const world = modelAnchorWorld(modelMatrix, anchor, TRAIL_DRAWN_SCALE, new Cesium.Cartesian3());
|
||||||
// Reference axes derived INDEPENDENTLY, from the ENU frame and the same
|
// Reference axes derived INDEPENDENTLY, from the ENU frame and the same
|
||||||
// heading the matrix was built with — deliberately NOT through
|
// heading the matrix was built with — deliberately NOT through
|
||||||
// modelAnchorWorld. Taking them through the function under test makes the
|
// modelAnchorWorld. Taking them through the function under test makes the
|
||||||
@@ -838,10 +820,10 @@ test('the trail anchor rides the rendered longitudinal axis at every heading', (
|
|||||||
// SIGNED, not magnitudes. An `abs()` on both sides passes an anchor that
|
// SIGNED, not magnitudes. An `abs()` on both sides passes an anchor that
|
||||||
// landed on the nose instead of the tail, or on the roof instead of the
|
// landed on the nose instead of the tail, or on the roof instead of the
|
||||||
// belly — which are the two directions the table exists to fix. The
|
// belly — which are the two directions the table exists to fix. The
|
||||||
// expected values carry the root transform, so a dropped `components`
|
// expected values carry the drawn scale, so a dropped scale fails here
|
||||||
// link fails here rather than quietly rendering the raw table value.
|
// rather than quietly rendering the raw table value.
|
||||||
const expectedAlong = anchor[0] * TRAIL_ROOT_SCALE + TRAIL_ROOT_OFFSET.lengthwise;
|
const expectedAlong = anchor[0] * TRAIL_DRAWN_SCALE;
|
||||||
const expectedUp = anchor[1] * TRAIL_ROOT_SCALE + TRAIL_ROOT_OFFSET.vertical;
|
const expectedUp = anchor[1] * TRAIL_DRAWN_SCALE;
|
||||||
assert.ok(Math.abs(along - expectedAlong) < 1e-4,
|
assert.ok(Math.abs(along - expectedAlong) < 1e-4,
|
||||||
`${where}: the longitudinal offset must land AFT along the heading ` +
|
`${where}: the longitudinal offset must land AFT along the heading ` +
|
||||||
`(${along.toFixed(4)} vs ${expectedAlong.toFixed(4)})`);
|
`(${along.toFixed(4)} vs ${expectedAlong.toFixed(4)})`);
|
||||||
@@ -861,13 +843,10 @@ test('a hovering rotorcraft anchors aft of its own hull, whatever its heading',
|
|||||||
const anchor = MODEL_TRAIL_ANCHOR_NATIVE['/models/bell206.glb'];
|
const anchor = MODEL_TRAIL_ANCHOR_NATIVE['/models/bell206.glb'];
|
||||||
for (const headingDeg of [0, 137.5, 271.9]) {
|
for (const headingDeg of [0, 137.5, 271.9]) {
|
||||||
const hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(headingDeg), 0, 0);
|
const hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(headingDeg), 0, 0);
|
||||||
const record = createModelRecord({
|
const matrix = Cesium.Transforms.headingPitchRollToFixedFrame(
|
||||||
modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
|
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4());
|
||||||
position, hpr, Cesium.Ellipsoid.WGS84, undefined, new Cesium.Matrix4()),
|
const origin = Cesium.Matrix4.getTranslation(matrix, new Cesium.Cartesian3());
|
||||||
computedScale: 1,
|
const world = modelAnchorWorld(matrix, anchor, 1, new Cesium.Cartesian3());
|
||||||
});
|
|
||||||
const origin = Cesium.Matrix4.getTranslation(record.matrix, new Cesium.Cartesian3());
|
|
||||||
const world = modelAnchorWorld(record, anchor, new Cesium.Cartesian3());
|
|
||||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(origin, Cesium.Ellipsoid.WGS84, new Cesium.Matrix4());
|
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(origin, Cesium.Ellipsoid.WGS84, new Cesium.Matrix4());
|
||||||
const ax = (i) => {
|
const ax = (i) => {
|
||||||
const c = Cesium.Matrix4.getColumn(enu, i, new Cesium.Cartesian4());
|
const c = Cesium.Matrix4.getColumn(enu, i, new Cesium.Cartesian4());
|
||||||
@@ -887,6 +866,56 @@ test('a hovering rotorcraft anchors aft of its own hull, whatever its heading',
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The glTF axis turn the anchors ride is the one the Cesium renderer draws
|
||||||
|
// models with (`Model.fromGltfAsync`'s defaults: Y up, Z forward), and no
|
||||||
|
// shipped asset carries a root transform of its own (CESIUM_RTC) that the
|
||||||
|
// renderer would add to the chain.
|
||||||
|
test('anchors turn glTF axes as the Cesium renderer draws models', () => {
|
||||||
|
const correction = Cesium.Matrix4.multiplyTransformation(
|
||||||
|
Cesium.Axis.Y_UP_TO_Z_UP, Cesium.Axis.Z_UP_TO_X_UP, new Cesium.Matrix4());
|
||||||
|
for (const v of [[1, 0, 0], [0, 1, 0], [0, 0, 1], [3.5, -2, 7]]) {
|
||||||
|
const drawn = Cesium.Matrix4.multiplyByPoint(
|
||||||
|
correction, new Cesium.Cartesian3(...v), new Cesium.Cartesian3());
|
||||||
|
const turned = gltfToPlacedAxes(v);
|
||||||
|
assert.deepEqual([drawn.x, drawn.y, drawn.z].map((n) => +n.toFixed(12)), turned.map((n) => +n.toFixed(12)));
|
||||||
|
}
|
||||||
|
for (const url of Object.keys(MODEL_TRAIL_ANCHOR_NATIVE)) {
|
||||||
|
const gltf = glbJson(path.join(ROOT, 'public', url));
|
||||||
|
assert.ok(!gltf.extensionsUsed?.includes('CESIUM_RTC'), `${url}: no root transform of its own`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// The drawn scale follows the draw contract's pixel rule, which the Cesium
|
||||||
|
// renderer implements as Model.computedScale: the bounding sphere's pixel
|
||||||
|
// size at its depth less its radius, raised to minPixelSize.
|
||||||
|
test('a model is drawn at its scale until the pixel floor raises it', () => {
|
||||||
|
const pose = { position: { x: 0, y: 0, z: 0 }, direction: { x: 0, y: 0, z: 1 }, fovy: Math.PI / 3 };
|
||||||
|
const base = { scale: 1, minPixelSize: 24, radiusM: 30, pose, viewportHeightPx: 1000 };
|
||||||
|
// Near: 60 m across fills hundreds of pixels.
|
||||||
|
assert.equal(modelDrawnScale({ ...base, center: { x: 0, y: 0, z: 500 } }), 1);
|
||||||
|
// Far: 100 km away, 24 px of a 30 m-radius sphere.
|
||||||
|
const depth = 100_000;
|
||||||
|
const mpp = (2 * (depth - 30) * Math.tan(Math.PI / 6)) / 1000;
|
||||||
|
const raised = modelDrawnScale({ ...base, center: { x: 0, y: 0, z: depth } });
|
||||||
|
assert.ok(Math.abs(raised - (24 * mpp) / 60) < 1e-9, `raised to ${raised}`);
|
||||||
|
// Depth along the view direction, not straight-line distance: off to one
|
||||||
|
// side at the same depth, it is raised the same.
|
||||||
|
assert.equal(modelDrawnScale({ ...base, center: { x: 40_000, y: 0, z: depth } }), raised);
|
||||||
|
// The sphere's size is capped at the drawing buffer's larger side in its own
|
||||||
|
// pixels: at a pixel ratio of 2 a 500 CSS px viewport is 1000 px across, so
|
||||||
|
// a sphere 600 CSS px wide is not raised.
|
||||||
|
const close = { ...base, minPixelSize: 600, viewportHeightPx: 500, center: { x: 0, y: 0, z: 60 } };
|
||||||
|
assert.equal(modelDrawnScale({ ...close, pixelRatio: 2 }), 1);
|
||||||
|
assert.ok(modelDrawnScale(close) < 1, 'at a pixel ratio of 1 the cap is 500 px');
|
||||||
|
// maxScale caps it, and the sphere it is measured by is at the capped scale.
|
||||||
|
assert.equal(modelDrawnScale({ ...base, maxScale: 2, center: { x: 0, y: 0, z: depth } }), 2);
|
||||||
|
const fromCapped = modelDrawnScale({ ...base, scale: 5, maxScale: 3, minPixelSize: 400, center: { x: 0, y: 0, z: depth } });
|
||||||
|
assert.equal(fromCapped, 3, 'raised past maxScale, then capped');
|
||||||
|
const big = modelDrawnScale({ ...base, scale: 5, maxScale: 1e9, minPixelSize: 4000, viewportHeightPx: 1000, viewportWidthPx: 5000, center: { x: 0, y: 0, z: depth } });
|
||||||
|
assert.ok(Math.abs(big - (4000 * ((2 * (depth - 150) * Math.tan(Math.PI / 6)) / 1000)) / 60) < 1e-9, `raised from the sphere at scale 5: ${big}`);
|
||||||
|
assert.equal(modelDrawnScale({ ...base, pose: null, center: { x: 0, y: 0, z: depth } }), 1);
|
||||||
|
});
|
||||||
|
|
||||||
test('legacy aircraft GLBs keep scale and orientation baked with no node transforms', () => {
|
test('legacy aircraft GLBs keep scale and orientation baked with no node transforms', () => {
|
||||||
for (const url of Object.keys(MODEL_VISUAL_CENTER_NATIVE)) {
|
for (const url of Object.keys(MODEL_VISUAL_CENTER_NATIVE)) {
|
||||||
const file = path.join(ROOT, 'public', url);
|
const file = path.join(ROOT, 'public', url);
|
||||||
|
|||||||
+114
-69
@@ -1,12 +1,11 @@
|
|||||||
import * as Cesium from 'cesium';
|
|
||||||
/**
|
/**
|
||||||
* Model-space visual centres measured from the shipped GLBs' scene-space AABBs.
|
* Model-space visual centres measured from the shipped GLBs' scene-space AABBs.
|
||||||
*
|
*
|
||||||
* glTF stores these assets Y-up. Cesium's model frame is Z-up, so a glTF
|
* Unlike the trail anchors below, these are stored in the placed model's
|
||||||
* centre `[x, y, z]` becomes `[x, -z, y]` before the layer's ENU modelMatrix
|
* own frame (the glTF axes already turned, see `gltfToPlacedAxes`), so the
|
||||||
* is applied. Values remain in native model units; callers supply the Model's
|
* layer's ENU placement applies to them directly. Values remain in native model units; callers supply the scale
|
||||||
* effective rendered `computedScale` because Cesium does not bake it into
|
* the model is drawn at (`modelDrawnScale`), which the placement does not
|
||||||
* modelMatrix and may raise it above `scale` to satisfy minimumPixelSize.
|
* carry and which the pixel floor may raise above `scale`.
|
||||||
*/
|
*/
|
||||||
const CENTERED_MODEL = Object.freeze([0, 0, 0]);
|
const CENTERED_MODEL = Object.freeze([0, 0, 0]);
|
||||||
|
|
||||||
@@ -19,7 +18,7 @@ export const MODEL_VISUAL_CENTER_NATIVE = Object.freeze({
|
|||||||
* Aft-belly trail attachment points, measured on each approved model's actual
|
* Aft-belly trail attachment points, measured on each approved model's actual
|
||||||
* MESH SURFACE. Every bundled asset is baked to one convention (see
|
* MESH SURFACE. Every bundled asset is baked to one convention (see
|
||||||
* aircraftClass.js): origin at the bbox centre, X = length with the NOSE at
|
* aircraftClass.js): origin at the bbox centre, X = length with the NOSE at
|
||||||
* local −X, glTF Y = up (Cesium-local Z here).
|
* local −X, glTF Y = up (the placed frame's Z here).
|
||||||
*
|
*
|
||||||
* Each value is the point of the hull's CENTRELINE PROFILE (the y = 0 slice of
|
* Each value is the point of the hull's CENTRELINE PROFILE (the y = 0 slice of
|
||||||
* the mesh) closest to that model's aft-belly AABB corner. Three things follow
|
* the mesh) closest to that model's aft-belly AABB corner. Three things follow
|
||||||
@@ -53,7 +52,7 @@ export const MODEL_VISUAL_CENTER_NATIVE = Object.freeze({
|
|||||||
* the nearest surface point in the round — so the attachment cannot drift
|
* the nearest surface point in the round — so the attachment cannot drift
|
||||||
* screen-left or screen-right as the aircraft turns. (The unconstrained nearest
|
* screen-left or screen-right as the aircraft turns. (The unconstrained nearest
|
||||||
* point would put jet.glb's anchor 1.29 m off-axis.) Values are native model
|
* point would put jet.glb's anchor 1.29 m off-axis.) Values are native model
|
||||||
* units; callers scale them by Cesium's effective `computedScale`.
|
* units; callers scale them by the model's drawn scale (`modelDrawnScale`).
|
||||||
*
|
*
|
||||||
* Locked against the shipped GLBs by modelScale.test.mjs, which re-reads the
|
* Locked against the shipped GLBs by modelScale.test.mjs, which re-reads the
|
||||||
* POSITION buffers and re-derives every property above — re-measure, never
|
* POSITION buffers and re-derives every property above — re-measure, never
|
||||||
@@ -70,79 +69,125 @@ export const MODEL_TRAIL_ANCHOR_NATIVE = Object.freeze({
|
|||||||
'/models/atr72.glb': Object.freeze([12.9785, -1.2935, 0]),
|
'/models/atr72.glb': Object.freeze([12.9785, -1.2935, 0]),
|
||||||
});
|
});
|
||||||
|
|
||||||
/** @constant Cesium's OWN axis correction for a glTF loaded with the
|
/**
|
||||||
* `Model.fromGltfAsync` defaults (upAxis Y, forwardAxis Z), assembled from its
|
* How a model's raw glTF axes land in its placed frame, as the renderer
|
||||||
* exported constants rather than restated as numbers here. `ModelSceneGraph`
|
* draws a glTF model (`ModelItem`: Y-up, turned to Z-up, nose along the
|
||||||
* renders with `modelMatrix x components.transform x axisCorrection`, and the
|
* frame's own axis): raw glTF `[x, y, z]` becomes `[z, x, y]` in the frame
|
||||||
* trail anchor has to ride the same chain or it lands on a different axis than
|
* `orientation` places. modelScale.test.mjs pins it against the Cesium
|
||||||
* the aircraft it is supposed to be attached to. */
|
* renderer's own axis correction.
|
||||||
const AXIS_CORRECTION = Cesium.Matrix4.multiplyTransformation(
|
* @param {readonly number[]} v Raw glTF `[x, y, z]`.
|
||||||
Cesium.Axis.Y_UP_TO_Z_UP,
|
* @returns {number[]} `[x, y, z]` in the placed frame.
|
||||||
Cesium.Axis.Z_UP_TO_X_UP,
|
*/
|
||||||
new Cesium.Matrix4(),
|
export const gltfToPlacedAxes = (v) => [v[2] || 0, v[0] || 0, v[1] || 0];
|
||||||
);
|
|
||||||
const _anchorScratch = new Cesium.Cartesian3();
|
|
||||||
const _chainScratch = new Cesium.Matrix4();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* World position of a model-local anchor, through the SAME transform chain
|
* World position of a model-local anchor, through the same chain the model
|
||||||
* Cesium renders the model with.
|
* is drawn with: its placement `matrix` (column-major, the frame the layer
|
||||||
|
* posed it in) after the glTF axis turn (`gltfToPlacedAxes`), at the scale
|
||||||
|
* it is drawn at.
|
||||||
*
|
*
|
||||||
* This exists because doing it by hand got it wrong. The anchors used to be
|
* This exists because doing it by hand got it wrong. The anchors used to be
|
||||||
* stored after a single glTF Y-up -> Z-up step (`[x, y, z]` -> `[x, -z, y]`) and
|
* stored after a single glTF Y-up -> Z-up step (`[x, y, z]` -> `[x, -z, y]`) and
|
||||||
* multiplied straight by `modelMatrix`. That is only half of Cesium's
|
* multiplied straight by the placement. That is only half of the turn: the
|
||||||
* correction: with the `fromGltfAsync` defaults it also applies Z_UP_TO_X_UP,
|
* full one maps raw glTF `[x, y, z]` -> `[z, x, y]`. The aircraft's
|
||||||
* and the full correction maps raw glTF `[x, y, z]` -> `[z, x, y]`. The
|
* longitudinal axis is raw glTF X, so the old conversion put the aft offset on
|
||||||
* aircraft's longitudinal axis is raw glTF X, so the old conversion put the
|
* the RENDERED model's lateral axis — a 90-degree disagreement. Because the
|
||||||
* aft offset on the RENDERED model's lateral axis — a 90-degree disagreement.
|
* placement carries the heading, both frames rotated together and the offset
|
||||||
* Because `modelMatrix` carries the heading, both frames rotated together and
|
* came out sideways in world space, flipping which side it appeared on as the
|
||||||
* the offset came out sideways in world space, flipping which side it appeared
|
* course changed. Measured on the live rig at N/E/S/W plus a helicopter: the
|
||||||
* on as the course changed. Measured on the live rig at N/E/S/W plus a
|
* old path put 81.47 m of a civil airliner's anchor entirely on cross-track
|
||||||
* helicopter: the old path put 81.47 m of a civil airliner's anchor entirely on
|
* with 0 aft, at every heading; this path puts 81.47 m aft with 0 cross.
|
||||||
* cross-track with 0 aft, at every heading; this path puts 81.47 m aft with 0
|
|
||||||
* cross.
|
|
||||||
*
|
*
|
||||||
* The anchors are therefore stored in RAW glTF coordinates and transformed
|
* The anchors are therefore stored in RAW glTF coordinates and turned here,
|
||||||
* here, so exactly one transform — Cesium's own — stands between the table and
|
* so exactly one convention stands between the table and the world.
|
||||||
* the world, and there is no second convention to drift.
|
|
||||||
*
|
*
|
||||||
* @param {import('./modelRecord.js').ModelRecord} record The layer's record of
|
* @param {ArrayLike<number>|null|undefined} matrix Where the model is placed.
|
||||||
* the model: where it placed it, and the Cesium Model (computedScale,
|
|
||||||
* sceneGraph).
|
|
||||||
* @param {readonly number[]} nativeAnchor Raw glTF `[x, y, z]` native-unit offset.
|
* @param {readonly number[]} nativeAnchor Raw glTF `[x, y, z]` native-unit offset.
|
||||||
|
* @param {number} scale The scale the model is drawn at (`modelDrawnScale`).
|
||||||
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
||||||
* @returns {{x:number,y:number,z:number}|null} World position of the anchor.
|
* @returns {{x:number,y:number,z:number}|null} World position of the anchor.
|
||||||
*/
|
*/
|
||||||
export function modelAnchorWorld(record, nativeAnchor, result) {
|
export function modelAnchorWorld(matrix, nativeAnchor, scale, result) {
|
||||||
const modelMatrix = record?.matrix;
|
if (!matrix || !nativeAnchor || !result) return null;
|
||||||
const model = record?.model;
|
return modelVisualAnchor(
|
||||||
if (!modelMatrix || !nativeAnchor || !result) return null;
|
matrix,
|
||||||
const scale = Number.isFinite(model.computedScale) ? model.computedScale : 1;
|
gltfToPlacedAxes(nativeAnchor),
|
||||||
_anchorScratch.x = (nativeAnchor[0] || 0) * scale;
|
scale,
|
||||||
_anchorScratch.y = (nativeAnchor[1] || 0) * scale;
|
result,
|
||||||
_anchorScratch.z = (nativeAnchor[2] || 0) * scale;
|
|
||||||
// The model's own root transform, when it exposes one, then Cesium's axis
|
|
||||||
// correction. Read from the instance rather than assumed, so an asset with a
|
|
||||||
// baked root transform is carried too.
|
|
||||||
const components = model?.sceneGraph?.components?.transform;
|
|
||||||
Cesium.Matrix4.multiplyTransformation(
|
|
||||||
modelMatrix,
|
|
||||||
components || Cesium.Matrix4.IDENTITY,
|
|
||||||
_chainScratch,
|
|
||||||
);
|
);
|
||||||
Cesium.Matrix4.multiplyTransformation(
|
}
|
||||||
_chainScratch,
|
|
||||||
AXIS_CORRECTION,
|
/**
|
||||||
_chainScratch,
|
* The scale a model is drawn at, by the draw contract's pixel rule: `scale`
|
||||||
|
* (capped by `maxScale`), raised so its bounding sphere spans `minPixelSize`
|
||||||
|
* pixels at the sphere's distance from the eye (its depth along the view
|
||||||
|
* direction, less its radius), and capped by `maxScale` again.
|
||||||
|
* @param {object} options
|
||||||
|
* @param {number} [options.scale] The item's `scale` (default 1). An item
|
||||||
|
* that also has `scaleByDistance` or `maxPixelSize` is drawn at a scale
|
||||||
|
* this does not model; aircraft models use neither.
|
||||||
|
* @param {number} [options.minPixelSize]
|
||||||
|
* @param {number} [options.maxScale]
|
||||||
|
* @param {number} options.radiusM The model's bounding radius at scale 1.
|
||||||
|
* @param {{x:number,y:number,z:number}} options.center Where it is placed.
|
||||||
|
* @param {{position: object, direction: object, fovy: number}|null} options.pose
|
||||||
|
* The camera pose (`camera.pose()`).
|
||||||
|
* @param {number} options.viewportHeightPx Drawn viewport height, CSS pixels.
|
||||||
|
* @param {number} [options.viewportWidthPx] Drawn viewport width, CSS pixels.
|
||||||
|
* @param {number} [options.pixelRatio] Drawing-buffer pixels per CSS pixel
|
||||||
|
* (default 1): the sphere's drawn size is capped at the drawing buffer's
|
||||||
|
* larger side, in its own pixels.
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
export function modelDrawnScale({
|
||||||
|
scale = 1,
|
||||||
|
minPixelSize = 0,
|
||||||
|
maxScale,
|
||||||
|
radiusM,
|
||||||
|
center,
|
||||||
|
pose,
|
||||||
|
viewportHeightPx,
|
||||||
|
viewportWidthPx = viewportHeightPx,
|
||||||
|
pixelRatio = 1,
|
||||||
|
}) {
|
||||||
|
const capped = (s) => (Number.isFinite(maxScale) ? Math.min(maxScale, s) : s);
|
||||||
|
if (
|
||||||
|
!(minPixelSize > 0) ||
|
||||||
|
!(radiusM > 0) ||
|
||||||
|
!center ||
|
||||||
|
!pose?.position ||
|
||||||
|
!pose.direction ||
|
||||||
|
!(pose.fovy > 0) ||
|
||||||
|
!(viewportHeightPx > 0)
|
||||||
|
)
|
||||||
|
return capped(scale);
|
||||||
|
const sphereRadius = radiusM * capped(scale);
|
||||||
|
const depth = Math.abs(
|
||||||
|
(center.x - pose.position.x) * pose.direction.x +
|
||||||
|
(center.y - pose.position.y) * pose.direction.y +
|
||||||
|
(center.z - pose.position.z) * pose.direction.z,
|
||||||
);
|
);
|
||||||
return Cesium.Matrix4.multiplyByPoint(_chainScratch, _anchorScratch, result);
|
const distance = Math.max(0, depth - sphereRadius);
|
||||||
|
const metresPerPixel =
|
||||||
|
(2 * distance * Math.tan(pose.fovy / 2)) / viewportHeightPx;
|
||||||
|
if (!(metresPerPixel > 0)) return capped(scale);
|
||||||
|
const diameterPx = Math.min(
|
||||||
|
(2 * sphereRadius) / metresPerPixel,
|
||||||
|
Math.max(viewportWidthPx || 0, viewportHeightPx) *
|
||||||
|
(pixelRatio > 0 ? pixelRatio : 1),
|
||||||
|
);
|
||||||
|
const drawn =
|
||||||
|
diameterPx < minPixelSize
|
||||||
|
? (minPixelSize * metresPerPixel) / (2 * radiusM)
|
||||||
|
: scale;
|
||||||
|
return capped(drawn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the model-local visual centre for an approved bundled GLB.
|
* Return the model-local visual centre for an approved bundled GLB.
|
||||||
* Assets absent from the table were audited as origin-centred.
|
* Assets absent from the table were audited as origin-centred.
|
||||||
* @param {string} url Bundled model URL.
|
* @param {string} url Bundled model URL.
|
||||||
* @returns {readonly number[]} Cesium-local `[x, y, z]` native-unit offset.
|
* @returns {readonly number[]} Placed-frame `[x, y, z]` native-unit offset.
|
||||||
*/
|
*/
|
||||||
export function visualCenterForModel(url) {
|
export function visualCenterForModel(url) {
|
||||||
return MODEL_VISUAL_CENTER_NATIVE[url] || CENTERED_MODEL;
|
return MODEL_VISUAL_CENTER_NATIVE[url] || CENTERED_MODEL;
|
||||||
@@ -153,7 +198,7 @@ export function visualCenterForModel(url) {
|
|||||||
* Unknown future assets safely fall back to their visual centre until their
|
* Unknown future assets safely fall back to their visual centre until their
|
||||||
* geometry is measured and added to the table.
|
* geometry is measured and added to the table.
|
||||||
* @param {string} url Bundled model URL.
|
* @param {string} url Bundled model URL.
|
||||||
* @returns {readonly number[]} Cesium-local `[x, y, z]` native-unit offset.
|
* @returns {readonly number[]} Placed-frame `[x, y, z]` native-unit offset.
|
||||||
*/
|
*/
|
||||||
export function trailAnchorForModel(url) {
|
export function trailAnchorForModel(url) {
|
||||||
return MODEL_TRAIL_ANCHOR_NATIVE[url] || visualCenterForModel(url);
|
return MODEL_TRAIL_ANCHOR_NATIVE[url] || visualCenterForModel(url);
|
||||||
@@ -210,15 +255,15 @@ export function trailAnchorForModel(url) {
|
|||||||
* and an in/out/in flap costs a few centimetres each way instead of ten metres.
|
* and an in/out/in flap costs a few centimetres each way instead of ten metres.
|
||||||
*
|
*
|
||||||
* Scale-free rather than tuned: both ends of that shell come from the model —
|
* Scale-free rather than tuned: both ends of that shell come from the model —
|
||||||
* `radiusM` carries Cesium's `computedScale`, so it tracks whatever
|
* `radiusM` carries the drawn scale, so it tracks whatever
|
||||||
* `minimumPixelSize` inflated the model to, and the station comes from the
|
* `minPixelSize` inflated the model to, and the station comes from the
|
||||||
* anchor being drawn to.
|
* anchor being drawn to.
|
||||||
*
|
*
|
||||||
* @param {{x:number,y:number,z:number}} start Segment start (last trail point).
|
* @param {{x:number,y:number,z:number}} start Segment start (last trail point).
|
||||||
* @param {{x:number,y:number,z:number}|null} anchor Segment end, world space —
|
* @param {{x:number,y:number,z:number}|null} anchor Segment end, world space —
|
||||||
* the aft-belly attachment point actually being drawn to.
|
* the aft-belly attachment point actually being drawn to.
|
||||||
* @param {{x:number,y:number,z:number}|null} center Model origin, world space.
|
* @param {{x:number,y:number,z:number}|null} center Model origin, world space.
|
||||||
* @param {number} radiusM Rendered bounding radius (nativeRadiusM x computedScale).
|
* @param {number} radiusM Rendered bounding radius (nativeRadiusM x drawn scale).
|
||||||
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
||||||
* @returns {{x:number,y:number,z:number}|null} The point to draw FROM, or null
|
* @returns {{x:number,y:number,z:number}|null} The point to draw FROM, or null
|
||||||
* when the segment should not be drawn. May be `start` itself.
|
* when the segment should not be drawn. May be `start` itself.
|
||||||
@@ -258,9 +303,9 @@ export function trailHeadStart(start, anchor, center, radiusM, result) {
|
|||||||
* allocating. `modelMatrix` continues to own the model origin (including
|
* allocating. `modelMatrix` continues to own the model origin (including
|
||||||
* ground snapping); bracket/readout and trail consumers supply their own
|
* ground snapping); bracket/readout and trail consumers supply their own
|
||||||
* model-local anchor.
|
* model-local anchor.
|
||||||
* @param {ArrayLike<number>} modelMatrix Cesium Matrix4-compatible value.
|
* @param {ArrayLike<number>} modelMatrix The placement, column-major 4x4.
|
||||||
* @param {readonly number[]} nativeCenter Cesium-local native-unit offset.
|
* @param {readonly number[]} nativeCenter Placed-frame native-unit offset.
|
||||||
* @param {number} scale Cesium Model.computedScale applied outside modelMatrix.
|
* @param {number} scale The drawn scale, applied outside the placement.
|
||||||
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
* @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result.
|
||||||
* @returns {{x:number,y:number,z:number}|null} World visual centre.
|
* @returns {{x:number,y:number,z:number}|null} World visual centre.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -545,8 +545,11 @@ export function createMotion({
|
|||||||
// half-correction put this offset on the lateral axis — see
|
// half-correction put this offset on the lateral axis — see
|
||||||
// modelAnchorWorld).
|
// modelAnchorWorld).
|
||||||
return modelAnchorWorld(
|
return modelAnchorWorld(
|
||||||
flightState._trackedModel,
|
flightState._trackedModel.matrix,
|
||||||
spec.trailAnchorNative,
|
spec.trailAnchorNative,
|
||||||
|
Number.isFinite(flightState._trackedModel.model.computedScale)
|
||||||
|
? flightState._trackedModel.model.computedScale
|
||||||
|
: spec.scale,
|
||||||
flightState._trackedTrailPos,
|
flightState._trackedTrailPos,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -407,8 +407,11 @@ export function createMotion({
|
|||||||
// half-correction put this offset on the lateral axis — see
|
// half-correction put this offset on the lateral axis — see
|
||||||
// modelAnchorWorld).
|
// modelAnchorWorld).
|
||||||
return modelAnchorWorld(
|
return modelAnchorWorld(
|
||||||
flightState._trackedModel,
|
flightState._trackedModel.matrix,
|
||||||
spec.trailAnchorNative,
|
spec.trailAnchorNative,
|
||||||
|
Number.isFinite(flightState._trackedModel.model.computedScale)
|
||||||
|
? flightState._trackedModel.model.computedScale
|
||||||
|
: spec.scale,
|
||||||
flightState._trackedTrailPos,
|
flightState._trackedTrailPos,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
"src/data/iconSetPrimitive.js",
|
"src/data/iconSetPrimitive.js",
|
||||||
"src/data/localGeojsonCore.js",
|
"src/data/localGeojsonCore.js",
|
||||||
"src/data/modelRecord.js",
|
"src/data/modelRecord.js",
|
||||||
"src/data/modelVisualAnchor.js",
|
|
||||||
"src/data/motionModel.js",
|
"src/data/motionModel.js",
|
||||||
"src/input/dispatcher.js",
|
"src/input/dispatcher.js",
|
||||||
"src/input/sensorPick.js",
|
"src/input/sensorPick.js",
|
||||||
|
|||||||
Reference in new issue
Block a user