Add GLB stage manifests
This commit is contained in:
@@ -25,6 +25,7 @@ function normalizeAreaConfig(raw, options = {}) {
|
||||
const compress = normalizeCompressConfig(raw.compress);
|
||||
const compressedFileStem = outputOverrides.compressedFileStem ||
|
||||
`${fileStem}-compressed-webp${compress.textureSize}${compress.meshopt ? "-meshopt" : ""}`;
|
||||
const pipelineDir = path.resolve(outputOverrides.pipelineDir || path.join(areaDir, "_pipeline"));
|
||||
const outputs = {
|
||||
areaDir,
|
||||
geojsonDir: path.resolve(outputOverrides.geojsonDir || path.join(areaDir, "osm2streets_web_out")),
|
||||
@@ -49,7 +50,8 @@ function normalizeAreaConfig(raw, options = {}) {
|
||||
),
|
||||
vehicleRoute: path.resolve(outputOverrides.vehicleRoute || path.join(areaDir, `${fileStem}-vehicle-route.json`)),
|
||||
vehicleModel: path.resolve(outputOverrides.vehicleModel || path.join(areaDir, `${fileStem}-vehicle-car.gltf`)),
|
||||
pipelineDir: path.resolve(outputOverrides.pipelineDir || path.join(areaDir, "_pipeline")),
|
||||
pipelineDir,
|
||||
stageManifestDir: path.resolve(outputOverrides.stageManifestDir || path.join(pipelineDir, "stages")),
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
100
scripts/lib/stage-manifest.js
Normal file
100
scripts/lib/stage-manifest.js
Normal file
@@ -0,0 +1,100 @@
|
||||
"use strict";
|
||||
|
||||
const crypto = require("crypto");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const MANIFEST_VERSION = 1;
|
||||
const BUDGETS = {
|
||||
glbBytes: 25 * 1024 * 1024,
|
||||
glbNodes: 1000,
|
||||
glbImages: 24,
|
||||
};
|
||||
|
||||
function stageManifestPath(area, stage) {
|
||||
return path.join(area.outputs.stageManifestDir, `${stage}.manifest.json`);
|
||||
}
|
||||
|
||||
function readStageManifest(area, stage) {
|
||||
const file = stageManifestPath(area, stage);
|
||||
if (!fs.existsSync(file)) return null;
|
||||
return JSON.parse(fs.readFileSync(file, "utf8"));
|
||||
}
|
||||
|
||||
function writeStageManifest(area, manifest) {
|
||||
const file = stageManifestPath(area, manifest.stage);
|
||||
const payload = {
|
||||
manifestVersion: MANIFEST_VERSION,
|
||||
area: area.id,
|
||||
...manifest,
|
||||
};
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true });
|
||||
const temp = `${file}.tmp`;
|
||||
fs.writeFileSync(temp, `${JSON.stringify(payload, null, 2)}\n`);
|
||||
fs.renameSync(temp, file);
|
||||
console.log(`Stage manifest: ${file}`);
|
||||
return file;
|
||||
}
|
||||
|
||||
function fileRecord(file) {
|
||||
const stat = fs.statSync(file);
|
||||
const record = {
|
||||
path: file,
|
||||
bytes: stat.size,
|
||||
modifiedAt: stat.mtime.toISOString(),
|
||||
};
|
||||
if (stat.isFile()) {
|
||||
record.sha256 = sha256(file);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
function optionalFileRecord(file) {
|
||||
if (!fs.existsSync(file)) return null;
|
||||
return fileRecord(file);
|
||||
}
|
||||
|
||||
function glbSummary(digest) {
|
||||
if (!digest) return null;
|
||||
return {
|
||||
fileBytes: digest.fileBytes,
|
||||
counts: digest.counts,
|
||||
extensionsUsed: digest.extensionsUsed,
|
||||
};
|
||||
}
|
||||
|
||||
function glbBudgetWarnings(label, digest, budgets = BUDGETS) {
|
||||
if (!digest) return [];
|
||||
const warnings = [];
|
||||
if (digest.fileBytes > budgets.glbBytes) {
|
||||
warnings.push(`${label} GLB size ${mb(digest.fileBytes)} MB exceeds budget ${mb(budgets.glbBytes)} MB`);
|
||||
}
|
||||
if (digest.counts.nodes > budgets.glbNodes) {
|
||||
warnings.push(`${label} GLB nodes ${digest.counts.nodes} exceed budget ${budgets.glbNodes}`);
|
||||
}
|
||||
if (digest.counts.images > budgets.glbImages) {
|
||||
warnings.push(`${label} GLB images ${digest.counts.images} exceed budget ${budgets.glbImages}`);
|
||||
}
|
||||
return warnings;
|
||||
}
|
||||
|
||||
function sha256(file) {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(fs.readFileSync(file));
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
function mb(bytes) {
|
||||
return Number((bytes / 1024 / 1024).toFixed(2));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
BUDGETS,
|
||||
fileRecord,
|
||||
glbBudgetWarnings,
|
||||
glbSummary,
|
||||
optionalFileRecord,
|
||||
readStageManifest,
|
||||
stageManifestPath,
|
||||
writeStageManifest,
|
||||
};
|
||||
Reference in New Issue
Block a user