118 lines
6.1 KiB
JavaScript
118 lines
6.1 KiB
JavaScript
#!/usr/bin/env node
|
||
// Generates production-line-assembly.html from line-spec + exported STLs.
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
|
||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||
const webRoot = path.resolve(here, "../..");
|
||
const spec = JSON.parse(fs.readFileSync(path.join(here, "line-spec.json"), "utf8"));
|
||
const outFile = path.join(webRoot, "teaching", "cases", "production-line-assembly.html");
|
||
fs.copyFileSync(path.join(here, "line-spec.json"), path.join(webRoot, "teaching", "cases", "production-line-spec.json"));
|
||
const parts = spec.models.flatMap(model => model.parts.map(part => ({
|
||
partName: part.partName, model: model.name, modelLabel: model.label,
|
||
color: part.color, worldBBox: part.worldBBox,
|
||
})));
|
||
const models = spec.models.map(model => ({ name: model.name, label: model.label }));
|
||
const groundModels = new Set(["GroundPlane", "Plane", "Plane001"]);
|
||
const machineParts = parts.filter(part => !groundModels.has(part.model));
|
||
const machineBox = [0, 1, 2].map(axis => Math.min(...machineParts.map(part => part.worldBBox[axis])))
|
||
.concat([0, 1, 2].map(axis => Math.max(...machineParts.map(part => part.worldBBox[axis + 3]))));
|
||
|
||
const html = `<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<link rel="icon" href="data:," />
|
||
<title>生产线 · 27 个模型 / 88 个部件重建结果</title>
|
||
<style>
|
||
html, body { margin: 0; height: 100%; background: #0d1318; color: #dbe6ee; font: 14px/1.5 system-ui, "Noto Sans SC", sans-serif; overflow: hidden; }
|
||
header { position: fixed; top: 0; left: 0; right: 0; z-index: 2; display: flex; align-items: center; gap: 14px; padding: 10px 16px; background: #0b1116e6; border-bottom: 1px solid #2a3a45; flex-wrap: wrap; }
|
||
header h1 { margin: 0; font-size: 16px; }
|
||
header select { background: #0b1014; color: #dbe6ee; border: 1px solid #3f3f3f; border-radius: 4px; padding: 3px 6px; }
|
||
header .stats { color: #93a6b3; font-size: 12px; }
|
||
#view { position: fixed; inset: 48px 0 0 0; }
|
||
#hint { position: fixed; left: 16px; bottom: 12px; color: #6f8390; font-size: 12px; z-index: 2; }
|
||
</style>
|
||
<script type="importmap">{ "imports": { "three": "./vendor/three.module.js" } }</script>
|
||
</head>
|
||
<body>
|
||
<header>
|
||
<h1>生产线重建结果</h1>
|
||
<select id="model" aria-label="Filter model"><option value="">全部 27 个模型</option></select>
|
||
<span class="stats" id="stats">加载中…</span>
|
||
</header>
|
||
<div id="view"></div>
|
||
<p id="hint">左键旋转 · 滚轮缩放 · 右键平移</p>
|
||
<script type="module">
|
||
import * as THREE from "three";
|
||
import { OrbitControls } from "./vendor/OrbitControls.js";
|
||
import { STLLoader } from "./vendor/STLLoader.js";
|
||
|
||
const PARTS = ${JSON.stringify(parts)};
|
||
const MODELS = ${JSON.stringify(models)};
|
||
const host = document.getElementById("view");
|
||
const select = document.getElementById("model");
|
||
for (const model of MODELS) {
|
||
const option = document.createElement("option");
|
||
option.value = model.name; option.textContent = model.label;
|
||
select.appendChild(option);
|
||
}
|
||
const scene = new THREE.Scene();
|
||
scene.background = new THREE.Color(0x0d1318);
|
||
const camera = new THREE.PerspectiveCamera(42, host.clientWidth / host.clientHeight, 1, 20000);
|
||
camera.up.set(0, 0, 1);
|
||
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
||
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
||
renderer.setSize(host.clientWidth, host.clientHeight);
|
||
host.appendChild(renderer.domElement);
|
||
scene.add(new THREE.HemisphereLight(0xffffff, 0x334455, 2.4));
|
||
const key = new THREE.DirectionalLight(0xffffff, 2.2); key.position.set(400, 700, 500); scene.add(key);
|
||
const fill = new THREE.DirectionalLight(0xffffff, 0.8); fill.position.set(-600, 200, -400); scene.add(fill);
|
||
|
||
const root = new THREE.Group();
|
||
scene.add(root);
|
||
const byModel = new Map();
|
||
const loader = new STLLoader();
|
||
let loaded = 0;
|
||
for (const part of PARTS) {
|
||
try {
|
||
const buffer = await (await fetch("./line-parts/" + encodeURIComponent(part.partName) + ".stl")).arrayBuffer();
|
||
const geometry = loader.parse(buffer);
|
||
const color = new THREE.Color().setRGB(part.color[0], part.color[1], part.color[2], THREE.SRGBColorSpace);
|
||
const mesh = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ color, metalness: 0.3, roughness: 0.6 }));
|
||
mesh.userData = { partName: part.partName, model: part.model };
|
||
if (!byModel.has(part.model)) { const group = new THREE.Group(); group.name = part.model; byModel.set(part.model, group); root.add(group); }
|
||
byModel.get(part.model).add(mesh);
|
||
loaded++;
|
||
} catch (error) { console.warn("part", part.partName, error); }
|
||
}
|
||
const [xmin, ymin, zmin, xmax, ymax, zmax] = ${JSON.stringify(machineBox)};
|
||
const size = new THREE.Vector3(xmax - xmin, ymax - ymin, zmax - zmin);
|
||
const center = new THREE.Vector3((xmin + xmax) / 2, (ymin + ymax) / 2, (zmin + zmax) / 2);
|
||
const maxSize = Math.max(size.x, size.y, size.z);
|
||
camera.position.set(center.x + maxSize * 1.05, center.y - maxSize * 1.35, center.z + maxSize * 0.75);
|
||
camera.lookAt(center);
|
||
const controls = new OrbitControls(camera, renderer.domElement);
|
||
controls.target.copy(center); controls.enableDamping = true; controls.dampingFactor = 0.08; controls.update();
|
||
document.getElementById("stats").textContent =
|
||
loaded + " / ${parts.length} 个部件 · ${spec.modelCount} 个模型 · 设备区 " + (size.x / 1000).toFixed(2) + " × " + (size.y / 1000).toFixed(2) + " × " + (size.z / 1000).toFixed(2) + " m";
|
||
select.addEventListener("change", () => {
|
||
for (const [model, group] of byModel) group.visible = !select.value || select.value === model;
|
||
});
|
||
addEventListener("resize", () => {
|
||
camera.aspect = host.clientWidth / host.clientHeight; camera.updateProjectionMatrix();
|
||
renderer.setSize(host.clientWidth, host.clientHeight);
|
||
});
|
||
const animate = () => { requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); };
|
||
animate();
|
||
window.__lineAssemblyStats = { loaded, models: byModel.size };
|
||
window.__lineAssemblyReady = true;
|
||
</script>
|
||
</body>
|
||
</html>
|
||
`;
|
||
fs.writeFileSync(outFile, html);
|
||
console.log(JSON.stringify({ ok: true, outFile, parts: parts.length, models: models.length }));
|