Files
FreeCADWasmDev/web/tools/line-course/build-reference-page.mjs

110 lines
5.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// Generates teaching/cases/production-line-reference.html: a Z-up three.js
// viewer with a 27-model selector. GLBs are served from the line-models symlink.
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 models = JSON.parse(fs.readFileSync(path.join(webRoot, "public", "line", "models.json"), "utf8")).models;
const outFile = path.join(webRoot, "teaching", "cases", "production-line-reference.html");
const esc = value => String(value ?? "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
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 个 GLB</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; }
header h1 { margin: 0; font-size: 16px; }
header select { background: #0b1014; color: #dbe6ee; border: 1px solid #3f3f3f; border-radius: 4px; padding: 3px 6px; max-width: 240px; }
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="Reference model"></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 { GLTFLoader } from "./vendor/GLTFLoader.js";
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.file;
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, 0.001, 1000);
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 controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; controls.dampingFactor = 0.08;
let root = null;
const load = async file => {
document.getElementById("stats").textContent = "加载中…";
if (root) {
scene.remove(root);
root.traverse(child => { if (child.isMesh) { child.geometry?.dispose(); child.material?.dispose?.(); } });
root = null;
}
const gltf = await new GLTFLoader().loadAsync("./line-models/" + encodeURIComponent(file));
root = gltf.scene;
scene.add(root);
const box = new THREE.Box3().setFromObject(root);
const size = box.getSize(new THREE.Vector3());
const center = box.getCenter(new THREE.Vector3());
root.position.sub(center);
const maxSize = Math.max(size.x, size.y, size.z) || 1;
camera.position.set(maxSize * 1.4, -maxSize * 2.0, maxSize * 1.1);
camera.near = maxSize / 500; camera.far = maxSize * 50; camera.updateProjectionMatrix();
controls.target.set(0, 0, 0); controls.update();
let meshes = 0;
root.traverse(child => { if (child.isMesh) meshes++; });
const stats = meshes + " 个网格 · 包围盒 " + size.x.toFixed(2) + " × " + size.y.toFixed(2) + " × " + size.z.toFixed(2) + " m";
document.getElementById("stats").textContent = stats;
window.__lineReferenceStats = { file, meshes, size: [size.x, size.y, size.z], stats };
};
select.addEventListener("change", () => load(select.value).catch(error => {
document.getElementById("stats").textContent = "加载失败:" + String(error?.message ?? error);
}));
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();
await load(MODELS[0].file);
window.__lineReferenceReady = true;
</script>
</body>
</html>
`;
fs.writeFileSync(outFile, html);
console.log(JSON.stringify({ ok: true, outFile, models: models.length }));