MotrixSimWeb/scripts/generate-composite-manifest.mjs

143 lines
5.8 KiB
JavaScript

#!/usr/bin/env node
import { writeFile } from "node:fs/promises";
import path from "node:path";
const G1_URL = process.env.G1_MANIFEST_URL || "http://127.0.0.1:18087/manifest";
const STORE_URL = process.env.STORE_MANIFEST_URL || "http://127.0.0.1:18085/manifest";
const OUT_FILE = process.argv[2] || "public/generated/g1-store-blocks-lite.manifest.json";
const STORE_PRODUCT_LIMIT = Number(process.env.STORE_PRODUCT_LIMIT || "90");
function resolveManifestUrls(manifest, manifestUrl) {
const copy = structuredClone(manifest);
if (copy.state?.url) copy.state.url = new URL(copy.state.url, manifestUrl).href;
if (copy.state?.controlUrl) copy.state.controlUrl = new URL(copy.state.controlUrl, manifestUrl).href;
for (const object of copy.objects || []) {
if (object.geometry?.kind === "mesh" && object.geometry.url) {
object.geometry.url = new URL(object.geometry.url, manifestUrl).href;
}
}
for (const material of copy.materials || []) {
if (material.textureUrl) {
material.textureUrl = new URL(material.textureUrl, manifestUrl).href;
}
}
return copy;
}
function prefixMaterial(id, prefix) {
if (!id || id === "default" || id === "fallback") return id;
return `${prefix}_${id}`;
}
function tuneStoreMaterial(material) {
const next = { ...material };
if (next.id === "store_Metal_White") {
next.color = "#687382";
next.roughness = 0.84;
next.metalness = 0.02;
return next;
}
if (next.id.startsWith("store_Cans_")) {
next.roughness = 0.5;
next.metalness = 0.12;
return next;
}
if (next.id.startsWith("store_Bottle_") || next.id.startsWith("store_Tetra_")) {
next.roughness = 0.48;
next.metalness = 0.0;
return next;
}
return next;
}
function prefixManifest(manifest, prefix, objectFilter = () => true) {
const materialMap = new Map();
const materials = [];
for (const material of manifest.materials || []) {
const id = prefixMaterial(material.id, prefix);
materialMap.set(material.id, id);
const next = { ...material, id };
materials.push(prefix === "store" ? tuneStoreMaterial(next) : next);
}
const objects = (manifest.objects || []).filter(objectFilter).map((object) => {
const next = structuredClone(object);
next.id = `${prefix}_${object.id}`;
next.name = object.name ? `${prefix}_${object.name}` : next.id;
if (next.material) {
next.material = materialMap.get(next.material) || prefixMaterial(next.material, prefix);
}
if (next.geometry?.materialOverride) {
next.geometry.materialOverride = materialMap.get(next.geometry.materialOverride) || prefixMaterial(next.geometry.materialOverride, prefix);
}
return next;
});
return { materials, objects };
}
async function loadManifest(url) {
const response = await fetch(url, { cache: "no-store" });
if (!response.ok) {
throw new Error(`${url} ${response.status}`);
}
return resolveManifestUrls(await response.json(), url);
}
const [g1, store] = await Promise.all([loadManifest(G1_URL), loadManifest(STORE_URL)]);
let productCount = 0;
const storeFilter = (object) => {
const url = object.geometry?.url || "";
if (url.includes("Shelf")) return true;
if (productCount < STORE_PRODUCT_LIMIT) {
productCount += 1;
return true;
}
return false;
};
const g1Part = prefixManifest(g1, "g1");
const storePart = prefixManifest(store, "store", storeFilter);
const materials = [
...g1Part.materials,
...storePart.materials,
{ id: "demo_red", color: "#e11d48", roughness: 0.55 },
{ id: "demo_blue", color: "#2563eb", roughness: 0.55 },
{ id: "demo_green", color: "#16a34a", roughness: 0.55 },
{ id: "demo_yellow", color: "#eab308", roughness: 0.55 },
];
const objects = [
...g1Part.objects,
...storePart.objects,
{ id: "demo_cube_red", name: "demo_cube_red", geometry: { kind: "primitive", primitive: "box", size: [0.12, 0.12, 0.12] }, material: "demo_red", pose: { position: [0.85, -0.45, 0.06] }, castShadow: true, receiveShadow: true },
{ id: "demo_cube_blue", name: "demo_cube_blue", geometry: { kind: "primitive", primitive: "box", size: [0.1, 0.16, 0.09] }, material: "demo_blue", pose: { position: [1.05, -0.28, 0.05], euler: [0, 0, 0.4] }, castShadow: true, receiveShadow: true },
{ id: "demo_ball_green", name: "demo_ball_green", geometry: { kind: "primitive", primitive: "sphere", radius: 0.075 }, material: "demo_green", pose: { position: [0.65, -0.22, 0.08] }, castShadow: true, receiveShadow: true },
{ id: "demo_capsule_yellow", name: "demo_capsule_yellow", geometry: { kind: "primitive", primitive: "capsule", radius: 0.035, length: 0.22 }, material: "demo_yellow", pose: { position: [1.2, -0.55, 0.08], euler: [0, 1.5708, 0.25] }, castShadow: true, receiveShadow: true },
];
const manifest = {
version: "1.0",
name: "G1 + Store Lite + Demo blocks",
description: "Composite visual manifest: complete G1 with live MotrixSim state, selected Store shelves/products, and demo primitives.",
units: "m",
coordinateSystem: g1.coordinateSystem || { up: "Z" },
materials,
objects,
lights: [
{ type: "ambient", color: "#ffffff", intensity: 0.24 },
{ type: "hemisphere", color: "#e0f2fe", intensity: 0.72 },
{ type: "directional", color: "#ffffff", intensity: 2.15, position: [4, -5, 8] },
{ type: "directional", color: "#dbeafe", intensity: 0.85, position: [-4, 3, 4] },
],
camera: { position: [3.4, -4.4, 2.2], target: [0.35, 0.55, 0.9], fov: 46 },
state: g1.state,
controls: g1.controls,
composite: {
type: "visual-manifest-merge-lite",
sources: [G1_URL, STORE_URL],
storeObjects: storePart.objects.length,
note: "Store/demo objects are visual/static; G1 live state/control comes from 18087.",
},
};
await writeFile(path.resolve(OUT_FILE), `${JSON.stringify(manifest, null, 2)}\n`);
console.log(JSON.stringify({ output: OUT_FILE, objects: objects.length, materials: materials.length, storeObjects: storePart.objects.length }, null, 2));