assembly.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * core/geometry/assembly.js —— 零件分层 → 3D 装配位姿 → 爆炸位移(纯函数)。
  3. * 坐标:整机底面中心为原点,Z 向上,单位 mm。分��偏移沿 Z。
  4. */
  5. export const DEFAULT_PARTS = [
  6. { id: 'front-shell', name: '前壳', role: 'lid', split: 0.88 },
  7. { id: 'back-shell', name: '后盖', role: 'back' },
  8. { id: 'button', name: '按键', role: 'button' },
  9. { id: 'hanger', name: '挂环', role: 'ring' },
  10. { id: 'screen', name: '显示屏', role: 'screen', transparent: 0.6 },
  11. { id: 'battery', name: '电池', role: 'battery' },
  12. { id: 'pcb', name: '主板', role: 'pcb' },
  13. { id: 'brass', name: '铜柱', role: 'brass' },
  14. { id: 'clip', name: '卡扣', role: 'clip' },
  15. ];
  16. /** 零件纵向 [z0, z1] 与横向包络(用于爆炸图与单件高亮) */
  17. export function partLayer(part, envelopeH, idx, total) {
  18. const role = part.role;
  19. const H = envelopeH;
  20. const band = {
  21. lid: [H * 0.7, H],
  22. back: [0, H * 0.7],
  23. shell: [0, H],
  24. button: [H * 0.3, H * 0.55],
  25. ring: [H * 0.2, H * 0.6],
  26. screen: [H * 0.72, H * 0.95],
  27. battery: [H * 0.4, H * 0.65],
  28. pcb: [H * 0.55, H * 0.72],
  29. brass: [H * 0.3, H * 0.7],
  30. clip: [H * 0.6, H * 0.7],
  31. }[role] || [H * 0.4, H * 0.6];
  32. return { z0: band[0], z1: band[1], order: idx, of: total };
  33. }
  34. export function buildParts(shape, related = []) {
  35. const list = related.length >= 9 ? related : DEFAULT_PARTS;
  36. const H = +(shape.defaults || shape).height || 10;
  37. return list.map((p, i) => ({ ...p, layer: partLayer(p, H, i, list.length) }));
  38. }
  39. /**
  40. * 爆炸位移:0 → 全部贴合;100% → 沿 Z 拉开。
  41. * external 外件(lid/back/ring/button)向上/下走,internal 按层序带状散开。
  42. * 返回 [{id, dz, isExternal}]
  43. */
  44. export function explodeOffsets(parts, pct = 0) {
  45. const t = Math.max(0, Math.min(100, pct)) / 100;
  46. const H = Math.max(...parts.map((p) => p.layer.z1), 1);
  47. const step = H * 0.55 * t + 6 * t;
  48. return parts.map((p) => {
  49. const role = p.role;
  50. const isExternal = ['lid', 'back', 'shell', 'ring', 'button'].includes(role);
  51. let dz = 0;
  52. if (role === 'lid') dz = step * 1.4;
  53. else if (role === 'back') dz = -step * 1.0;
  54. else if (role === 'ring') dz = -step * 1.5;
  55. else if (role === 'button') dz = step * 0.6;
  56. else {
  57. const mid = (p.layer.z0 + p.layer.z1) / 2;
  58. dz = (mid - H / 2) * t * 0.6;
  59. if (role === 'screen') dz += step * 0.9;
  60. if (role === 'battery') dz -= step * 0.4;
  61. }
  62. return { id: p.id, role, dz, isExternal };
  63. });
  64. }