Here’s a function to center mesh content in a node:
function centerMeshContent(node: TransformNode) {
const { min, max } = node.getHierarchyBoundingVectors();
const halfRange = max.subtract(min).scaleInPlace(0.5);
const currentCenter = min.add(halfRange);
const offset = currentCenter.negate();
traverseNodes(node, (n) => {
if (n instanceof Mesh) {
n.position.addInPlace(offset);
// The bounding box is world-based, so re-adjust back into local space.
n.position.addInPlace(node.getAbsolutePosition());
}
});
}
function traverseNodes(node: Node | Scene, visit: (node: Node) => void) {
const children = node instanceof Node ? node.getChildren() : node.rootNodes;
for (const child of children) {
visit(child);
traverseNodes(child, visit);
}
}