Better preview

This commit is contained in:
loucass003 committed 2026-09-26 01:20:26 +02:00
1 parent 89592a9689
commit 0b520d0f40
3 files changed
+125 -39

No files matched your search

+70 -18
View File
@@ -1,6 +1,7 @@
import {
Box3,
BoxGeometry,
Matrix4,
Mesh,
MeshStandardMaterial,
Object3D,
@@ -27,11 +28,13 @@ import {
import { SkeletonProportions, deriveSkeletonProportions } from './skeletonProportions';
const position = new Vector3();
const shapePos = new Vector3();
const quat = new Quaternion();
const localOffset = new Vector3();
const modelBox = new Box3();
const shapeBounds = new Box3();
const shapeToBone = new Matrix4();
const corner = new Vector3();
const mountingNormal = new Vector3();
const rayOrigin = new Vector3();
const rayDirection = new Vector3();
@@ -58,8 +61,43 @@ function loadModel(url: string): Promise<Object3D | null> {
interface AttachedShape {
config: BoneShapeConfig;
/** On the bone: its orientation, and the shape's offset and size along its axes. */
node: Object3D;
/** Under the node: the turn that puts the model onto the bone's axes. */
tilt: Object3D;
/** From the bone's head to the node, in the bone's axes. */
offset: Vector3;
model: ModelDimensions | null;
/** The model's box in its own axes. */
bounds: Box3 | null;
}
/**
* A point inside the shape, in the bone's axes and from its head, out of a
* position in the shape's bounds (-1 to 1 per axis from its centre). False until
* the model has loaded.
*/
function shapeAnchor(
{ node, tilt, offset, bounds }: AttachedShape,
anchor: Vector3,
out: Vector3
) {
if (!bounds) return false;
tilt.updateMatrix();
shapeToBone.makeScale(node.scale.x, node.scale.y, node.scale.z).multiply(tilt.matrix);
shapeBounds.makeEmpty();
for (const x of [bounds.min.x, bounds.max.x]) {
for (const y of [bounds.min.y, bounds.max.y]) {
for (const z of [bounds.min.z, bounds.max.z]) {
shapeBounds.expandByPoint(corner.set(x, y, z).applyMatrix4(shapeToBone));
}
}
}
shapeBounds.getCenter(out);
shapeBounds.getSize(corner).multiplyScalar(0.5);
out.add(corner.multiply(anchor)).add(offset);
return true;
}
interface BonePart extends SkeletonRenderPart {
@@ -111,10 +149,16 @@ export class BasedSkeletonMeshHelper extends Object3D {
node.matrixAutoUpdate = false;
this.add(node);
const tilt = new Object3D();
node.add(tilt);
const attached: AttachedShape = {
config: shapeConfig,
node,
tilt,
offset: new Vector3(),
model: null,
bounds: null,
};
const modelUrl = shapeConfig.modelUrl ?? DefaultBoneModelUrl;
@@ -154,8 +198,9 @@ export class BasedSkeletonMeshHelper extends Object3D {
length: Math.abs(modelBox.min.y),
};
attached.node.clear();
attached.node.add(o);
attached.bounds = modelBox.clone();
attached.tilt.clear();
attached.tilt.add(o);
part.surfaceDirty = true;
}
});
@@ -209,7 +254,7 @@ export class BasedSkeletonMeshHelper extends Object3D {
const boneLength = Math.max(bone.boneLength, 1e-4);
for (const attached of shapes) {
const { config, node, model } = attached;
const { config, node, tilt, model } = attached;
const size = computeShapeScale(
config,
this.proportions,
@@ -217,19 +262,19 @@ export class BasedSkeletonMeshHelper extends Object3D {
model ?? undefined
);
shapePos.copy(position);
attached.offset.set(0, 0, 0);
if (config.offset && model) {
localOffset
.copy(config.offset({ model, proportions: this.proportions, boneLength }))
.applyQuaternion(quat);
shapePos.add(localOffset);
attached.offset.copy(
config.offset({ model, proportions: this.proportions, boneLength })
);
}
node.position.copy(shapePos);
node.position.copy(attached.offset).applyQuaternion(quat).add(position);
node.quaternion.copy(quat);
if (config.rotation) node.quaternion.multiply(config.rotation);
node.scale.set(size.width, size.length, size.depth);
node.updateMatrix();
if (config.rotation) tilt.quaternion.copy(config.rotation);
}
if (part.tracker) {
@@ -245,9 +290,12 @@ export class BasedSkeletonMeshHelper extends Object3D {
part.marker.scale.setScalar(getTrackerMarkerScale(bone.bodyPart));
part.marker.quaternion.copy(quat).multiply(part.tracker.mountingOrientation);
part.marker.position.copy(position);
localOffset
.set(0, -boneLength * part.tracker.boneOffset, 0)
.applyQuaternion(quat);
const anchor = SKELETON_PART_PRESETS[bone.bodyPart]?.trackerAnchor;
const anchored = anchor && shapes[0] && shapeAnchor(shapes[0], anchor, localOffset);
if (!anchored) {
localOffset.set(0, -boneLength * part.tracker.boneOffset, 0);
}
localOffset.applyQuaternion(quat);
part.marker.position.add(localOffset);
mountingNormal.set(0, 0, 1).applyQuaternion(part.marker.quaternion);
if (part.surfaceDirty) {
@@ -257,11 +305,15 @@ export class BasedSkeletonMeshHelper extends Object3D {
rayOrigin.copy(part.marker.position).applyMatrix4(this.matrixWorld);
rayDirection.copy(mountingNormal).transformDirection(this.matrixWorld);
surfaceRaycaster.set(rayOrigin, rayDirection);
// The last hit is the outermost surface: the marker starts inside the
// shape, and a plate's inner face comes before its outer one.
part.surfaceDistance =
surfaceRaycaster.intersectObjects(
shapes.map(({ node }) => node),
true
)[0]?.distance ?? 0;
surfaceRaycaster
.intersectObjects(
shapes.map(({ node }) => node),
true
)
.at(-1)?.distance ?? 0;
part.surfaceDirty = false;
}
part.marker.position.addScaledVector(mountingNormal, part.surfaceDistance);
+50 -16
View File
@@ -187,6 +187,12 @@ export interface BonePartConfig {
shapes: BoneShapeConfig[];
/** Where an assigned tracker sits from this bone's head (0) to tail (1). */
trackerOffset?: number;
/**
* Where the marker starts inside the part's first shape, in the bone's own
* axes: -1 to 1 on each, 0 at the shape's centre. Used instead of
* `trackerOffset` for bones whose length doesn't span their shape.
*/
trackerAnchor?: Vector3;
}
export const shape = (overrides: Partial<BoneShapeConfig> = {}): BoneShapeConfig => ({
@@ -217,8 +223,18 @@ const fingerRight = (file: string) =>
model(file, { scale: otherSide(spanBone()), offset: inMetres({ width: 0.003 }) })
);
const toe = (file: string) => part(model(file));
const toeRight = (file: string) => part(model(file, { scale: otherSide(spanBone()) }));
// Toe bones are drawn along their local y like the foot, so +y is back toward the ankle.
// The base of each toe mesh is a painted cap, tucked into the foot so it never
// shows. The length ratio keeps the tip on the bone's tail despite the tuck.
const TOE_TUCK = 0.5;
const toeScale = (width: number, depth: number) =>
spanBone({ width, depth, length: 1 + TOE_TUCK });
const toeOffset = inBoneLengths({ length: TOE_TUCK, depth: 0.11 });
const toe = (file: string, scale = toeScale(1.3, 1.0)) =>
part(model(file, { scale, offset: toeOffset }));
const toeRight = (file: string, scale = toeScale(1.3, 1.0)) =>
part(model(file, { scale: otherSide(scale), offset: toeOffset }));
const bigToeScale = toeScale(1.3, 1.15);
const shoulderScale = spanBone({ width: 0.8, depth: 0.8, length: 1.3 });
const handScale = spanBone({ width: 1.0, depth: 1.0, length: 1.0 });
@@ -226,11 +242,25 @@ const upperArmScale = byCircumference(1.2, { length: 1.0 });
const lowerArmScale = byCircumference(1.0, { length: 1.0 });
const upperLegScale = spanBone({ girthFrom: 'hips', length: 1.0 });
const lowerLegScale = spanBone({ girthFrom: 'hips', length: 0.9 });
const footScale = authoredSize({ width: 0.99, depth: 1.26 });
const REFERENCE_FOOT_LENGTH = 0.13;
const TOE_ROW_SPREAD = 0.46;
const TOE_ROW_EDGES = 0.028;
const footScale: ShapeScale = {
compute: ({ proportions, boneLength }) => {
const toeRowSpan =
TOE_ROW_SPREAD * boneLength + TOE_ROW_EDGES * proportions.bodyScale;
const referenceSpan = TOE_ROW_SPREAD * REFERENCE_FOOT_LENGTH + TOE_ROW_EDGES;
return {
width: (0.9 * toeRowSpan) / referenceSpan,
length: (1.12 * boneLength) / REFERENCE_FOOT_LENGTH,
depth: 1.14 * proportions.bodyScale,
};
},
};
// Forward follows foot length; the drop from the ankle to the sole doesn't.
const footOffset: ShapeOffset = ({ boneLength, proportions }) =>
new Vector3(0, -0.1 * boneLength, -0.1 * proportions.bodyScale);
// Recommended tracker mounting positions, measured from a bone's head (0)
// towards its tail (1). Mounting orientation still decides the front/back
// surface; these values only choose the position along the bone.
const CHEST_TRACKER_OFFSET = 0.2;
const WAIST_TRACKER_OFFSET = 0.8;
const HIP_TRACKER_OFFSET = 0.75;
@@ -238,15 +268,19 @@ const UPPER_ARM_TRACKER_OFFSET = 0.75;
const LOWER_ARM_TRACKER_OFFSET = 0.15;
const UPPER_LEG_TRACKER_OFFSET = 0.7;
const LOWER_LEG_TRACKER_OFFSET = 0.75;
const FOOT_TRACKER_OFFSET = 0.15;
const FOOT_MARKER: Partial<BonePartConfig> = {
trackerAnchor: new Vector3(0, -0.3, 0),
};
export const SKELETON_PART_PRESETS: Record<BodyPart, BonePartConfig> = {
[BodyPart.NONE]: part(shape(), { visible: false }),
[BodyPart.HEAD]: part(
model('head', {
rotation: turn({ width: 90 }),
offset: inBoneLengths({ length: -1, depth: 0.5 }),
})
scale: authoredSize({ length: 1.04 }),
offset: inMetres({ length: 0.005 }),
}),
{ trackerAnchor: new Vector3(0, 0.2, 0) }
),
[BodyPart.NECK]: part(
model('neck', {
@@ -360,19 +394,19 @@ export const SKELETON_PART_PRESETS: Record<BodyPart, BonePartConfig> = {
),
[BodyPart.LEFT_FOOT]: part(
model('foot', {
offset: inBoneLengths({ depth: -0.7, length: -0.2 }),
offset: footOffset,
scale: footScale,
rotation: turn({ width: -42 }),
}),
{ trackerOffset: FOOT_TRACKER_OFFSET }
FOOT_MARKER
),
[BodyPart.RIGHT_FOOT]: part(
model('foot', {
offset: inBoneLengths({ depth: -0.7, length: -0.2 }),
offset: footOffset,
scale: otherSide(footScale),
rotation: turn({ width: -42 }),
}),
{ trackerOffset: FOOT_TRACKER_OFFSET }
FOOT_MARKER
),
[BodyPart.LEFT_THUMB_METACARPAL]: finger('thumb_metacarpal'),
@@ -407,12 +441,12 @@ export const SKELETON_PART_PRESETS: Record<BodyPart, BonePartConfig> = {
[BodyPart.RIGHT_LITTLE_INTERMEDIATE]: fingerRight('little_intermediate'),
[BodyPart.RIGHT_LITTLE_DISTAL]: fingerRight('little_distal'),
[BodyPart.LEFT_BIG_TOE]: toe('big_toe'),
[BodyPart.LEFT_BIG_TOE]: toe('big_toe', bigToeScale),
[BodyPart.LEFT_INDEX_TOE]: toe('index_toe'),
[BodyPart.LEFT_MIDDLE_TOE]: toe('middle_toe'),
[BodyPart.LEFT_RING_TOE]: toe('ring_toe'),
[BodyPart.LEFT_LITTLE_TOE]: toe('little_toe'),
[BodyPart.RIGHT_BIG_TOE]: toeRight('big_toe'),
[BodyPart.RIGHT_BIG_TOE]: toeRight('big_toe', bigToeScale),
[BodyPart.RIGHT_INDEX_TOE]: toeRight('index_toe'),
[BodyPart.RIGHT_MIDDLE_TOE]: toeRight('middle_toe'),
[BodyPart.RIGHT_RING_TOE]: toeRight('ring_toe'),
@@ -259,27 +259,27 @@ private val TOES = listOf(
Toe(
BodyPart.LEFT_BIG_TOE to BodyPart.RIGHT_BIG_TOE,
lengthFraction = 0.27f,
headOffset = Vector3(0.28f, 0f, 0f),
headOffset = Vector3(0.23f, 0f, 0f),
),
Toe(
BodyPart.LEFT_INDEX_TOE to BodyPart.RIGHT_INDEX_TOE,
lengthFraction = 0.24f,
headOffset = Vector3(0.12f, 0f, 0f),
headOffset = Vector3(0.106f, 0f, 0f),
),
Toe(
BodyPart.LEFT_MIDDLE_TOE to BodyPart.RIGHT_MIDDLE_TOE,
lengthFraction = 0.23f,
headOffset = Vector3(0f, 0f, 0f),
headOffset = Vector3(-0.01f, 0f, 0f),
),
Toe(
BodyPart.LEFT_RING_TOE to BodyPart.RIGHT_RING_TOE,
lengthFraction = 0.21f,
headOffset = Vector3(-0.12f, 0f, 0f),
headOffset = Vector3(-0.122f, 0f, 0f),
),
Toe(
BodyPart.LEFT_LITTLE_TOE to BodyPart.RIGHT_LITTLE_TOE,
lengthFraction = 0.18f,
headOffset = Vector3(-0.26f, 0f, 0f),
headOffset = Vector3(-0.23f, 0f, 0f),
),
)