mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-24 04:02:19 +00:00
Merge main into denis/iss-live-telemetry
Conflicts: the locale catalogs, package boundaries and format scope are three-way merges (entries main removed, such as displayPanelScroll.js, stay removed); applicationShell.js and ContextView.vue keep main's Space Missions panel and the STATION and LIVE wiring, moved back into _initGlobalContextPanel; installations take main's feedback module, and their label box joins main's card-on-top click rule; ports.test.mjs lists modelParts and modelNodes; CURRENT-STATE.md keeps both edits. zh-Hant is regenerated from zh-CN, as main now requires. "Below" keeps main's translations.
This commit is contained in:
commit
0620d01140
1526 files changed
+52168
-11320
No files matched your search
@@ -14,6 +14,7 @@ import { AUSTIN, SCRIPT_VIEWPORT } from '../cameraScripts.js';
|
||||
import { deepEqual, equal, near, nearVec, ok, throws } from '../expect.js';
|
||||
import { iconPosition } from '../../motion/iconMotion.js';
|
||||
import { encodeVectorTile } from '../../vectorBase/encode.js';
|
||||
import { partsModelGlb } from '../partsModel.js';
|
||||
|
||||
/** 1 × 1 PNGs (red, green, blue), so imagery checks load nothing from the network. */
|
||||
const PIXEL_PNGS = [
|
||||
@@ -4113,6 +4114,93 @@ export function portChecks() {
|
||||
},
|
||||
{ capability: 'models', bareSky: true },
|
||||
),
|
||||
check(
|
||||
'draw.models.paint',
|
||||
'a lit model draws its vertex paint (`COLOR_0`) under the sun, stays readable with the sun below the horizon, and a tint mixed in keeps the paint',
|
||||
async (h, r) => {
|
||||
if (h.pixels === false)
|
||||
return { skip: 'this harness reads no drawn pixels' };
|
||||
// Austin at 13:00 local (sun about 80° up) and at 03:00 (about 32° below).
|
||||
const DAY = Date.parse('2026-06-21T18:00:00Z');
|
||||
const NIGHT = Date.parse('2026-06-21T08:00:00Z');
|
||||
r.frame.setTime(DAY);
|
||||
await overAustin(h, r, 200);
|
||||
const set = r.draw.models({ id: 'conf:models.paint' });
|
||||
const ready = [];
|
||||
set.on('ready', (id) => ready.push(id));
|
||||
const pos = h.toEcef({ ...AUSTIN, height: 0 });
|
||||
const model = (extra = {}) => ({
|
||||
id: 'm',
|
||||
pos,
|
||||
url: 'conformance:painted-model.glb',
|
||||
// Red paint, as a fleet model's livery: a white material, its
|
||||
// colour per vertex.
|
||||
gltf: partsModelGlb({ paint: [0.8, 0.05, 0.05] }),
|
||||
orientation: { heading: 0, pitch: 0, roll: 0 },
|
||||
environmentMap: false,
|
||||
tint: '#ffffff',
|
||||
colorBlend: { mode: 'highlight' },
|
||||
...extra,
|
||||
});
|
||||
try {
|
||||
set.set([model()]);
|
||||
for (let i = 0; i < 600 && !ready.length; i++) await drawn(h, r, 1);
|
||||
deepEqual(ready, ['m'], 'ready');
|
||||
const s = r.query.project(pos);
|
||||
ok(s, 'the model projects');
|
||||
// The mean colour of a patch on the model's top, seen from above.
|
||||
const mean = async (time) => {
|
||||
r.frame.setTime(time);
|
||||
await drawn(h, r, 3);
|
||||
await settled(r);
|
||||
r.frame.setTime(time);
|
||||
r.frame.renderNow();
|
||||
const image = r.readPixels({
|
||||
crop: {
|
||||
x: Math.round(s.x) - 4,
|
||||
y: Math.round(s.y) - 4,
|
||||
width: 9,
|
||||
height: 9,
|
||||
},
|
||||
});
|
||||
const sum = [0, 0, 0];
|
||||
for (let i = 0; i < image.data.length; i += 4)
|
||||
for (let c = 0; c < 3; c++) sum[c] += image.data[i + c];
|
||||
return sum.map((v) => Math.round(v / (image.data.length / 4)));
|
||||
};
|
||||
const day = await mean(DAY);
|
||||
ok(
|
||||
day[0] > 100 && day[0] > 2 * day[1] && day[0] > 2 * day[2],
|
||||
`in daylight, its red paint: ${day}`,
|
||||
);
|
||||
const night = await mean(NIGHT);
|
||||
ok(
|
||||
night[0] >= 30 && night[0] > 2 * night[1],
|
||||
`with the sun below the horizon, still its paint, not black: ${night}`,
|
||||
);
|
||||
ok(
|
||||
night[0] < day[0],
|
||||
`darker at night than by day: ${night}, ${day}`,
|
||||
);
|
||||
// A tint with no red, as a tracked cyan: mixed in, the paint shows.
|
||||
set.upsert([
|
||||
model({
|
||||
tint: '#00ffff',
|
||||
colorBlend: { mode: 'mix', amount: 0.3 },
|
||||
}),
|
||||
]);
|
||||
const tinted = await mean(DAY);
|
||||
ok(
|
||||
tinted[0] > 60 && tinted[1] > day[1] + 30,
|
||||
`a cyan tint mixed in at 0.3 keeps the red paint and adds its colour: ${tinted}`,
|
||||
);
|
||||
deepEqual(ready, ['m'], 'never loaded again');
|
||||
} finally {
|
||||
set.dispose();
|
||||
}
|
||||
},
|
||||
{ capability: 'models', bareSky: true },
|
||||
),
|
||||
check(
|
||||
'draw.models.bytes',
|
||||
'a model given its bytes is drawn from them, its url never fetched',
|
||||
@@ -4188,6 +4276,186 @@ export function portChecks() {
|
||||
},
|
||||
{ capability: 'models' },
|
||||
),
|
||||
check(
|
||||
'draw.models.parts',
|
||||
'a model’s parts are moved, hidden, clamped and spun live, without loading again',
|
||||
async (h, r) => {
|
||||
await overAustin(h, r, 200);
|
||||
const set = r.draw.models({ id: 'conf:models.parts' });
|
||||
const ready = [];
|
||||
const failed = [];
|
||||
set.on('ready', (id) => ready.push(id));
|
||||
set.on('loadFailed', (id) => failed.push(id));
|
||||
// `partsModel.js`: a body, a pod on it, a ram on that, an arm ahead.
|
||||
const models = {
|
||||
m: partsModelGlb(),
|
||||
// The same model, its mesh node turned, scaled and moved.
|
||||
n: partsModelGlb({ scale: 4, turnDeg: 90, translation: [3, 2, -1] }),
|
||||
};
|
||||
const pos = h.toEcef({ ...AUSTIN, height: 0 });
|
||||
const model = (id, parts, extra = {}) => ({
|
||||
id,
|
||||
pos,
|
||||
url: `conformance:parts-model-${id}.glb`,
|
||||
gltf: models[id],
|
||||
orientation: { heading: 0, pitch: 0, roll: 0 },
|
||||
environmentMap: false,
|
||||
meta: { n: 1 },
|
||||
...extra,
|
||||
...(parts ? { parts } : {}),
|
||||
});
|
||||
const metresPerLon = 111320 * Math.cos((AUSTIN.lat * Math.PI) / 180);
|
||||
/** The point `east`, `north` metres from the model, `up` above it. */
|
||||
const at = ([east, north], metres, up = 0) =>
|
||||
h.toEcef({
|
||||
lon: AUSTIN.lon + (east * metres) / metresPerLon,
|
||||
lat: AUSTIN.lat + (north * metres) / 110574,
|
||||
height: up,
|
||||
});
|
||||
const pickedAt = (p) => {
|
||||
const s = r.query.project(p);
|
||||
return (
|
||||
s !== null &&
|
||||
r.query.drillPick(s.x, s.y).some((hit) => hit.set === set.id)
|
||||
);
|
||||
};
|
||||
// The compass, clockwise seen from above.
|
||||
const COMPASS = [
|
||||
[0, 1],
|
||||
[1, 0],
|
||||
[0, -1],
|
||||
[-1, 0],
|
||||
];
|
||||
const clockwise = ([e, n]) => [n, -e];
|
||||
const same = (a, b) => a[0] === b[0] && a[1] === b[1];
|
||||
/** The compass points 15 m off at which the model is picked. */
|
||||
const pickedAround = (up = 0) =>
|
||||
COMPASS.filter((d) => pickedAt(at(d, 15, up)));
|
||||
const picks = h.pixels !== false;
|
||||
const look = async () => {
|
||||
await drawn(h, r, 3);
|
||||
await settled(r);
|
||||
};
|
||||
const show = async (item) => {
|
||||
set.upsert([item]);
|
||||
await look();
|
||||
};
|
||||
set.set([model('m')]);
|
||||
for (let i = 0; i < 600 && !ready.length && !failed.length; i++)
|
||||
await drawn(h, r, 1);
|
||||
deepEqual(ready, ['m'], 'ready');
|
||||
await look();
|
||||
if (picks) ok(pickedAt(pos), 'drawn whole, at rest');
|
||||
|
||||
await show(model('m', { poses: { pod: { hidden: true } } }));
|
||||
if (picks) ok(pickedAt(pos), 'the pod hidden, the body drawn');
|
||||
await show(model('m', { poses: { body: { hidden: true } } }));
|
||||
if (picks) ok(!pickedAt(pos), 'the body hidden, and all riding it');
|
||||
|
||||
// Moves go where they say: forward, then right of that.
|
||||
await show(model('m', { poses: { body: { offsetM: [0, 15, 0] } } }));
|
||||
const ahead = picks ? pickedAround() : [];
|
||||
if (picks) equal(ahead.length, 1, 'moved forward, drawn 15 m one way');
|
||||
await show(model('m', { poses: { body: { offsetM: [15, 0, 0] } } }));
|
||||
if (picks) {
|
||||
ok(!pickedAt(pos), 'moved right, gone from its place');
|
||||
const right = pickedAround();
|
||||
equal(right.length, 1, 'moved right, drawn 15 m one way');
|
||||
ok(
|
||||
ahead.length === 1 &&
|
||||
right.length === 1 &&
|
||||
same(right[0], clockwise(ahead[0])),
|
||||
'its right is a quarter turn clockwise of its forward',
|
||||
);
|
||||
}
|
||||
const forward = ahead[0] ?? COMPASS[0];
|
||||
const rightward = clockwise(forward);
|
||||
const back = clockwise(clockwise(forward));
|
||||
|
||||
// A mesh node turned, scaled and moved: parts move in the model's
|
||||
// frame all the same.
|
||||
set.set([model('n')]);
|
||||
for (let i = 0; i < 600 && ready.length < 2 && !failed.length; i++)
|
||||
await drawn(h, r, 1);
|
||||
deepEqual(ready, ['m', 'n'], 'the turned-node model ready');
|
||||
await show(model('n', { poses: { body: { offsetM: [15, 0, 0] } } }));
|
||||
if (picks) {
|
||||
ok(
|
||||
!pickedAt(pos),
|
||||
'its node turned: moved right, gone from its place',
|
||||
);
|
||||
ok(pickedAt(at(rightward, 15)), 'and drawn 15 m to its right');
|
||||
}
|
||||
set.set([model('m')]);
|
||||
for (let i = 0; i < 600 && ready.length < 3 && !failed.length; i++)
|
||||
await drawn(h, r, 1);
|
||||
await look();
|
||||
|
||||
// Stretched twice its length: a move along it stretches with it.
|
||||
const stretched = { axisScale: [2, 1, 1] };
|
||||
await show(model('m', undefined, stretched));
|
||||
if (picks) ok(!pickedAt(at(back, 30)), 'stretched, it ends 20 m back');
|
||||
await show(
|
||||
model('m', { poses: { body: { offsetM: [0, -7.5, 0] } } }, stretched),
|
||||
);
|
||||
if (picks)
|
||||
ok(
|
||||
pickedAt(at(back, 30)),
|
||||
'moved 7.5 m back stretched, drawn 15 m back',
|
||||
);
|
||||
|
||||
// A slide's travel stays within its limits: the ram stops at 20 m.
|
||||
const ramAhead = at(forward, 20, 23);
|
||||
await show(model('m'));
|
||||
if (picks) ok(!pickedAt(ramAhead), 'the ram at rest, on top');
|
||||
await show(model('m', { poses: { ram: { offsetM: [0, 40, 0] } } }));
|
||||
if (picks) ok(pickedAt(ramAhead), 'pushed 40 m, the ram stops at 20 m');
|
||||
await show(model('m', { deploy: { ram: 1 } }));
|
||||
if (picks) ok(pickedAt(ramAhead), 'deployed, the ram is out 20 m');
|
||||
|
||||
// A hinge's turn stays within its limits: the arm swings 45° left
|
||||
// at most, and not at all to the right.
|
||||
const left = clockwise(back);
|
||||
const armAt = (d, m = 15) => at(d, m, 3);
|
||||
const diagonal = (side) =>
|
||||
h.toEcef({
|
||||
lon: AUSTIN.lon + ((forward[0] + side[0]) * 10.6) / metresPerLon,
|
||||
lat: AUSTIN.lat + ((forward[1] + side[1]) * 10.6) / 110574,
|
||||
height: 3,
|
||||
});
|
||||
if (picks) ok(pickedAt(armAt(forward)), 'the arm at rest, ahead');
|
||||
await show(model('m', { poses: { arm: { yawDeg: -90 } } }));
|
||||
if (picks) {
|
||||
ok(!pickedAt(armAt(forward)), 'turned left, the arm leaves');
|
||||
ok(!pickedAt(armAt(left)), 'and stops short of a quarter turn');
|
||||
ok(pickedAt(diagonal(left)), 'at 45° ahead and to the left');
|
||||
}
|
||||
await show(model('m', { poses: { arm: { yawDeg: 90 } } }));
|
||||
if (picks) {
|
||||
ok(!pickedAt(armAt(rightward)), 'turned right, the arm does not');
|
||||
ok(pickedAt(armAt(forward)), 'and stays ahead');
|
||||
}
|
||||
|
||||
// A spinning part asks for frames for as long as it spins, so the
|
||||
// scene never settles; wait for frames to be drawn instead.
|
||||
set.upsert([
|
||||
model('m', {
|
||||
spin: { pod: 90 },
|
||||
deploy: { pod: 1, missing: 1 },
|
||||
poses: { pod: { highlight: true }, missing: { hidden: true } },
|
||||
}),
|
||||
]);
|
||||
await drawn(h, r, 3);
|
||||
if (picks) ok(pickedAt(pos), 'spinning and highlighted, where it is');
|
||||
await show(model('m'));
|
||||
if (picks) ok(pickedAt(pos), 'back at rest');
|
||||
deepEqual(ready, ['m', 'n', 'm'], 'posed without loading again');
|
||||
deepEqual(failed, [], 'nothing failed');
|
||||
equal(set.size(), 1, 'one model');
|
||||
set.dispose();
|
||||
},
|
||||
{ capability: ['models', 'modelParts'] },
|
||||
),
|
||||
|
||||
// --- Query -------------------------------------------------------------
|
||||
check(
|
||||
@@ -5222,6 +5490,125 @@ export function portChecks() {
|
||||
},
|
||||
{ capability: 'imageryFrames', bareSky: true },
|
||||
),
|
||||
check(
|
||||
'globe.imagery.decoded',
|
||||
'decoded imagery draws the images its source hands over for the slot shown',
|
||||
async (h, r) => {
|
||||
await overAustin(h, r, 200000);
|
||||
const t0 = r.frame.time();
|
||||
const slots = [t0, t0 + 60000, t0 + 120000];
|
||||
const colours = ['#ff0000', '#0000ff', '#00ff00'];
|
||||
const pixels = h.pixels !== false;
|
||||
/** A solid tile of a slot's colour, or a canvas where none is made. */
|
||||
const imageOf = async (slot) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 16;
|
||||
canvas.height = 16;
|
||||
const g = canvas.getContext('2d');
|
||||
g.fillStyle = colours[slots.indexOf(slot)];
|
||||
g.fillRect(0, 0, 16, 16);
|
||||
return createImageBitmap(canvas);
|
||||
};
|
||||
const asked = [];
|
||||
const source = {
|
||||
kind: 'decoded',
|
||||
slotAt: (ms) => {
|
||||
if (ms < t0 - 30000) return null;
|
||||
let best = slots[0];
|
||||
for (const slot of slots)
|
||||
if (Math.abs(slot - ms) < Math.abs(best - ms)) best = slot;
|
||||
return best;
|
||||
},
|
||||
prefetch: (ms) => slots.filter((slot) => slot > ms),
|
||||
async tile({ tile, slot, signal, urgency }) {
|
||||
asked.push({ slot, urgency });
|
||||
signal.throwIfAborted();
|
||||
// The first ask of each slot is put off: the renderer asks again.
|
||||
if (asked.filter((a) => a.slot === slot).length === 1)
|
||||
return { defer: true };
|
||||
if (tile.level > 0) return { missing: true };
|
||||
return { image: await imageOf(slot) };
|
||||
},
|
||||
};
|
||||
const layer = r.globe.imagery.add({
|
||||
kind: 'single-image',
|
||||
url: PIXEL_PNG,
|
||||
rectangle: {
|
||||
west: AUSTIN.lon - 2,
|
||||
south: AUSTIN.lat - 2,
|
||||
east: AUSTIN.lon + 2,
|
||||
north: AUSTIN.lat + 2,
|
||||
},
|
||||
});
|
||||
const ready = [];
|
||||
layer.on('frameReady', (time) => ready.push(time));
|
||||
const wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
||||
const shows = async (ms, slot, colour, what) => {
|
||||
const before = ready.length;
|
||||
layer.setFrameTime(ms);
|
||||
for (let i = 0; i < 500 && ready.length === before; i++) {
|
||||
await drawn(h, r, 1);
|
||||
await wait(10);
|
||||
}
|
||||
equal(ready.at(-1), slot, `${what}: its slot is ready`);
|
||||
if (!pixels) return;
|
||||
r.frame.renderNow();
|
||||
const s = r.query.project(h.toEcef({ ...AUSTIN, height: 0 }));
|
||||
const image = r.readPixels({
|
||||
crop: {
|
||||
x: Math.round(s.x),
|
||||
y: Math.round(s.y),
|
||||
width: 1,
|
||||
height: 1,
|
||||
},
|
||||
});
|
||||
const [red, green, blue] = image.data;
|
||||
ok(colour(red, green, blue), `${what}: (${red}, ${green}, ${blue})`);
|
||||
};
|
||||
try {
|
||||
throws(
|
||||
() => layer.setFrames({ kind: 'decoded', slotAt: () => null }),
|
||||
'a decoded source with no tile',
|
||||
);
|
||||
layer.setFrames(source, { clock: 'own', prefetch: 1 });
|
||||
await shows(
|
||||
t0 + 10000,
|
||||
t0,
|
||||
(rr, g, b) => rr > 180 && g < 70 && b < 70,
|
||||
'the first slot',
|
||||
);
|
||||
if (pixels) {
|
||||
ok(
|
||||
asked.some((a) => a.slot === t0 && a.urgency === 'shown'),
|
||||
'the slot shown is asked for as shown',
|
||||
);
|
||||
ok(
|
||||
asked.some(
|
||||
(a) => a.slot === t0 + 60000 && a.urgency === 'prefetch',
|
||||
),
|
||||
'the next slot is asked for ahead, as a prefetch',
|
||||
);
|
||||
ok(
|
||||
!asked.some((a) => a.slot === t0 + 120000),
|
||||
'no more slots are held ahead than prefetch allows',
|
||||
);
|
||||
}
|
||||
await shows(
|
||||
t0 + 65000,
|
||||
t0 + 60000,
|
||||
(rr, g, b) => b > 180 && rr < 70 && g < 70,
|
||||
'the second slot',
|
||||
);
|
||||
ready.length = 0;
|
||||
layer.setFrameTime(t0 - 60000);
|
||||
for (let i = 0; i < 20; i++) await drawn(h, r, 1);
|
||||
deepEqual(ready, [], 'a time with no slot announces nothing');
|
||||
} finally {
|
||||
layer.remove();
|
||||
}
|
||||
},
|
||||
{ capability: ['imageryFrames', 'imageryDecoded'], bareSky: true },
|
||||
),
|
||||
check(
|
||||
'globe.withoutImageHooks',
|
||||
'without imageHooks, a spec with main-thread tile hooks throws',
|
||||
|
||||
@@ -0,0 +1,298 @@
|
||||
/**
|
||||
* @module render/conformance/partsModel
|
||||
* @description A glTF model with moving parts, built in code for the
|
||||
* conformance checks (`PARTS_MODEL_TABLE` says what its parts are). One
|
||||
* mesh whose vertices carry their part index (`_PART`), its node scaled
|
||||
* (by ten unless told otherwise: the mesh is in tenths of the model's
|
||||
* metres), and the part table in that node's `extras`.
|
||||
*/
|
||||
|
||||
/** Each cube face: its normal and two axes across it. */
|
||||
const FACES = [
|
||||
[
|
||||
[1, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, 1],
|
||||
],
|
||||
[
|
||||
[-1, 0, 0],
|
||||
[0, 0, 1],
|
||||
[0, 1, 0],
|
||||
],
|
||||
[
|
||||
[0, 1, 0],
|
||||
[0, 0, 1],
|
||||
[1, 0, 0],
|
||||
],
|
||||
[
|
||||
[0, -1, 0],
|
||||
[1, 0, 0],
|
||||
[0, 0, 1],
|
||||
],
|
||||
[
|
||||
[0, 0, 1],
|
||||
[1, 0, 0],
|
||||
[0, 1, 0],
|
||||
],
|
||||
[
|
||||
[0, 0, -1],
|
||||
[0, 1, 0],
|
||||
[1, 0, 0],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Append a cube of half size `half` centred at `centre` (the model's glTF
|
||||
* frame, metres) as part `part`, its vertices and normals put into the
|
||||
* mesh's own frame by `toMesh`.
|
||||
*/
|
||||
function cube(mesh, centre, half, part, toMesh) {
|
||||
for (const [n, u, v] of FACES) {
|
||||
const base = mesh.positions.length / 3;
|
||||
for (const [su, sv] of [
|
||||
[-1, -1],
|
||||
[1, -1],
|
||||
[1, 1],
|
||||
[-1, 1],
|
||||
]) {
|
||||
const p = [0, 1, 2].map(
|
||||
(i) => centre[i] + half * (n[i] + su * u[i] + sv * v[i]),
|
||||
);
|
||||
mesh.positions.push(...toMesh.point(p));
|
||||
mesh.normals.push(...toMesh.direction(n));
|
||||
mesh.parts.push(part);
|
||||
}
|
||||
// Counter-clockwise seen from outside: u × v is the normal.
|
||||
mesh.indices.push(base, base + 1, base + 2, base, base + 2, base + 3);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The part table, in the model's glTF frame (forward −X, up +Y, right −Z):
|
||||
* a 20 m cube (`body`) centred on the origin; a 10 m cube on top of it
|
||||
* (`pod`) that spins about the vertical through its centre; a 6 m cube on
|
||||
* top of that (`ram`) that slides forward up to 20 m; and a 6 m cube 15 m
|
||||
* ahead of the body at its foot (`arm`), hinged about the vertical through
|
||||
* the body's centre, turning up to 45° to the left.
|
||||
*/
|
||||
export const PARTS_MODEL_TABLE = Object.freeze([
|
||||
{ name: 'body', kind: 'body' },
|
||||
{ name: 'pod', kind: 'spin', pivot: [0, 15, 0], axis: [0, 1, 0] },
|
||||
{
|
||||
name: 'ram',
|
||||
kind: 'slide',
|
||||
pivot: [0, 23, 0],
|
||||
axis: [-1, 0, 0],
|
||||
limits: [0, 20],
|
||||
rest: 0,
|
||||
},
|
||||
{
|
||||
name: 'arm',
|
||||
kind: 'hinge',
|
||||
pivot: [0, 3, 0],
|
||||
axis: [0, 1, 0],
|
||||
limits: [0, 45],
|
||||
rest: 0,
|
||||
},
|
||||
]);
|
||||
|
||||
/** Each part's cube: its centre (glTF frame, metres) and half size. */
|
||||
const CUBES = [
|
||||
[[0, 0, 0], 10],
|
||||
[[0, 15, 0], 5],
|
||||
[[0, 23, 0], 3],
|
||||
[[-15, 3, 0], 3],
|
||||
];
|
||||
|
||||
/**
|
||||
* The model's GLB bytes. Its mesh node is scaled by `scale`, turned
|
||||
* `turnDeg` about its vertical and moved by `translation`; the mesh is
|
||||
* built in that node's own frame, so the model it draws is the same
|
||||
* whatever the node's transform. `paint` (RGB, 0..1) is every vertex's
|
||||
* colour (`COLOR_0`) over a white material, as the fleet models carry
|
||||
* their paint; without it the model has no vertex colours.
|
||||
* @param {{scale?: number, turnDeg?: number,
|
||||
* translation?: [number, number, number],
|
||||
* paint?: [number, number, number]}} [node]
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
export function partsModelGlb({
|
||||
scale = 10,
|
||||
turnDeg = 0,
|
||||
translation = [0, 0, 0],
|
||||
paint,
|
||||
} = {}) {
|
||||
const a = (turnDeg * Math.PI) / 180;
|
||||
const c = Math.cos(a);
|
||||
const sn = Math.sin(a);
|
||||
// The node's turn undone: about +Y by −turnDeg.
|
||||
const unturn = ([x, y, z]) => [c * x - sn * z, y, sn * x + c * z];
|
||||
const toMesh = {
|
||||
point: (p) =>
|
||||
unturn([0, 1, 2].map((i) => p[i] - translation[i])).map((v) => v / scale),
|
||||
direction: unturn,
|
||||
};
|
||||
const mesh = { positions: [], normals: [], parts: [], indices: [] };
|
||||
CUBES.forEach(([centre, half], part) =>
|
||||
cube(mesh, centre, half, part, toMesh),
|
||||
);
|
||||
const count = mesh.parts.length;
|
||||
const positions = new Float32Array(mesh.positions);
|
||||
const normals = new Float32Array(mesh.normals);
|
||||
// A vertex attribute's elements sit on 4-byte boundaries.
|
||||
const parts = new Uint8Array(count * 4);
|
||||
mesh.parts.forEach((part, i) => (parts[i * 4] = part));
|
||||
const indices = new Uint16Array(mesh.indices);
|
||||
// Normalised bytes, RGBA: four to a vertex, on its 4-byte boundary.
|
||||
const colors = paint ? new Uint8Array(count * 4) : null;
|
||||
if (colors)
|
||||
for (let i = 0; i < count; i++)
|
||||
colors.set([...paint.map((c) => Math.round(c * 255)), 255], i * 4);
|
||||
const chunks = [positions, normals, parts, indices];
|
||||
if (colors) chunks.push(colors);
|
||||
const offsets = [];
|
||||
let length = 0;
|
||||
for (const chunk of chunks) {
|
||||
offsets.push(length);
|
||||
length += Math.ceil(chunk.byteLength / 4) * 4;
|
||||
}
|
||||
const bin = new Uint8Array(length);
|
||||
chunks.forEach((chunk, i) =>
|
||||
bin.set(
|
||||
new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength),
|
||||
offsets[i],
|
||||
),
|
||||
);
|
||||
const min = [Infinity, Infinity, Infinity];
|
||||
const max = [-Infinity, -Infinity, -Infinity];
|
||||
for (let i = 0; i < positions.length; i++) {
|
||||
min[i % 3] = Math.min(min[i % 3], positions[i]);
|
||||
max[i % 3] = Math.max(max[i % 3], positions[i]);
|
||||
}
|
||||
const json = {
|
||||
asset: { version: '2.0' },
|
||||
scene: 0,
|
||||
scenes: [{ nodes: [0] }],
|
||||
nodes: [
|
||||
{
|
||||
name: 'parts',
|
||||
mesh: 0,
|
||||
scale: [scale, scale, scale],
|
||||
rotation: [0, Math.sin(a / 2), 0, Math.cos(a / 2)],
|
||||
translation,
|
||||
extras: { parts: PARTS_MODEL_TABLE },
|
||||
},
|
||||
],
|
||||
meshes: [
|
||||
{
|
||||
primitives: [
|
||||
{
|
||||
attributes: {
|
||||
POSITION: 0,
|
||||
NORMAL: 1,
|
||||
_PART: 2,
|
||||
...(colors ? { COLOR_0: 4 } : {}),
|
||||
},
|
||||
indices: 3,
|
||||
material: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
materials: [
|
||||
{
|
||||
pbrMetallicRoughness: {
|
||||
baseColorFactor: [1, 1, 1, 1],
|
||||
metallicFactor: 0,
|
||||
roughnessFactor: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
buffers: [{ byteLength: length }],
|
||||
bufferViews: [
|
||||
{
|
||||
buffer: 0,
|
||||
byteOffset: offsets[0],
|
||||
byteLength: positions.byteLength,
|
||||
target: 34962,
|
||||
},
|
||||
{
|
||||
buffer: 0,
|
||||
byteOffset: offsets[1],
|
||||
byteLength: normals.byteLength,
|
||||
target: 34962,
|
||||
},
|
||||
{
|
||||
buffer: 0,
|
||||
byteOffset: offsets[2],
|
||||
byteLength: parts.byteLength,
|
||||
byteStride: 4,
|
||||
target: 34962,
|
||||
},
|
||||
{
|
||||
buffer: 0,
|
||||
byteOffset: offsets[3],
|
||||
byteLength: indices.byteLength,
|
||||
target: 34963,
|
||||
},
|
||||
...(colors
|
||||
? [
|
||||
{
|
||||
buffer: 0,
|
||||
byteOffset: offsets[4],
|
||||
byteLength: colors.byteLength,
|
||||
byteStride: 4,
|
||||
target: 34962,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
accessors: [
|
||||
{
|
||||
bufferView: 0,
|
||||
componentType: 5126,
|
||||
count,
|
||||
type: 'VEC3',
|
||||
min,
|
||||
max,
|
||||
},
|
||||
{ bufferView: 1, componentType: 5126, count, type: 'VEC3' },
|
||||
{ bufferView: 2, componentType: 5121, count, type: 'SCALAR' },
|
||||
{
|
||||
bufferView: 3,
|
||||
componentType: 5123,
|
||||
count: indices.length,
|
||||
type: 'SCALAR',
|
||||
},
|
||||
...(colors
|
||||
? [
|
||||
{
|
||||
bufferView: 4,
|
||||
componentType: 5121,
|
||||
normalized: true,
|
||||
count,
|
||||
type: 'VEC4',
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
};
|
||||
let text = new TextEncoder().encode(JSON.stringify(json));
|
||||
const padded = new Uint8Array(Math.ceil(text.length / 4) * 4).fill(0x20);
|
||||
padded.set(text);
|
||||
text = padded;
|
||||
const total = 12 + 8 + text.length + 8 + bin.length;
|
||||
const out = new Uint8Array(total);
|
||||
const view = new DataView(out.buffer);
|
||||
view.setUint32(0, 0x46546c67, true);
|
||||
view.setUint32(4, 2, true);
|
||||
view.setUint32(8, total, true);
|
||||
view.setUint32(12, text.length, true);
|
||||
view.setUint32(16, 0x4e4f534a, true);
|
||||
out.set(text, 20);
|
||||
const binAt = 20 + text.length;
|
||||
view.setUint32(binAt, bin.length, true);
|
||||
view.setUint32(binAt + 4, 0x004e4942, true);
|
||||
out.set(bin, binAt + 8);
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user