mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Run the Navara paths without the Cesium SDK, and load it on demand for the features that still build on it
Every Cesium read that a Navara page made was surveyed with the strict acceptance (GEV_QA_CESIUM_TRAP=record). Math on those paths now uses src/geo: screen annotations, weather and wind center readings, cyclone picks, scene data pack polygons and images (new navaraGeometry.js), the awareness subject, the orbit offset of camera verbs, voice frame_overhead, the agent world packet and Directions colours and ground picks. The celestial ring's Sun and Moon come from a bit-identical port of Cesium's Simon1994 model, JulianDate and TEME rotation (src/geo/simon1994.js, julianDate.js, transforms.js); a Cesium globe still applies its ICRF rotation once loaded. Traffic, Street Level, the Nepal event and locator and geo import build on Cesium's math or entity model on either globe; their enable (or the import) now loads the SDK first through src/app/loadCesium.js, synchronously when it is already there. A production page guards the global so a missed read is reported and starts the load. No module reads Cesium while it loads (scripts/check-cesium-load-time.mjs, run by npm test). Cesium.js is still loaded by index.html here.
This commit is contained in:
1 parent
f87da8523e
commit
b8305de14b
58 files changed
+2130
-325
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
BufferAttribute,
|
||||
BufferGeometry,
|
||||
@@ -11,6 +10,8 @@ import {
|
||||
SRGBColorSpace,
|
||||
Texture,
|
||||
} from 'three';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { polygonShape, rectangleShape } from './navaraGeometry.js';
|
||||
import {
|
||||
attachSceneDepth,
|
||||
createSceneDepthTest,
|
||||
@@ -18,7 +19,7 @@ import {
|
||||
const KEY = 'gevScenePack',
|
||||
registered = new WeakSet();
|
||||
const cyan = [0, 1, 1, 1],
|
||||
xyz = (p) => Cesium.Cartesian3.fromDegrees(...p);
|
||||
xyz = ([lon, lat, height]) => geo.cartesian3.fromDegrees(lon, lat, height);
|
||||
export function createNavaraPackRenderer(engine, { view, MeshDesc }) {
|
||||
if (!registered.has(view)) {
|
||||
view.registerMesh(
|
||||
@@ -153,20 +154,9 @@ export function createNavaraPackRenderer(engine, { view, MeshDesc }) {
|
||||
line.setPositions(f.coordinates.map(xyz));
|
||||
} else {
|
||||
batch ??= meshGroup(handle);
|
||||
const polygon = new Cesium.PolygonGeometry({
|
||||
polygonHierarchy: new Cesium.PolygonHierarchy(
|
||||
f.coordinates[0].map(xyz),
|
||||
f.coordinates
|
||||
.slice(1)
|
||||
.map((r) => new Cesium.PolygonHierarchy(r.map(xyz))),
|
||||
),
|
||||
perPositionHeight: true,
|
||||
vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
|
||||
arcType: Cesium.ArcType.GEODESIC,
|
||||
});
|
||||
addMesh(
|
||||
batch,
|
||||
Cesium.PolygonGeometry.createGeometry(polygon),
|
||||
polygonShape(f.coordinates),
|
||||
{ color: new Color(0x00ffff), opacity: 0.35 },
|
||||
id,
|
||||
null,
|
||||
@@ -189,7 +179,7 @@ export function createNavaraPackRenderer(engine, { view, MeshDesc }) {
|
||||
const hits = raycaster.intersectObjects(batch.group.children);
|
||||
const earth = engine.picker.pickGlobe(at);
|
||||
const depth = earth
|
||||
? Cesium.Cartesian3.distance(ray.origin, earth)
|
||||
? geo.cartesian3.distance(ray.origin, earth)
|
||||
: Infinity;
|
||||
return hits
|
||||
.filter((h) => h.distance <= depth + 1)
|
||||
@@ -210,12 +200,9 @@ export function createNavaraPackRenderer(engine, { view, MeshDesc }) {
|
||||
texture.colorSpace = SRGBColorSpace;
|
||||
texture.needsUpdate = true;
|
||||
batch.owned.push(texture);
|
||||
const shape = Cesium.RectangleGeometry.createGeometry(
|
||||
new Cesium.RectangleGeometry({
|
||||
rectangle: Cesium.Rectangle.fromDegrees(...pack.placement.bounds),
|
||||
height: pack.placement.height,
|
||||
vertexFormat: Cesium.VertexFormat.POSITION_AND_ST,
|
||||
}),
|
||||
const shape = rectangleShape(
|
||||
pack.placement.bounds,
|
||||
pack.placement.height,
|
||||
);
|
||||
addMesh(batch, shape, { color: 0xffffff, opacity: 1 }, null, texture);
|
||||
engine.render.request('scene-pack');
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
// Pack polygons and images as Navara mesh geometry, without the Cesium SDK (a
|
||||
// page that starts on Navara never loads it). A shape is what Cesium's
|
||||
// Geometry gives the renderer: Earth-fixed positions, triangle indices, an
|
||||
// optional texture coordinate per vertex and a centre to draw relative to.
|
||||
import { ShapeUtils, Vector2 } from 'three';
|
||||
import * as geo from '../../geo/index.js';
|
||||
|
||||
// Cesium's default granularity: no edge or cell spans more than one degree.
|
||||
const GRANULARITY_DEGREES = 1;
|
||||
|
||||
function shape(positions, indices, st = null) {
|
||||
const center = geo.cartesian3.create();
|
||||
for (const p of positions) geo.cartesian3.add(center, p, center);
|
||||
geo.cartesian3.divideByScalar(center, positions.length || 1, center);
|
||||
const values = new Float64Array(positions.length * 3);
|
||||
positions.forEach((p, i) => values.set([p.x, p.y, p.z], i * 3));
|
||||
return {
|
||||
attributes: {
|
||||
position: { values },
|
||||
...(st ? { st: { values: st } } : {}),
|
||||
},
|
||||
indices: new Uint32Array(indices),
|
||||
boundingSphere: { center },
|
||||
};
|
||||
}
|
||||
|
||||
/** A [lon, lat, height?] ring with long edges split along great circles. */
|
||||
function densify(ring) {
|
||||
const out = [];
|
||||
ring.forEach((a, i) => {
|
||||
const b = ring[(i + 1) % ring.length];
|
||||
const steps = Math.max(
|
||||
1,
|
||||
Math.ceil(
|
||||
Math.max(Math.abs(b[0] - a[0]), Math.abs(b[1] - a[1])) /
|
||||
GRANULARITY_DEGREES,
|
||||
),
|
||||
);
|
||||
for (let k = 0; k < steps; k++) {
|
||||
const t = k / steps;
|
||||
out.push([
|
||||
a[0] + (b[0] - a[0]) * t,
|
||||
a[1] + (b[1] - a[1]) * t,
|
||||
(a[2] ?? 0) + ((b[2] ?? 0) - (a[2] ?? 0)) * t,
|
||||
]);
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* A filled polygon: `rings` are GeoJSON-style [lon, lat, height?] rings, the
|
||||
* first the outline and the rest holes, each vertex at its own height (as
|
||||
* Cesium's perPositionHeight). Triangulated in the plane tangent to the
|
||||
* Earth at the outline's centre; null when nothing is left to fill.
|
||||
*/
|
||||
export function polygonShape(rings) {
|
||||
const loops = rings
|
||||
.map((ring) => {
|
||||
const open =
|
||||
ring.length > 1 &&
|
||||
ring[0][0] === ring.at(-1)[0] &&
|
||||
ring[0][1] === ring.at(-1)[1]
|
||||
? ring.slice(0, -1)
|
||||
: ring;
|
||||
return densify(open);
|
||||
})
|
||||
.filter((ring) => ring.length >= 3);
|
||||
if (!loops.length) return null;
|
||||
const [outline, ...holes] = loops;
|
||||
const points = loops.flat();
|
||||
const world = points.map(([lon, lat, h]) =>
|
||||
geo.cartesian3.fromDegrees(lon, lat, h),
|
||||
);
|
||||
const middle = geo.cartesian3.create();
|
||||
for (const p of world.slice(0, outline.length))
|
||||
geo.cartesian3.add(middle, p, middle);
|
||||
geo.cartesian3.divideByScalar(middle, outline.length, middle);
|
||||
const frame = geo.transforms.eastNorthUpToFixedFrame(middle);
|
||||
const east = geo.cartesian3.create(frame[0], frame[1], frame[2]);
|
||||
const north = geo.cartesian3.create(frame[4], frame[5], frame[6]);
|
||||
const offset = geo.cartesian3.create();
|
||||
const plane = world.map((p) => {
|
||||
geo.cartesian3.subtract(p, middle, offset);
|
||||
return new Vector2(
|
||||
geo.cartesian3.dot(offset, east),
|
||||
geo.cartesian3.dot(offset, north),
|
||||
);
|
||||
});
|
||||
let start = outline.length;
|
||||
const holePoints = holes.map((hole) => {
|
||||
const ring = plane.slice(start, start + hole.length);
|
||||
start += hole.length;
|
||||
return ring;
|
||||
});
|
||||
const triangles = ShapeUtils.triangulateShape(
|
||||
plane.slice(0, outline.length),
|
||||
holePoints,
|
||||
);
|
||||
if (!triangles.length) return null;
|
||||
return shape(world, triangles.flat());
|
||||
}
|
||||
|
||||
/**
|
||||
* An image's rectangle, [west, south, east, north] in degrees at `height`:
|
||||
* a grid of cells no larger than the granularity, texture coordinates from
|
||||
* (0, 0) at the south-west corner to (1, 1) at the north-east one.
|
||||
*/
|
||||
export function rectangleShape([west, south, east, north], height = 0) {
|
||||
const span = east >= west ? east - west : east + 360 - west;
|
||||
const columns = Math.max(1, Math.ceil(span / GRANULARITY_DEGREES));
|
||||
const rows = Math.max(
|
||||
1,
|
||||
Math.ceil(Math.abs(north - south) / GRANULARITY_DEGREES),
|
||||
);
|
||||
const positions = [];
|
||||
const st = new Float32Array((rows + 1) * (columns + 1) * 2);
|
||||
for (let row = 0; row <= rows; row++)
|
||||
for (let column = 0; column <= columns; column++) {
|
||||
const s = column / columns,
|
||||
t = row / rows;
|
||||
st.set([s, t], positions.length * 2);
|
||||
positions.push(
|
||||
geo.cartesian3.fromDegrees(
|
||||
west + span * s,
|
||||
south + (north - south) * t,
|
||||
height,
|
||||
),
|
||||
);
|
||||
}
|
||||
const indices = [];
|
||||
for (let row = 0; row < rows; row++)
|
||||
for (let column = 0; column < columns; column++) {
|
||||
const a = row * (columns + 1) + column,
|
||||
b = a + 1,
|
||||
c = a + columns + 1,
|
||||
d = c + 1;
|
||||
indices.push(a, b, d, a, d, c);
|
||||
}
|
||||
return shape(positions, indices, st);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { polygonShape, rectangleShape } from './navaraGeometry.js';
|
||||
|
||||
const vertex = (shape, index) => {
|
||||
const v = shape.attributes.position.values;
|
||||
return geo.cartographic.fromCartesian(
|
||||
geo.cartesian3.create(v[index * 3], v[index * 3 + 1], v[index * 3 + 2]),
|
||||
);
|
||||
};
|
||||
const area = (shape) => {
|
||||
// Sum of triangle areas in square metres.
|
||||
const v = shape.attributes.position.values,
|
||||
i = shape.indices;
|
||||
const p = (k) => geo.cartesian3.create(v[k * 3], v[k * 3 + 1], v[k * 3 + 2]);
|
||||
let sum = 0;
|
||||
for (let t = 0; t < i.length; t += 3) {
|
||||
const a = p(i[t]),
|
||||
ab = geo.cartesian3.subtract(p(i[t + 1]), a, geo.cartesian3.create()),
|
||||
ac = geo.cartesian3.subtract(p(i[t + 2]), a, geo.cartesian3.create());
|
||||
sum +=
|
||||
geo.cartesian3.magnitude(
|
||||
geo.cartesian3.cross(ab, ac, geo.cartesian3.create()),
|
||||
) / 2;
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
|
||||
test('a pack polygon fills its outline less its holes, each vertex at its own height', () => {
|
||||
const square = [
|
||||
[-97.75, 30.25, 10],
|
||||
[-97.74, 30.25, 10],
|
||||
[-97.74, 30.26, 10],
|
||||
[-97.75, 30.26, 10],
|
||||
[-97.75, 30.25, 10],
|
||||
];
|
||||
const hole = [
|
||||
[-97.747, 30.253, 10],
|
||||
[-97.743, 30.253, 10],
|
||||
[-97.743, 30.257, 10],
|
||||
[-97.747, 30.257, 10],
|
||||
];
|
||||
const full = polygonShape([square]);
|
||||
const holed = polygonShape([square, hole]);
|
||||
assert.equal(full.indices.length, 6, 'the closing vertex is not repeated');
|
||||
assert.ok(
|
||||
Math.abs(area(holed) / area(full) - (1 - 0.16)) < 0.01,
|
||||
`the hole is left out: ${area(holed) / area(full)}`,
|
||||
);
|
||||
for (let k = 0; k < holed.attributes.position.values.length / 3; k++)
|
||||
assert.ok(Math.abs(vertex(holed, k).height - 10) < 1e-6);
|
||||
assert.equal(polygonShape([[square[0], square[1]]]), null);
|
||||
});
|
||||
|
||||
test('a long pack edge is split so no step spans more than a degree', () => {
|
||||
const shape = polygonShape([
|
||||
[
|
||||
[0, 0],
|
||||
[3, 0],
|
||||
[3, 1],
|
||||
[0, 1],
|
||||
],
|
||||
]);
|
||||
assert.equal(shape.attributes.position.values.length / 3, 8);
|
||||
});
|
||||
|
||||
test('a pack image rectangle maps its texture from the south-west corner', () => {
|
||||
const shape = rectangleShape([-97.8, 30.2, -97.7, 30.3], 25);
|
||||
const st = shape.attributes.st.values;
|
||||
assert.deepEqual([...st.slice(0, 2)], [0, 0]);
|
||||
assert.deepEqual([...st.slice(-2)], [1, 1]);
|
||||
const first = vertex(shape, 0),
|
||||
last = vertex(shape, st.length / 2 - 1);
|
||||
assert.ok(Math.abs(geo.math.toDegrees(first.longitude) + 97.8) < 1e-9);
|
||||
assert.ok(Math.abs(geo.math.toDegrees(last.latitude) - 30.3) < 1e-9);
|
||||
assert.ok(Math.abs(first.height - 25) < 1e-6);
|
||||
assert.equal(shape.indices.length, 6);
|
||||
const wide = rectangleShape([10, 0, 13.5, 1]);
|
||||
assert.equal(wide.indices.length, 4 * 1 * 6, 'cells of at most a degree');
|
||||
});
|
||||
Reference in new issue
Block a user