diff --git a/README.md b/README.md index 90fcc51..6bbd70a 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ town-kit/ Town & Market Kit: 27 pieces — street tiles ( tools/town-kit/ its reproducible build + e2e arcane-kit/ Arcane Study Kit: 7 wizard's-study hero props with LODs (crystal ball, alchemist's table, lectern, telescope, armillary, potions, rune rug; CC0, Meshy.ai) +avatars/ Adventurers: 9 rigged, animated characters (KayKit by Kay Lousberg, CC0) — the + app's player avatars, placeable as animated models (Idle loops) +tools/avatars/ their build: merge to one skinned mesh + 1 material, shared clips, thumbnails; + runtime/ = what core ships in static/avatars/ ``` Each `index.json` row: `{name, title, value | zip, attribution, copyright, license, source}` — diff --git a/avatars/Barbarian/glTF-Binary/barbarian.glb b/avatars/Barbarian/glTF-Binary/barbarian.glb new file mode 100644 index 0000000..dd75c8d Binary files /dev/null and b/avatars/Barbarian/glTF-Binary/barbarian.glb differ diff --git a/avatars/Barbarian/thumb.webp b/avatars/Barbarian/thumb.webp new file mode 100644 index 0000000..e44ebe1 Binary files /dev/null and b/avatars/Barbarian/thumb.webp differ diff --git a/avatars/HoodedRogue/glTF-Binary/rogue-hooded.glb b/avatars/HoodedRogue/glTF-Binary/rogue-hooded.glb new file mode 100644 index 0000000..60251c7 Binary files /dev/null and b/avatars/HoodedRogue/glTF-Binary/rogue-hooded.glb differ diff --git a/avatars/HoodedRogue/thumb.webp b/avatars/HoodedRogue/thumb.webp new file mode 100644 index 0000000..302a199 Binary files /dev/null and b/avatars/HoodedRogue/thumb.webp differ diff --git a/avatars/Knight/glTF-Binary/knight.glb b/avatars/Knight/glTF-Binary/knight.glb new file mode 100644 index 0000000..522bd4b Binary files /dev/null and b/avatars/Knight/glTF-Binary/knight.glb differ diff --git a/avatars/Knight/thumb.webp b/avatars/Knight/thumb.webp new file mode 100644 index 0000000..de78e9a Binary files /dev/null and b/avatars/Knight/thumb.webp differ diff --git a/avatars/Mage/glTF-Binary/mage.glb b/avatars/Mage/glTF-Binary/mage.glb new file mode 100644 index 0000000..03530c1 Binary files /dev/null and b/avatars/Mage/glTF-Binary/mage.glb differ diff --git a/avatars/Mage/thumb.webp b/avatars/Mage/thumb.webp new file mode 100644 index 0000000..2b88c7c Binary files /dev/null and b/avatars/Mage/thumb.webp differ diff --git a/avatars/Rogue/glTF-Binary/rogue.glb b/avatars/Rogue/glTF-Binary/rogue.glb new file mode 100644 index 0000000..2ee270f Binary files /dev/null and b/avatars/Rogue/glTF-Binary/rogue.glb differ diff --git a/avatars/Rogue/thumb.webp b/avatars/Rogue/thumb.webp new file mode 100644 index 0000000..227d234 Binary files /dev/null and b/avatars/Rogue/thumb.webp differ diff --git a/avatars/Skeleton/glTF-Binary/skeleton-minion.glb b/avatars/Skeleton/glTF-Binary/skeleton-minion.glb new file mode 100644 index 0000000..9e7265a Binary files /dev/null and b/avatars/Skeleton/glTF-Binary/skeleton-minion.glb differ diff --git a/avatars/Skeleton/thumb.webp b/avatars/Skeleton/thumb.webp new file mode 100644 index 0000000..160944a Binary files /dev/null and b/avatars/Skeleton/thumb.webp differ diff --git a/avatars/SkeletonMage/glTF-Binary/skeleton-mage.glb b/avatars/SkeletonMage/glTF-Binary/skeleton-mage.glb new file mode 100644 index 0000000..cc188ef Binary files /dev/null and b/avatars/SkeletonMage/glTF-Binary/skeleton-mage.glb differ diff --git a/avatars/SkeletonMage/thumb.webp b/avatars/SkeletonMage/thumb.webp new file mode 100644 index 0000000..33332ad Binary files /dev/null and b/avatars/SkeletonMage/thumb.webp differ diff --git a/avatars/SkeletonRogue/glTF-Binary/skeleton-rogue.glb b/avatars/SkeletonRogue/glTF-Binary/skeleton-rogue.glb new file mode 100644 index 0000000..5ed13bb Binary files /dev/null and b/avatars/SkeletonRogue/glTF-Binary/skeleton-rogue.glb differ diff --git a/avatars/SkeletonRogue/thumb.webp b/avatars/SkeletonRogue/thumb.webp new file mode 100644 index 0000000..55153bf Binary files /dev/null and b/avatars/SkeletonRogue/thumb.webp differ diff --git a/avatars/SkeletonWarrior/glTF-Binary/skeleton-warrior.glb b/avatars/SkeletonWarrior/glTF-Binary/skeleton-warrior.glb new file mode 100644 index 0000000..e078ec1 Binary files /dev/null and b/avatars/SkeletonWarrior/glTF-Binary/skeleton-warrior.glb differ diff --git a/avatars/SkeletonWarrior/thumb.webp b/avatars/SkeletonWarrior/thumb.webp new file mode 100644 index 0000000..d67f8b9 Binary files /dev/null and b/avatars/SkeletonWarrior/thumb.webp differ diff --git a/avatars/attribution.html b/avatars/attribution.html new file mode 100644 index 0000000..8ce2ab2 --- /dev/null +++ b/avatars/attribution.html @@ -0,0 +1,21 @@ +

Attribution Statement

+ +

The Adventurers pack holds nine rigged, animated characters by Kay Lousberg (KayKit Character Pack: + Adventurers 1.0 and Skeletons 1.0, kaylousberg.com), rebuilt for + theprototype: weapons removed, headgear baked into the skin, every part merged into one mesh on one material, + the gradient atlas reduced to 256², and ten of the clips kept (tools/avatars/build.mjs). The same characters are + the app's player avatars.

+

Kay Lousberg dedicated the source packs to the public domain under + CC0 1.0, and this pack keeps that license: use it + for anything, no credit required (crediting Kay Lousberg is appreciated by the author).

+
+

Details:

+ +
diff --git a/avatars/default.json b/avatars/default.json new file mode 100644 index 0000000..0b88a7f --- /dev/null +++ b/avatars/default.json @@ -0,0 +1,128 @@ +[ + { + "name": "Knight", + "screenshot": "thumb.webp", + "label": "Knight (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "knight.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "Mage", + "screenshot": "thumb.webp", + "label": "Mage (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "mage.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "Rogue", + "screenshot": "thumb.webp", + "label": "Rogue (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "rogue.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "HoodedRogue", + "screenshot": "thumb.webp", + "label": "Hooded rogue (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "rogue-hooded.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "Barbarian", + "screenshot": "thumb.webp", + "label": "Barbarian (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "barbarian.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "Skeleton", + "screenshot": "thumb.webp", + "label": "Skeleton (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "skeleton-minion.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "SkeletonWarrior", + "screenshot": "thumb.webp", + "label": "Skeleton warrior (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "skeleton-warrior.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "SkeletonMage", + "screenshot": "thumb.webp", + "label": "Skeleton mage (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "skeleton-mage.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + }, + { + "name": "SkeletonRogue", + "screenshot": "thumb.webp", + "label": "Skeleton rogue (rigged, animated: Idle loops; clips Idle, Walking_A, Walking_Backwards, Running_A, Running_Strafe_Left, Running_Strafe_Right, Jump_Idle, Cheer, Interact, Sit_Floor_Idle)", + "variants": { + "glTF-Binary": "skeleton-rogue.glb" + }, + "behavior": { + "type": "loop", + "clip": "Idle", + "trigger": "click", + "autoplay": true + } + } +] diff --git a/index.json b/index.json index c5d4baa..d7a191f 100644 --- a/index.json +++ b/index.json @@ -113,5 +113,14 @@ "license": "CC0-1.0", "source": "https://github.com/theprototype-app/packs/tree/main/arcane-kit", "cover": "arcane-kit/cover.webp" + }, + { + "name": "avatars", + "title": "Adventurers (rigged characters)", + "value": "avatars/default.json", + "attribution": "avatars/attribution.html", + "copyright": "(c) Kay Lousberg (KayKit)", + "license": "CC0-1.0", + "source": "https://github.com/theprototype-app/packs/tree/main/avatars" } ] diff --git a/tools/avatars/build.mjs b/tools/avatars/build.mjs new file mode 100644 index 0000000..cb3f3c1 --- /dev/null +++ b/tools/avatars/build.mjs @@ -0,0 +1,429 @@ +// tools/avatars/build.mjs — the avatar characters (roadmap 36, plan 76) from KayKit's CC0 character packs. +// +// node tools/avatars/build.mjs [--src ] [--core ] +// node tools/avatars/build.mjs --thumbs (each pack row's thumb.webp, posed at Idle 0.4 s — renders headless) +// +// Sources: KayKit Adventurers 1.0 + Skeletons 1.0 (Kay Lousberg, CC0), fetched at PINNED commits into +// ~/.cache/tp-avatars (or --src). Every character shares one 41-joint rig, so ONE clip file animates all. +// +// Per character (the 36-avatars budget: ≤ 10k tris, 1 material, ≤ 2 draw calls with the head/hat): +// - weapons/props on the hand slots are dropped; +// - rigid headgear and capes (children of `head` / `chest`) are BAKED into the skin, weighted 100% to the +// bone they hang from, so they bend with it and cost no extra draw; +// - every part is merged into ONE skinned primitive on ONE material (the skeletons' second "Glow" material +// only colours the eyes, whose UVs already sit on the atlas's yellow cell — they join the main one); +// - the 1024² gradient atlas → 256² (8×4 cells of vertical gradients: nothing is lost, 16× less GPU memory); +// - no animations (clips.glb carries them once). +// +// Outputs: +// - avatars//glTF-Binary/.glb + avatars/default.json the Explorer pack row: the merged mesh WITH the clips (a placeable, +// animated character; behavior loop Idle) +// - tools/avatars/runtime/.glb mesh only — what core ships in static/avatars/ +// - tools/avatars/runtime/clips.glb the rig + the locomotion/emote clips, deform-bone channels only +// - tools/avatars/runtime/avatars.json the catalog core reads (tris, height, eye height, outfit cells) +// --core also copies runtime/* into /static/avatars/. +import fs from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; +import { execFileSync } from 'node:child_process'; +import { pathToFileURL } from 'node:url'; +import { load, makeIO, REPO } from '../kit-build/lib/deps.mjs'; + +const { Document } = await load('@gltf-transform/core'); +const { prune, dedup } = await load('@gltf-transform/functions'); +const THREE = await load('three'); +const sharp = (await load('sharp')).default; + +const args = process.argv.slice(2); +const opt = (name) => { + const i = args.indexOf(name); + return i >= 0 ? args[i + 1] : undefined; +}; +const SRC = opt('--src') || path.join(os.homedir(), '.cache/tp-avatars'); +const CORE = opt('--core'); +const OUT = path.join(REPO, 'avatars'); +const RUNTIME = path.join(REPO, 'tools/avatars/runtime'); + +const PACKS = { + adventurers: { + repo: 'KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0', + sha: '672074b73ba276876a19e8816ecdc5241817ab47', + dir: 'addons/kaykit_character_pack_adventures/Characters/gltf' + }, + skeletons: { + repo: 'KayKit-Game-Assets/KayKit-Character-Pack-Skeletons-1.0', + sha: '15b62b9bad122f72926c10fb14d622c73819fa54', + dir: 'addons/kaykit_character_pack_skeletons/Characters/gltf' + } +}; + +// id, display name, source pack + file. ORDER = the picker's order. +export const CHARACTERS = [ + { id: 'knight', name: 'Knight', pack: 'adventurers', file: 'Knight.glb' }, + { id: 'mage', name: 'Mage', pack: 'adventurers', file: 'Mage.glb' }, + { id: 'rogue', name: 'Rogue', pack: 'adventurers', file: 'Rogue.glb' }, + { id: 'rogue-hooded', name: 'Hooded rogue', pack: 'adventurers', file: 'Rogue_Hooded.glb' }, + { id: 'barbarian', name: 'Barbarian', pack: 'adventurers', file: 'Barbarian.glb' }, + { id: 'skeleton-minion', name: 'Skeleton', pack: 'skeletons', file: 'Skeleton_Minion.glb' }, + { id: 'skeleton-warrior', name: 'Skeleton warrior', pack: 'skeletons', file: 'Skeleton_Warrior.glb' }, + { id: 'skeleton-mage', name: 'Skeleton mage', pack: 'skeletons', file: 'Skeleton_Mage.glb' }, + { id: 'skeleton-rogue', name: 'Skeleton rogue', pack: 'skeletons', file: 'Skeleton_Rogue.glb' } +]; + +// what the avatar runtime plays (locomotion blend + a few emotes); everything else stays in the source packs +export const CLIPS = [ + 'Idle', + 'Walking_A', + 'Walking_Backwards', + 'Running_A', + 'Running_Strafe_Left', + 'Running_Strafe_Right', + 'Jump_Idle', + 'Cheer', + 'Interact', + 'Sit_Floor_Idle' +]; + +// bones the skin actually deforms with — the clip channels on the IK/control helpers are dropped +const DEFORM = new Set([ + 'root', 'hips', 'spine', 'chest', 'head', + 'upperarm.l', 'lowerarm.l', 'wrist.l', 'hand.l', 'upperarm.r', 'lowerarm.r', 'wrist.r', 'hand.r', + 'upperleg.l', 'lowerleg.l', 'foot.l', 'toes.l', 'upperleg.r', 'lowerleg.r', 'foot.r', 'toes.r' +]); +// rigid parts kept (baked into the skin) when they hang from one of these; the rest (hand slots) are props +const KEEP_RIGID_UNDER = new Set(['head', 'chest']); +const ATLAS = 256; +const GRID = { cols: 8, rows: 4 }; // the KayKit gradient atlas layout + +function fetchSources() { + for (const c of CHARACTERS) { + const p = PACKS[c.pack]; + const dir = path.join(SRC, c.pack); + fs.mkdirSync(dir, { recursive: true }); + const files = [c.file]; + for (const f of files) { + const dest = path.join(dir, f); + if (fs.existsSync(dest)) continue; + const url = `https://raw.githubusercontent.com/${p.repo}/${p.sha}/${p.dir}/${f}`; + console.error('fetch ' + url); + execFileSync('curl', ['-sSfL', '-o', dest, url]); + } + } +} + +/** world matrix of a gltf-transform node at its rest TRS @param {any} node */ +function worldOf(node) { + const m = new THREE.Matrix4().fromArray(node.getMatrix()); + let p = node.getParentNode(); + while (p) { + m.premultiply(new THREE.Matrix4().fromArray(p.getMatrix())); + p = p.getParentNode(); + } + return m; +} + +/** names of a node's ancestors (nearest first) @param {any} node */ +function ancestors(node) { + const out = []; + for (let p = node.getParentNode(); p; p = p.getParentNode()) out.push(p); + return out; +} + +/** dispose a sampler AND its accessors: a disposed animation's samplers keep their accessors alive, and + * prune then keeps the bytes (gltf-transform counts the orphan sampler as a parent) @param {any} s */ +function disposeSampler(s) { + if (!s) return; + for (const a of [s.getInput(), s.getOutput()]) if (a && a.listParents().every((p) => p === s || p.propertyType === 'Root')) a.dispose(); + s.dispose(); +} +/** @param {any} anim */ +function disposeAnimation(anim) { + for (const ch of anim.listChannels()) ch.dispose(); + for (const s of anim.listSamplers()) disposeSampler(s); + anim.dispose(); +} + +/** read an accessor into a plain array of tuples @param {any} acc */ +function tuples(acc) { + const n = acc.getCount(); + const out = []; + for (let i = 0; i < n; i++) out.push(acc.getElement(i, [])); + return out; +} + +/** + * Merge every kept part of one character into a single skinned primitive. + * @returns {{doc: any, tris: number, cellsByPart: Record>}} + */ +async function buildCharacter(file) { + const io = makeIO(); + const doc = await io.read(file); + const root = doc.getRoot(); + const skin = root.listSkins()[0]; + const joints = skin.listJoints(); + const jointIndex = new Map(joints.map((j, i) => [j, i])); + const ibm = tuples(skin.getInverseBindMatrices()).map((a) => new THREE.Matrix4().fromArray(a)); + const material = root.listMaterials().find((m) => m.getBaseColorTexture()) ?? root.listMaterials()[0]; + + const pos = [], nrm = [], uv = [], jnt = [], wgt = [], idx = []; + /** @type {Record>} */ + const cellsByPart = {}; + const dropped = []; + const meshNodes = root.listNodes().filter((n) => n.getMesh()); + for (const node of meshNodes) { + const skinned = !!node.getSkin(); + const anc = ancestors(node); + const bone = skinned ? null : anc.find((a) => jointIndex.has(a)); + if (!skinned && (!bone || !KEEP_RIGID_UNDER.has(bone.getName()))) { + dropped.push(node.getName()); + continue; + } + // rigid: mesh space -> model (bind) space = inverse(IBM[bone]) * inverse(boneWorld) * meshWorld + let toModel = null; + if (!skinned) { + const bi = jointIndex.get(bone); + const bindWorld = ibm[bi].clone().invert(); + const rel = worldOf(bone).invert().multiply(worldOf(node)); + toModel = bindWorld.multiply(rel); + } + const normalMat = toModel ? new THREE.Matrix3().getNormalMatrix(toModel) : null; + const part = node.getName(); + const cells = (cellsByPart[part] = {}); + for (const prim of node.getMesh().listPrimitives()) { + const base = pos.length; + const P = tuples(prim.getAttribute('POSITION')); + const N = prim.getAttribute('NORMAL') ? tuples(prim.getAttribute('NORMAL')) : P.map(() => [0, 1, 0]); + const T = prim.getAttribute('TEXCOORD_0') ? tuples(prim.getAttribute('TEXCOORD_0')) : P.map(() => [0.5, 0.5]); + const J = skinned ? tuples(prim.getAttribute('JOINTS_0')) : null; + const W = skinned ? tuples(prim.getAttribute('WEIGHTS_0')) : null; + // a skinned part's JOINTS_0 indexes ITS skin; all parts share the one skin (asserted) + if (skinned && node.getSkin() !== skin) throw new Error(`${part}: a second skin`); + const v = new THREE.Vector3(); + for (let i = 0; i < P.length; i++) { + v.fromArray(P[i]); + if (toModel) v.applyMatrix4(toModel); + pos.push([v.x, v.y, v.z]); + v.fromArray(N[i]); + if (normalMat) v.applyMatrix3(normalMat).normalize(); + nrm.push([v.x, v.y, v.z]); + uv.push(T[i]); + if (skinned) { + const w = W[i]; + const s = w[0] + w[1] + w[2] + w[3] || 1; + jnt.push(J[i]); + wgt.push(w.map((x) => x / s)); + } else { + jnt.push([jointIndex.get(bone), 0, 0, 0]); + wgt.push([1, 0, 0, 0]); + } + const cu = Math.min(GRID.cols - 1, Math.max(0, Math.floor(T[i][0] * GRID.cols))); + const cv = Math.min(GRID.rows - 1, Math.max(0, Math.floor(T[i][1] * GRID.rows))); + const cell = cv * GRID.cols + cu; + cells[cell] = (cells[cell] ?? 0) + 1; + } + const I = prim.getIndices() ? Array.from(prim.getIndices().getArray()) : P.map((_, i) => i); + for (const i of I) idx.push(base + i); + } + } + + // the new single mesh + const buffer = root.listBuffers()[0]; + const flat = (rows, Type) => Type.from(rows.flat()); + const acc = (type, array) => doc.createAccessor().setType(type).setArray(array).setBuffer(buffer); + const prim = doc + .createPrimitive() + .setAttribute('POSITION', acc('VEC3', flat(pos, Float32Array))) + .setAttribute('NORMAL', acc('VEC3', flat(nrm, Float32Array))) + .setAttribute('TEXCOORD_0', acc('VEC2', flat(uv, Float32Array))) + .setAttribute('JOINTS_0', acc('VEC4', flat(jnt, Uint16Array))) + .setAttribute('WEIGHTS_0', acc('VEC4', flat(wgt, Float32Array))) + .setIndices(acc('SCALAR', pos.length > 65535 ? Uint32Array.from(idx) : Uint16Array.from(idx))) + .setMaterial(material); + const mesh = doc.createMesh('Avatar').addPrimitive(prim); + const rig = root.listScenes()[0].listChildren()[0]; + const avatarNode = doc.createNode('Avatar').setMesh(mesh).setSkin(skin); + rig.addChild(avatarNode); + for (const node of meshNodes) { + const m = node.getMesh(); + node.dispose(); + if (m && !m.listParents().some((p) => p.propertyType === "Node")) for (const p of m.listPrimitives()) { for (const a of [...p.listAttributes(), p.getIndices()]) a?.dispose(); p.dispose(); } + } + for (const m of root.listMaterials()) if (m !== material) m.dispose(); + material.setName('avatar'); + for (const a of root.listAnimations()) disposeAnimation(a); + + // atlas → 256² PNG + const tex = material.getBaseColorTexture(); + if (tex) { + const small = await sharp(Buffer.from(tex.getImage())).resize(ATLAS, ATLAS, { kernel: 'lanczos3' }).png({ compressionLevel: 9 }).toBuffer(); + tex.setImage(new Uint8Array(small)).setMimeType('image/png'); + } + await doc.transform(prune(), dedup()); + return { doc, tris: idx.length / 3, cellsByPart, dropped, bounds: boundsOf(pos), headY: worldOf(joints.find((j) => j.getName() === 'head')).elements[13] }; +} + +function boundsOf(pos) { + const min = [Infinity, Infinity, Infinity], max = [-Infinity, -Infinity, -Infinity]; + for (const p of pos) for (let k = 0; k < 3; k++) { + min[k] = Math.min(min[k], p[k]); + max[k] = Math.max(max[k], p[k]); + } + return { min, max }; +} + +/** the clip file: the first character's rig + the chosen clips, deform channels only */ +async function buildClips(file) { + const io = makeIO(); + const doc = await io.read(file); + const root = doc.getRoot(); + for (const n of root.listNodes()) if (n.getMesh()) n.dispose(); + for (const s of root.listSkins()) s.dispose(); + const kept = []; + for (const a of root.listAnimations()) { + if (!CLIPS.includes(a.getName())) { + disposeAnimation(a); + continue; + } + for (const ch of a.listChannels()) { + const t = ch.getTargetNode(); + if (!t || !DEFORM.has(t.getName()) || ch.getTargetPath() === 'scale') { + const s = ch.getSampler(); + ch.dispose(); + if (s && !s.listParents().some((p) => p.propertyType === 'AnimationChannel')) disposeSampler(s); + } + } + kept.push(a.getName()); + } + const missing = CLIPS.filter((c) => !kept.includes(c)); + if (missing.length) throw new Error('clips missing: ' + missing.join(', ')); + await doc.transform(prune({ keepLeaves: true }), dedup()); + return doc; +} + +/** the character with the clips merged in (the Explorer pack's animated model) */ +async function withClips(charDoc, clipFile) { + const io = makeIO(); + // round-trip through bytes so the two documents never share property instances + const doc = await io.readBinary(await io.writeBinary(charDoc)); + const clips = await buildClips(clipFile); + const byName = new Map(doc.getRoot().listNodes().map((n) => [n.getName(), n])); + const buffer = doc.getRoot().listBuffers()[0]; + for (const a of clips.getRoot().listAnimations()) { + const anim = doc.createAnimation(a.getName()); + for (const ch of a.listChannels()) { + const target = byName.get(ch.getTargetNode().getName()); + if (!target) continue; + const s = ch.getSampler(); + const copy = (accessor) => doc.createAccessor().setType(accessor.getType()).setArray(accessor.getArray().slice()).setBuffer(buffer); + const sampler = doc.createAnimationSampler().setInput(copy(s.getInput())).setOutput(copy(s.getOutput())).setInterpolation(s.getInterpolation()); + anim.addSampler(sampler).addChannel(doc.createAnimationChannel().setTargetNode(target).setTargetPath(ch.getTargetPath()).setSampler(sampler)); + } + } + await doc.transform(dedup()); + return doc; +} + +/** a copy of a character posed at `t` seconds of one clip (the thumbnailer draws the bind pose otherwise) */ +async function posedCopy(file, clip = 'Idle', t = 0.4) { + const io = makeIO(); + const doc = await io.read(file); + const anim = doc.getRoot().listAnimations().find((a) => a.getName() === clip); + if (anim) + for (const ch of anim.listChannels()) { + const node = ch.getTargetNode(); + const s = ch.getSampler(); + if (!node || !s) continue; + const times = s.getInput().getArray(); + let k = 0; + while (k + 1 < times.length && times[k + 1] <= t) k++; + const v = s.getOutput().getElement(k, []); + const path_ = ch.getTargetPath(); + if (path_ === 'rotation') node.setRotation(v); + else if (path_ === 'translation') node.setTranslation(v); + else if (path_ === 'scale') node.setScale(v); + } + for (const a of doc.getRoot().listAnimations()) disposeAnimation(a); + return io.writeBinary(doc); +} + +async function thumbs() { + const { renderThumbs } = await import(pathToFileURL(path.join(REPO, 'tools/meshy/lib/thumb.js')).href); + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tp-avatar-thumbs-')); + const items = []; + for (const row of JSON.parse(fs.readFileSync(path.join(OUT, 'default.json'), 'utf8'))) { + const glb = path.join(tmp, row.name + '.glb'); + fs.writeFileSync(glb, await posedCopy(path.join(OUT, row.name, 'glTF-Binary', row.variants['glTF-Binary']))); + items.push({ glb, out: path.join(OUT, row.name, row.screenshot) }); + } + await renderThumbs(items, { size: 512, yaw: 25 }); + fs.rmSync(tmp, { recursive: true, force: true }); + console.error('thumbs: ' + items.length); +} + +async function main() { + if (args.includes('--thumbs')) return thumbs(); + fetchSources(); + fs.mkdirSync(RUNTIME, { recursive: true }); + const io = makeIO(); + const catalog = []; + /** @type {any[]} the Explorer pack's model list */ + const rows = []; + const clipSrc = path.join(SRC, 'adventurers', 'Knight.glb'); + for (const c of CHARACTERS) { + const file = path.join(SRC, c.pack, c.file); + const built = await buildCharacter(file); + const { doc, tris, cellsByPart, dropped, bounds, headY } = built; + const runtimeFile = path.join(RUNTIME, c.id + '.glb'); + await io.write(runtimeFile, doc); + const folder = c.name.replace(/(^|[^A-Za-z0-9]+)([a-z])/g, (_, __, ch) => ch.toUpperCase()).replace(/[^A-Za-z0-9]+/g, ''); + fs.mkdirSync(path.join(OUT, folder, 'glTF-Binary'), { recursive: true }); + await io.write(path.join(OUT, folder, 'glTF-Binary', c.id + '.glb'), await withClips(doc, clipSrc)); + rows.push({ + name: folder, + screenshot: 'thumb.webp', + label: c.name + ' (rigged, animated: Idle loops; clips ' + CLIPS.join(', ') + ')', + variants: { 'glTF-Binary': c.id + '.glb' }, + behavior: { type: 'loop', clip: 'Idle', trigger: 'click', autoplay: true } + }); + // the outfit = the atlas cells the body/arms/legs/cape use most that the HEAD part does not + // (the head carries skin + face, which an outfit colour must never repaint) + const sum = (re) => { + /** @type {Record} */ + const t = {}; + for (const [part, cells] of Object.entries(cellsByPart)) if (re.test(part)) for (const [k, n] of Object.entries(cells)) t[+k] = (t[+k] ?? 0) + n; + return t; + }; + const head = sum(/Head|Eyes|Jaw/); + const outfit = sum(/Body|Arm|Leg|Cape|Cloak/); + const outfitCells = Object.entries(outfit) + .filter(([k]) => !head[+k]) + .sort((a, b) => b[1] - a[1]) + .slice(0, 3) + .map(([k]) => +k); + const height = bounds.max[1]; + catalog.push({ + id: c.id, + name: c.name, + file: c.id + '.glb', + source: PACKS[c.pack].repo, + tris, + height: +height.toFixed(3), + headBoneY: +headY.toFixed(3), + outfitCells + }); + console.error(`${c.id}: ${tris} tris, ${(fs.statSync(runtimeFile).size / 1024).toFixed(0)} KiB, height ${height.toFixed(2)}, dropped ${dropped.join(',') || '-'}, outfit cells ${outfitCells}`); + } + fs.writeFileSync(path.join(OUT, 'default.json'), JSON.stringify(rows, null, '\t') + '\n'); + const clips = await buildClips(clipSrc); + await io.write(path.join(RUNTIME, 'clips.glb'), clips); + fs.writeFileSync(path.join(RUNTIME, 'avatars.json'), JSON.stringify({ version: 1, atlasGrid: GRID, clips: CLIPS, characters: catalog }, null, '\t') + '\n'); + console.error(`clips.glb: ${(fs.statSync(path.join(RUNTIME, 'clips.glb')).size / 1024).toFixed(0)} KiB`); + if (CORE) { + const dest = path.join(CORE, 'static/avatars'); + fs.mkdirSync(dest, { recursive: true }); + for (const f of fs.readdirSync(RUNTIME)) fs.copyFileSync(path.join(RUNTIME, f), path.join(dest, f)); + console.error('copied to ' + dest); + } +} + +await main(); diff --git a/tools/avatars/runtime/avatars.json b/tools/avatars/runtime/avatars.json new file mode 100644 index 0000000..0129071 --- /dev/null +++ b/tools/avatars/runtime/avatars.json @@ -0,0 +1,147 @@ +{ + "version": 1, + "atlasGrid": { + "cols": 8, + "rows": 4 + }, + "clips": [ + "Idle", + "Walking_A", + "Walking_Backwards", + "Running_A", + "Running_Strafe_Left", + "Running_Strafe_Right", + "Jump_Idle", + "Cheer", + "Interact", + "Sit_Floor_Idle" + ], + "characters": [ + { + "id": "knight", + "name": "Knight", + "file": "knight.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0", + "tris": 4712, + "height": 2.467, + "headBoneY": 1.241, + "outfitCells": [ + 3, + 7, + 6 + ] + }, + { + "id": "mage", + "name": "Mage", + "file": "mage.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0", + "tris": 4509, + "height": 2.716, + "headBoneY": 1.241, + "outfitCells": [ + 8, + 19, + 4 + ] + }, + { + "id": "rogue", + "name": "Rogue", + "file": "rogue.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0", + "tris": 4347, + "height": 2.187, + "headBoneY": 1.241, + "outfitCells": [ + 8, + 9, + 19 + ] + }, + { + "id": "rogue-hooded", + "name": "Hooded rogue", + "file": "rogue-hooded.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0", + "tris": 4005, + "height": 2.251, + "headBoneY": 1.241, + "outfitCells": [ + 8, + 19, + 5 + ] + }, + { + "id": "barbarian", + "name": "Barbarian", + "file": "barbarian.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Adventures-1.0", + "tris": 4777, + "height": 2.398, + "headBoneY": 1.241, + "outfitCells": [ + 6, + 10, + 19 + ] + }, + { + "id": "skeleton-minion", + "name": "Skeleton", + "file": "skeleton-minion.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Skeletons-1.0", + "tris": 5288, + "height": 2.166, + "headBoneY": 1.241, + "outfitCells": [ + 6, + 7, + 5 + ] + }, + { + "id": "skeleton-warrior", + "name": "Skeleton warrior", + "file": "skeleton-warrior.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Skeletons-1.0", + "tris": 5934, + "height": 2.59, + "headBoneY": 1.241, + "outfitCells": [ + 3, + 6, + 18 + ] + }, + { + "id": "skeleton-mage", + "name": "Skeleton mage", + "file": "skeleton-mage.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Skeletons-1.0", + "tris": 4588, + "height": 2.63, + "headBoneY": 1.241, + "outfitCells": [ + 18, + 7, + 3 + ] + }, + { + "id": "skeleton-rogue", + "name": "Skeleton rogue", + "file": "skeleton-rogue.glb", + "source": "KayKit-Game-Assets/KayKit-Character-Pack-Skeletons-1.0", + "tris": 5278, + "height": 2.308, + "headBoneY": 1.241, + "outfitCells": [ + 3, + 7, + 6 + ] + } + ] +} diff --git a/tools/avatars/runtime/barbarian.glb b/tools/avatars/runtime/barbarian.glb new file mode 100644 index 0000000..7986108 Binary files /dev/null and b/tools/avatars/runtime/barbarian.glb differ diff --git a/tools/avatars/runtime/clips.glb b/tools/avatars/runtime/clips.glb new file mode 100644 index 0000000..25a08c6 Binary files /dev/null and b/tools/avatars/runtime/clips.glb differ diff --git a/tools/avatars/runtime/knight.glb b/tools/avatars/runtime/knight.glb new file mode 100644 index 0000000..e39d89e Binary files /dev/null and b/tools/avatars/runtime/knight.glb differ diff --git a/tools/avatars/runtime/mage.glb b/tools/avatars/runtime/mage.glb new file mode 100644 index 0000000..5d550f6 Binary files /dev/null and b/tools/avatars/runtime/mage.glb differ diff --git a/tools/avatars/runtime/rogue-hooded.glb b/tools/avatars/runtime/rogue-hooded.glb new file mode 100644 index 0000000..17d3006 Binary files /dev/null and b/tools/avatars/runtime/rogue-hooded.glb differ diff --git a/tools/avatars/runtime/rogue.glb b/tools/avatars/runtime/rogue.glb new file mode 100644 index 0000000..0b7ed94 Binary files /dev/null and b/tools/avatars/runtime/rogue.glb differ diff --git a/tools/avatars/runtime/skeleton-mage.glb b/tools/avatars/runtime/skeleton-mage.glb new file mode 100644 index 0000000..fd411fd Binary files /dev/null and b/tools/avatars/runtime/skeleton-mage.glb differ diff --git a/tools/avatars/runtime/skeleton-minion.glb b/tools/avatars/runtime/skeleton-minion.glb new file mode 100644 index 0000000..2128989 Binary files /dev/null and b/tools/avatars/runtime/skeleton-minion.glb differ diff --git a/tools/avatars/runtime/skeleton-rogue.glb b/tools/avatars/runtime/skeleton-rogue.glb new file mode 100644 index 0000000..c19add1 Binary files /dev/null and b/tools/avatars/runtime/skeleton-rogue.glb differ diff --git a/tools/avatars/runtime/skeleton-warrior.glb b/tools/avatars/runtime/skeleton-warrior.glb new file mode 100644 index 0000000..057619d Binary files /dev/null and b/tools/avatars/runtime/skeleton-warrior.glb differ diff --git a/tools/kit-build/allow.json b/tools/kit-build/allow.json index cf650d0..ffa2196 100644 --- a/tools/kit-build/allow.json +++ b/tools/kit-build/allow.json @@ -97,6 +97,12 @@ "check": "flicker", "file": "nature-kit/PondSet/glTF-Binary/PondSet.glb", "reason": "render probe 404 px (> the 400 px recording limit) — the same 404 px 33-pack-fix-lod measured; none of defight's candidates flickers clearly less (best 403 px), and the roadmap-33 in-app scan passed it under the patch limit. The kitbash's reeds and pond edge, not a fixable coplanar layer; re-judge if the pond is rebuilt" + }, + { + "pack": "avatars", + "item": "*", + "check": "lods", + "reason": "skinned characters: a decimated level changes the skin weights and the rig, and a scene holds a handful of them (8 peers = ~40k tris); core's runtime LOD skips skinned meshes anyway" } ] } diff --git a/tools/kit-build/packs.json b/tools/kit-build/packs.json index a132daf..3bab59d 100644 --- a/tools/kit-build/packs.json +++ b/tools/kit-build/packs.json @@ -385,6 +385,13 @@ "category": "legacy", "pivot": "any", "about": "upstream-fetch: every row is an absolute URL into KhronosGroup/glTF-Sample-Assets, no bytes here" + }, + "avatars": { + "textureCap": 256, + "emissive": "none", + "category": "hero", + "pivot": "bottom-center", + "about": "rigged KayKit characters (tools/avatars); one skinned mesh each, the app's avatars" } } }