Jelajahi Sumber

fix(box): 修正卡牌尺寸映射与 3D 厚度,3D 预览同步打孔

- 卡牌尺寸按 [长, 高] 读取,不再把高度写入深度/沿用上一个盒型的高度
- 卡牌 3D 采用纸张厚度,不再渲染成盒体
- 正反面 3D 几何按刀版孔位开孔,孔半径变化触发几何重建
- 盒型卡片补充名称 tooltip
wenhongquan 1 bulan lalu
induk
melakukan
983cdffe42
2 mengubah file dengan 74 tambahan dan 21 penghapusan
  1. 10 6
      assets/js/box.js
  2. 64 15
      assets/js/box3d.js

+ 10 - 6
assets/js/box.js

@@ -906,6 +906,7 @@
       !boxMine && h('div', { className: 'bx-box-grid' }, list.map(function (b) {
         return h('button', {
           key: b.no, className: 'bx-box-card' + (state.boxNo === b.no ? ' on' : ''),
+          title: b.name,
           onClick: function () { applyLibBox(b); }
         },
           h('span', { className: 'thumb', dangerouslySetInnerHTML: { __html: structThumbSvg(b.gen, b.no) } }),
@@ -959,12 +960,15 @@
     state.boxNo = entry.no;
     state.structure = entry.gen;
     if (entry.sizes && entry.sizes.length) {
-      state.w = entry.sizes[0][0];
-      state.d = entry.sizes[0][1];
-      state.h = entry.sizes[0][2] || state.h;
-    }
-    if (entry.gen === 'card') {
-      state.h = state.h;
+      const s = entry.sizes[0];
+      state.w = s[0];
+      if (entry.gen === 'card') {
+        // 卡牌尺寸为 [长, 高],无厚度维度
+        state.h = s[1];
+      } else {
+        state.d = s[1];
+        state.h = s[2] || state.h;
+      }
     }
     commit();
     UI.message.success('已切换到「' + entry.name + '」');

+ 64 - 15
assets/js/box3d.js

@@ -27,16 +27,29 @@
 
   function boxCenter(b) { return { x: b.x + b.w / 2, y: b.y + b.h / 2 }; }
 
-  function planeGeometry(b, poly) {
-    if (!poly) return new THREE.PlaneGeometry(b.w, b.h);
+  function planeGeometry(b, poly, holes) {
+    if (!poly && (!holes || !holes.length)) return new THREE.PlaneGeometry(b.w, b.h);
     const shape = new THREE.Shape();
-    poly.forEach(function (pt, i) {
-      const x = pt[0] - b.x - b.w / 2;
-      const y = -(pt[1] - b.y - b.h / 2);
-      if (i === 0) shape.moveTo(x, y);
-      else shape.lineTo(x, y);
+    if (poly) {
+      poly.forEach(function (pt, i) {
+        const x = pt[0] - b.x - b.w / 2;
+        const y = -(pt[1] - b.y - b.h / 2);
+        if (i === 0) shape.moveTo(x, y);
+        else shape.lineTo(x, y);
+      });
+      shape.closePath();
+    } else {
+      shape.moveTo(-b.w / 2, -b.h / 2);
+      shape.lineTo(b.w / 2, -b.h / 2);
+      shape.lineTo(b.w / 2, b.h / 2);
+      shape.lineTo(-b.w / 2, b.h / 2);
+      shape.closePath();
+    }
+    (holes || []).forEach(function (ho) {
+      const path = new THREE.Path();
+      path.absarc(ho.x - b.x - b.w / 2, -(ho.y - b.y - b.h / 2), ho.r, 0, Math.PI * 2, true);
+      shape.holes.push(path);
     });
-    shape.closePath();
     const geom = new THREE.ShapeGeometry(shape);
     const pos = geom.attributes.position;
     const uv = geom.attributes.uv;
@@ -47,6 +60,30 @@
     return geom;
   }
 
+  // 卡牌等刚性面:在贴图平面上按刀版孔位真实开孔(局部坐标,y 向上)
+  function rigidPlaneGeometry(w, h, holes) {
+    if (!holes || !holes.length) return new THREE.PlaneGeometry(w, h);
+    const shape = new THREE.Shape();
+    shape.moveTo(-w / 2, -h / 2);
+    shape.lineTo(w / 2, -h / 2);
+    shape.lineTo(w / 2, h / 2);
+    shape.lineTo(-w / 2, h / 2);
+    shape.closePath();
+    holes.forEach(function (ho) {
+      const path = new THREE.Path();
+      path.absarc(ho.x, ho.y, ho.r, 0, Math.PI * 2, true);
+      shape.holes.push(path);
+    });
+    const geom = new THREE.ShapeGeometry(shape);
+    const pos = geom.attributes.position;
+    const uv = geom.attributes.uv;
+    for (let i = 0; i < pos.count; i += 1) {
+      uv.setXY(i, (pos.getX(i) + w / 2) / Math.max(0.001, w), (pos.getY(i) + h / 2) / Math.max(0.001, h));
+    }
+    uv.needsUpdate = true;
+    return geom;
+  }
+
   const FOLD_PARTS = { carton: 1, rsc: 1, mailbox: 1, tuck: 1, straight: 1, autobottom: 1, mailer: 1, drawer: 1, bag: 1, sleeve: 1 };
 
   function panelLayer(p) {
@@ -348,7 +385,7 @@
     }
 
     function makePanelMesh(p, box) {
-      const geom = planeGeometry(box, p.poly);
+      const geom = planeGeometry(box, p.poly, p.holes);
       const face = p.face || p.paint || null;
       const fillOnly = !p.face && !!p.paint;
       const mat = face ? makeMaterial(face, { w: box.w, h: box.h }, state.side === 'inner', fillOnly) : cardboardMaterial();
@@ -499,12 +536,16 @@
       return true;
     }
 
-    function buildRigid() {
+    function buildRigid(panels) {
       const t = material.thickness || 0;
       const W = state.w + t * 2;
-      const D = state.d + t * 2;
+      // 卡牌为单张纸片,厚度取材质厚度,不沿用盒体的深度
+      const D = state.structure === 'card' ? t * 2 : state.d + t * 2;
       const H = state.h + t * 2;
       const inner = state.side === 'inner';
+      // 卡牌刀版在正面定义挂孔,3D 正反面同步开孔
+      const cardFront = (panels || []).find(function (p) { return p.face === 'front' && p.holes && p.holes.length; });
+      const cardBox = cardFront ? panelBox(cardFront) : null;
       const defs = [
         { face: 'front', w: W, h: H, pos: [0, 0, D / 2], rot: [0, 0, 0] },
         { face: 'back', w: W, h: H, pos: [0, 0, -D / 2], rot: [0, Math.PI, 0] },
@@ -514,7 +555,12 @@
         { face: 'bottom', w: W, h: D, pos: [0, -H / 2, 0], rot: [Math.PI / 2, 0, 0] }
       ];
       defs.forEach(function (def) {
-        const geom = new THREE.PlaneGeometry(def.w, def.h);
+        const holes = cardBox && (def.face === 'front' || def.face === 'back')
+          ? cardFront.holes.map(function (ho) {
+            return { x: ho.x - (cardBox.x + cardBox.w / 2), y: def.h / 2 - (ho.y - cardBox.y), r: ho.r };
+          })
+          : null;
+        const geom = rigidPlaneGeometry(def.w, def.h, holes);
         const mat = makeMaterial(def.face, { w: def.w, h: def.h }, inner);
         const mesh = new THREE.Mesh(geom, mat);
         mesh.position.set(def.pos[0], def.pos[1], def.pos[2]);
@@ -834,14 +880,17 @@
       resize();
       state = nextState;
       material = nextMaterial;
-      // 结构签名:盒型/尺寸/内外侧/面数变化才重建几何;否则仅刷新面纹理(增量,避免卡顿)
-      const sig = [state.structure, state.w, state.d, state.h, state.side, panels ? panels.length : 0].join('|');
+      // 结构签名:盒型/尺寸/内外侧/面数/孔位变化才重建几何;否则仅刷新面纹理(增量,避免卡顿)
+      const holeSig = (panels || []).map(function (p) {
+        return (p.holes || []).map(function (h) { return h.x.toFixed(2) + ',' + h.y.toFixed(2) + ',' + h.r; }).join(';');
+      }).join('|');
+      const sig = [state.structure, state.w, state.d, state.h, state.side, panels ? panels.length : 0, holeSig].join('|');
       if (sig !== lastGeomSig) {
         lastGeomSig = sig;
         clearRoot();
         const hasParts = panels && panels.length && FOLD_PARTS[state.structure];
         if (hasParts) buildFromDieline(panels);
-        if (!meshes.length) buildRigid();
+        if (!meshes.length) buildRigid(panels);
         applyFold(foldT);
         applyLines();
       }