Add anatomical character customization at initial creation
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { build } from 'esbuild';
|
||||
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { resolve } from 'node:path';
|
||||
import * as T from 'three';
|
||||
|
||||
// Geometry-only validation; does not launch browsers or change game state.
|
||||
const temporary = resolve('.local/humanoid-check.mjs');
|
||||
await mkdir('.local', { recursive: true });
|
||||
try {
|
||||
await build({ entryPoints: ['client/src/appearance-model.ts'], outfile: temporary, bundle: true, platform: 'node', format: 'esm', packages: 'external' });
|
||||
const { buildAppearance, defaultAppearance, disposeAppearance } = await import(pathToFileURL(temporary));
|
||||
let topology;
|
||||
for (const chest of ['male', 'female']) for (const extent of [-1, 0, 1]) {
|
||||
const a = defaultAppearance(); a.body.chest = chest;
|
||||
for (const key of ['headLength', 'headWidth', 'headDepth', 'chestWidth', 'hipsWidth', 'footSize']) a.body[key] = 1 + extent * .15;
|
||||
for (const key of ['armWidth', 'legWidth', 'eyeSpacing']) a.body[key] = 1 + extent * .2;
|
||||
for (const key of ['armLength', 'legLength']) a.body[key] = 1 + extent * .1;
|
||||
a.body.eyeHeight = a.body.noseHeight = extent * .035; a.body.mouthHeight = extent * .025;
|
||||
const root = buildAppearance(a), mesh = root.getObjectByName('humanoid-body');
|
||||
assert(mesh?.isSkinnedMesh);
|
||||
const p = mesh.geometry.getAttribute('position'), weights = mesh.geometry.getAttribute('skinWeight');
|
||||
assert([...p.array].every(Number.isFinite));
|
||||
const count = [p.count, mesh.geometry.index.count];
|
||||
if (topology) assert.deepEqual(count, topology); else topology = count;
|
||||
assert.equal(mesh.skeleton.bones.length, 17);
|
||||
for (let i = 0; i < weights.count; i++) assert(Math.abs(weights.getX(i) + weights.getY(i) - 1) < 1e-6);
|
||||
for (const name of ['helmet', 'chest', 'pants', 'weapon', 'offhand', 'cape', 'bag', 'ring1', 'ring8']) assert(root.getObjectByName('equipment:' + name));
|
||||
const before = new T.Vector3(); mesh.getVertexPosition(100, before);
|
||||
const leg = root.getObjectByName('left-shin'); leg.rotation.x = .5;
|
||||
root.updateMatrixWorld(true); mesh.skeleton.update();
|
||||
let displaced = 0;
|
||||
for (let i = 0; i < p.count; i++) {
|
||||
const vertex = mesh.getVertexPosition(i, new T.Vector3());
|
||||
assert([vertex.x, vertex.y, vertex.z].every(Number.isFinite));
|
||||
if (vertex.distanceTo(new T.Vector3().fromBufferAttribute(p, i)) > .01) displaced++;
|
||||
}
|
||||
assert(displaced > 20, 'joint rotation must actually deform weighted vertices');
|
||||
disposeAppearance(root);
|
||||
}
|
||||
for (const variant of [0, 1, 2, 3]) {
|
||||
const a = defaultAppearance(); a.variant = variant;
|
||||
const root = buildAppearance(a); assert(new T.Box3().setFromObject(root).getSize(new T.Vector3()).y > 1.5); disposeAppearance(root);
|
||||
}
|
||||
const legacy = { ...defaultAppearance(), version: 1, body: undefined };
|
||||
const old = buildAppearance(legacy); assert.equal(old.name, 'human-0-v1'); disposeAppearance(old);
|
||||
if (process.argv.includes('--preview-data')) {
|
||||
const designs = [];
|
||||
for (const chest of ['male', 'female']) {
|
||||
const a = defaultAppearance(); a.body.chest = chest;
|
||||
const root = buildAppearance(a), mesh = root.getObjectByName('humanoid-body');
|
||||
designs.push({ chest, positions: [...mesh.geometry.attributes.position.array], colors: [...mesh.geometry.attributes.color.array], indices: [...mesh.geometry.index.array] });
|
||||
disposeAppearance(root);
|
||||
}
|
||||
await writeFile('.local/humanoid-preview.json', JSON.stringify(designs));
|
||||
}
|
||||
console.log(`Humanoid bounds, topology, weights, joint deformation, anchors and legacy rendering passed (${topology[0]} vertices, ${topology[1] / 3} triangles).`);
|
||||
} finally { await rm(temporary, { force: true }); }
|
||||
Reference in New Issue
Block a user