// Split UB (single skinned primitive, one BLEND/double-sided material) into THREE
// primitives — opaque body+eyes, translucent side fins, and the translucent tail —
// sharing the SAME vertex data, skeleton, and animations. Classification is by
// skin weight: a vertex is "fin" when the majority of its weight rides non-Spine
// bones, and "tail" when the majority rides the Tail_Fork subtree (the forked
// caudal fin + its ray joints). Triangles vote 2-of-3. The tail gets its own
// material so it can render SINGLE-sided — a thin double-sided translucent fan
// shows its front and back faces at once and they bleed through each other while
// it sweeps, which reads as "two tails." All three primitives reference the same
// attribute accessors (no vertex duplication); only index buffer + material differ.
import { NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';

const [input, output] = process.argv.slice(2);
const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
const doc = await io.read(input);
const root = doc.getRoot();

const skin = root.listSkins()[0];
const joints = skin.listJoints();
const isFinJoint = joints.map((n) => !n.getName().startsWith('Spine'));
// Tail = the Tail_Fork_Top/Bottom subtree (fork bones + their fin-ray joints).
const tailSet = new Set();
const mark = (n) => { tailSet.add(n); n.listChildren().forEach(mark); };
joints.filter((n) => /^Tail_Fork_(Top|Bottom)$/.test(n.getName())).forEach(mark);
const isTailJoint = joints.map((n) => tailSet.has(n));

const mesh = root.listMeshes()[0];
const prim = mesh.listPrimitives()[0];
const pos = prim.getAttribute('POSITION');
const jA = prim.getAttribute('JOINTS_0');
const wA = prim.getAttribute('WEIGHTS_0');
const idx = prim.getIndices();

// Per-vertex classification: fin (non-Spine majority) and tail (Tail_Fork majority).
const vcount = pos.getCount();
const vIsFin = new Uint8Array(vcount);
const vIsTail = new Uint8Array(vcount);
const j = [0, 0, 0, 0], w = [0, 0, 0, 0];
for (let i = 0; i < vcount; i++) {
  jA.getElement(i, j);
  wA.getElement(i, w);
  let fin = 0, tail = 0, tot = 0;
  for (let k = 0; k < 4; k++) { tot += w[k]; if (isFinJoint[j[k]]) fin += w[k]; if (isTailJoint[j[k]]) tail += w[k]; }
  vIsFin[i] = tot > 0 && fin / tot >= 0.5 ? 1 : 0;
  vIsTail[i] = tot > 0 && tail / tot >= 0.5 ? 1 : 0;
}

// Per-triangle split: body unless ≥2 fin verts; within fin, tail if ≥2 tail verts.
const arr = idx.getArray();
const bodyIdx = [], finIdx = [], tailIdx = [];
for (let t = 0; t < arr.length; t += 3) {
  const a = arr[t], b = arr[t + 1], c = arr[t + 2];
  if (vIsFin[a] + vIsFin[b] + vIsFin[c] >= 2) {
    (vIsTail[a] + vIsTail[b] + vIsTail[c] >= 2 ? tailIdx : finIdx).push(a, b, c);
  } else {
    bodyIdx.push(a, b, c);
  }
}
const IndexArr = vcount > 65535 ? Uint32Array : Uint16Array;
const buffer = root.listBuffers()[0];
const mkIndex = (a) => doc.createAccessor().setType('SCALAR').setArray(new IndexArr(a)).setBuffer(buffer);

// Materials: original → opaque body; clones → translucent fins / tail.
const origMat = prim.getMaterial();
const bodyMat = origMat.setName('UB_Body').setAlphaMode('OPAQUE').setDoubleSided(false);
const finMat = origMat.clone().setName('UB_Fins').setAlphaMode('BLEND').setDoubleSided(true);
const tailMat = origMat.clone().setName('UB_Tail').setAlphaMode('BLEND').setDoubleSided(false);

// Body reuses the existing primitive; fins + tail get new primitives sharing attrs.
const addPrim = (indices, mat) => {
  const p = doc.createPrimitive().setIndices(mkIndex(indices)).setMaterial(mat);
  for (const sem of prim.listSemantics()) p.setAttribute(sem, prim.getAttribute(sem));
  mesh.addPrimitive(p);
};
prim.setIndices(mkIndex(bodyIdx)).setMaterial(bodyMat);
addPrim(finIdx, finMat);
addPrim(tailIdx, tailMat);

await io.write(output, doc);
console.log(`wrote ${output}: body ${bodyIdx.length / 3}, fins ${finIdx.length / 3}, tail ${tailIdx.length / 3} tris`);
