mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(globe): camera.commanded, picker.pickGlobe, pose.aspectRatio and isDestroyed
Shell navigation needs to know when a new owner takes the camera (the arrival ground guard), where the globe surface is under a window position (orientation pivot, view-centred place search), the frustum aspect ratio (landmark framing) and whether the engine is gone. Each member has a contract suite check and is implemented by the Cesium engine, the Navara engine and the fake. On Cesium, commanded hooks the viewer camera's command methods and trackedEntityChanged only while it has listeners, so direct Cesium calls from layers still announce a new owner.
This commit is contained in:
1 parent
840e34a881
commit
a3e929870d
16 files changed
+459
-6
No files matched your search
@@ -97,7 +97,10 @@ export function createCameraModel({
|
||||
changed: createEngineEvent(),
|
||||
moveStart: createEngineEvent(),
|
||||
moveEnd: createEngineEvent(),
|
||||
commanded: createEngineEvent(),
|
||||
};
|
||||
// A camera command takes the camera from whoever held it; say so first.
|
||||
const announce = () => events.commanded.raiseEvent();
|
||||
const initial = port.read();
|
||||
let state = initial
|
||||
? copyPose(initial)
|
||||
@@ -295,19 +298,23 @@ export function createCameraModel({
|
||||
cartesian3.create(),
|
||||
),
|
||||
fovy: current.fovy,
|
||||
aspectRatio: current.width / current.height,
|
||||
};
|
||||
},
|
||||
setView(options) {
|
||||
if (!options?.destination)
|
||||
throw new TypeError('setView needs a destination');
|
||||
announce();
|
||||
place(viewFor(options.destination, options.orientation));
|
||||
},
|
||||
flyTo(options) {
|
||||
if (!options?.destination)
|
||||
throw new TypeError('flyTo needs a destination');
|
||||
announce();
|
||||
startFlight(options, viewFor(options.destination, options.orientation));
|
||||
},
|
||||
flyToSphere(sphere, options = {}) {
|
||||
announce();
|
||||
transform = [...matrix4.IDENTITY];
|
||||
const { width, height } = port.size();
|
||||
const range =
|
||||
@@ -328,6 +335,7 @@ export function createCameraModel({
|
||||
},
|
||||
flightActive: () => flight != null,
|
||||
lookAt(target, offset) {
|
||||
announce();
|
||||
camera.lookAtTransform(
|
||||
geo.transforms.eastNorthUpToFixedFrame(target, WGS84),
|
||||
offset,
|
||||
@@ -342,6 +350,7 @@ export function createCameraModel({
|
||||
transform = [...matrix4.IDENTITY];
|
||||
},
|
||||
cancelFlight() {
|
||||
announce();
|
||||
cancelCurrentFlight();
|
||||
},
|
||||
zoomIn(amount = DEFAULT_ZOOM_AMOUNT) {
|
||||
@@ -367,6 +376,7 @@ export function createCameraModel({
|
||||
changed: events.changed,
|
||||
moveStart: events.moveStart,
|
||||
moveEnd: events.moveEnd,
|
||||
commanded: events.commanded,
|
||||
get percentageChanged() {
|
||||
return percentageChanged;
|
||||
},
|
||||
|
||||
@@ -457,3 +457,33 @@ test('livePosition is one engine-owned object holding the current position; heig
|
||||
const pose = camera.pose();
|
||||
assert.notEqual(pose.position, live, 'pose shares the live object');
|
||||
});
|
||||
|
||||
test('commanded fires before each camera command and never for reads', () => {
|
||||
const { camera } = setup();
|
||||
let raised = 0;
|
||||
const remove = camera.commanded.addEventListener(() => raised++);
|
||||
assert.ok(isEngineEvent(camera.commanded));
|
||||
camera.pose();
|
||||
camera.height();
|
||||
camera.viewRectangle();
|
||||
assert.equal(raised, 0);
|
||||
const target = cartesian3.fromDegrees(10, 10, 0);
|
||||
camera.setView({ destination: cartesian3.fromDegrees(10, 10, 1e6) });
|
||||
camera.flyTo({ destination: cartesian3.fromDegrees(11, 10, 1e6), duration: 0 });
|
||||
camera.flyToSphere({ center: target, radius: 10 }, { duration: 0 });
|
||||
camera.lookAt(target, { heading: 0, pitch: -1, range: 500 });
|
||||
camera.cancelFlight();
|
||||
assert.equal(raised, 5);
|
||||
camera.release();
|
||||
camera.zoomIn(10);
|
||||
assert.equal(raised, 5, 'release and zoom are not new owners');
|
||||
remove();
|
||||
camera.setView({ destination: cartesian3.fromDegrees(10, 10, 1e6) });
|
||||
assert.equal(raised, 5);
|
||||
});
|
||||
|
||||
test('pose carries the view aspect ratio', () => {
|
||||
const { camera, port } = setup();
|
||||
const { width, height } = port.size();
|
||||
assert.equal(camera.pose().aspectRatio, width / height);
|
||||
});
|
||||
@@ -31,6 +31,31 @@ function readCamera(raw) {
|
||||
};
|
||||
}
|
||||
|
||||
// Nearest hit of a ray with the WGS84 ellipsoid raised by `height` metres
|
||||
// (each radius grown by it), or undefined.
|
||||
function rayRaisedEllipsoid({ origin, direction }, height) {
|
||||
const radii = geo.ellipsoid.WGS84.radii;
|
||||
const rx = radii.x + height;
|
||||
const ry = radii.y + height;
|
||||
const rz = radii.z + height;
|
||||
const o = [origin.x / rx, origin.y / ry, origin.z / rz];
|
||||
const d = [direction.x / rx, direction.y / ry, direction.z / rz];
|
||||
const a = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
|
||||
const b = 2 * (o[0] * d[0] + o[1] * d[1] + o[2] * d[2]);
|
||||
const c = o[0] * o[0] + o[1] * o[1] + o[2] * o[2] - 1;
|
||||
const discriminant = b * b - 4 * a * c;
|
||||
if (discriminant < 0) return undefined;
|
||||
const root = Math.sqrt(discriminant);
|
||||
const near = (-b - root) / (2 * a);
|
||||
const t = near > 0 ? near : (-b + root) / (2 * a);
|
||||
if (!(t > 0)) return undefined;
|
||||
return cartesian3.add(
|
||||
origin,
|
||||
cartesian3.multiplyByScalar(direction, t, cartesian3.create()),
|
||||
cartesian3.create(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} view An initialised Navara ThreeView.
|
||||
* @param {{
|
||||
@@ -115,6 +140,26 @@ export function createNavaraEngine(
|
||||
return hit ? cartesian3.create(hit.x, hit.y, hit.z) : undefined;
|
||||
},
|
||||
pickPositionSupported: true,
|
||||
// The globe under a window position: the pick ray's ellipsoid hit, moved
|
||||
// onto Navara's terrain (a few fixed-point steps; terrain varies slowly
|
||||
// over the distance the hit moves).
|
||||
pickGlobe(screen) {
|
||||
let hit = model.camera.pickEllipsoid(screen);
|
||||
if (!hit) return undefined;
|
||||
const ray = model.camera.pickRay(screen);
|
||||
for (let step = 0; step < 4; step++) {
|
||||
const height = surface.terrainHeight(
|
||||
geo.cartographic.fromCartesian(hit, geo.ellipsoid.WGS84),
|
||||
);
|
||||
if (!Number.isFinite(height)) break;
|
||||
const next = rayRaisedEllipsoid(ray, height);
|
||||
if (!next) break;
|
||||
const moved = cartesian3.distance(next, hit);
|
||||
hit = next;
|
||||
if (moved < 0.01) break;
|
||||
}
|
||||
return hit;
|
||||
},
|
||||
};
|
||||
|
||||
let destroyed = false;
|
||||
@@ -151,5 +196,6 @@ export function createNavaraEngine(
|
||||
credits.destroy();
|
||||
maps.destroy();
|
||||
},
|
||||
isDestroyed: () => destroyed,
|
||||
};
|
||||
}
|
||||
@@ -205,3 +205,24 @@ function near(actual, expected, tolerance, label) {
|
||||
`${label}: ${actual} is not within ${tolerance} of ${expected}`,
|
||||
);
|
||||
}
|
||||
|
||||
test('isDestroyed follows destroy', () => {
|
||||
const { engine } = setup();
|
||||
assert.equal(engine.isDestroyed(), false);
|
||||
engine.destroy();
|
||||
assert.equal(engine.isDestroyed(), true);
|
||||
});
|
||||
|
||||
test('pickGlobe meets the terrain along the pick ray, or the ellipsoid', () => {
|
||||
const { engine, view } = setup();
|
||||
const centre = cartesian2.create(640, 400);
|
||||
const bare = engine.picker.pickGlobe(centre);
|
||||
const ellipsoid = engine.camera.pickEllipsoid(centre);
|
||||
near(cartesian3.distance(bare, ellipsoid), 0, 1e-6, 'no terrain: ellipsoid');
|
||||
view.sampleTerrainHeight = () => 2000;
|
||||
const raised = engine.picker.pickGlobe(centre);
|
||||
const place = geo.cartographic.fromCartesian(raised);
|
||||
near(place.height, 2000, 0.5, 'terrain height (m)');
|
||||
near(math.toDegrees(place.longitude), 0, 1e-6, 'longitude');
|
||||
assert.equal(engine.picker.pickGlobe(cartesian2.create(640, -1e7)), undefined);
|
||||
});
|
||||
Reference in new issue
Block a user