/** * core/geometry/assembly.js —— 零件分层 → 3D 装配位姿 → 爆炸位移(纯函数)。 * 坐标:整机底面中心为原点,Z 向上,单位 mm。分��偏移沿 Z。 */ export const DEFAULT_PARTS = [ { id: 'front-shell', name: '前壳', role: 'lid', split: 0.88 }, { id: 'back-shell', name: '后盖', role: 'back' }, { id: 'button', name: '按键', role: 'button' }, { id: 'hanger', name: '挂环', role: 'ring' }, { id: 'screen', name: '显示屏', role: 'screen', transparent: 0.6 }, { id: 'battery', name: '电池', role: 'battery' }, { id: 'pcb', name: '主板', role: 'pcb' }, { id: 'brass', name: '铜柱', role: 'brass' }, { id: 'clip', name: '卡扣', role: 'clip' }, ]; /** 零件纵向 [z0, z1] 与横向包络(用于爆炸图与单件高亮) */ export function partLayer(part, envelopeH, idx, total) { const role = part.role; const H = envelopeH; const band = { lid: [H * 0.7, H], back: [0, H * 0.7], shell: [0, H], button: [H * 0.3, H * 0.55], ring: [H * 0.2, H * 0.6], screen: [H * 0.72, H * 0.95], battery: [H * 0.4, H * 0.65], pcb: [H * 0.55, H * 0.72], brass: [H * 0.3, H * 0.7], clip: [H * 0.6, H * 0.7], }[role] || [H * 0.4, H * 0.6]; return { z0: band[0], z1: band[1], order: idx, of: total }; } export function buildParts(shape, related = []) { const list = related.length >= 9 ? related : DEFAULT_PARTS; const H = +(shape.defaults || shape).height || 10; return list.map((p, i) => ({ ...p, layer: partLayer(p, H, i, list.length) })); } /** * 爆炸位移:0 → 全部贴合;100% → 沿 Z 拉开。 * external 外件(lid/back/ring/button)向上/下走,internal 按层序带状散开。 * 返回 [{id, dz, isExternal}] */ export function explodeOffsets(parts, pct = 0) { const t = Math.max(0, Math.min(100, pct)) / 100; const H = Math.max(...parts.map((p) => p.layer.z1), 1); const step = H * 0.55 * t + 6 * t; return parts.map((p) => { const role = p.role; const isExternal = ['lid', 'back', 'shell', 'ring', 'button'].includes(role); let dz = 0; if (role === 'lid') dz = step * 1.4; else if (role === 'back') dz = -step * 1.0; else if (role === 'ring') dz = -step * 1.5; else if (role === 'button') dz = step * 0.6; else { const mid = (p.layer.z0 + p.layer.z1) / 2; dz = (mid - H / 2) * t * 0.6; if (role === 'screen') dz += step * 0.9; if (role === 'battery') dz -= step * 0.4; } return { id: p.id, role, dz, isExternal }; }); }