Import a 3D Model and automatically assign and export animations
TypeScript
easier accessor and setters for Vec3 and Quat classes.
Scott Petrovic2cc13803cbc0
diff --git a/src/retarget/human-retargeting/Quat.ts b/src/retarget/human-retargeting/Quat.tsindex f1f11c7..38780cb 100644--- a/src/retarget/human-retargeting/Quat.ts+++ b/src/retarget/human-retargeting/Quat.ts@@ -4,10 +4,25 @@ import Vec3 from './Vec3.js'// they are implicitly created by the Array base class.export default class Quat extends Array {// optional copy contructor- constructor (v: Quat | null = null) {+ constructor (v: Quat | number | null = null, y?: number, z?: number, w?: number) {super(4)- if (v?.length === 4) this.copy(v)- else this.identity()++ if (typeof v === 'number' && y !== undefined && z !== undefined && w !== undefined) {+ this[0] = v+ this[1] = y+ this[2] = z+ this[3] = w+ return+ }++ if (v === null) {+ this.identity()+ return+ }++ if (typeof v === 'object' && v?.length === 4) {+ this.copy(v)+ }}// #region SETTERSdiff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.tsindex b627f50..a65cdb9 100644--- a/src/retarget/human-retargeting/Retargeter.ts+++ b/src/retarget/human-retargeting/Retargeter.ts@@ -6,7 +6,7 @@ import Vec3 from './Vec3'import Quat from './Quat'import Transform from './Transform'import { type RigItem } from './RigItem'-import { Joint } from './Joint'+import { type Joint } from './Joint'// example and library functions taken from sketchpunklabs// https://github.com/sketchpunklabs/threejs_proto/blob/main/code/webgl/anim/002_retarget_4m2m.html@@ -160,13 +160,13 @@ export class Retargeter {if (src === null || tar === null) return// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- const aTran = this.getWorld(this.srcRig.skel, src[0].idx)- const aSwing = new Vec3().fromQuat(aTran.rot, src[0].swing)- const aTwist = new Vec3().fromQuat(aTran.rot, src[0].twist)+ const aTran: Transform = this.getWorld(this.srcRig.skel, src[0].idx)+ const aSwing: Vec3 = new Vec3().fromQuat(aTran.rot, src[0].swing)+ const aTwist: Vec3 = new Vec3().fromQuat(aTran.rot, src[0].twist)- const bTran = this.getWorld(this.srcRig.skel, src[src.length - 1].idx)- const bSwing = new Vec3().fromQuat(bTran.rot, src[src.length - 1].swing)- const bTwist = new Vec3().fromQuat(bTran.rot, src[src.length - 1].twist)+ const bTran: Transform = this.getWorld(this.srcRig.skel, src[src.length - 1].idx)+ const bSwing: Vec3 = new Vec3().fromQuat(bTran.rot, src[src.length - 1].swing)+ const bTwist: Vec3 = new Vec3().fromQuat(bTran.rot, src[src.length - 1].twist)// Visualize data over source skeleton// Debug.pnt.add(aTran.pos, 0xffff00, 1.2)@@ -208,43 +208,44 @@ export class Retargeter {// Compute offset translation & scale it to fit better on targetapplyScaledTranslation (k: string): void {- if (this.srcRig === null || this.tarRig === null || this.pose === null) {- console.warn('Retargeter: applyScaledTranslation(). Missing srcRig, tarRig, or pose.')- return- }-// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- // Make sure we have our src & target+ // get chain root itemsconst src: RigItem = this.srcRig.chains[k][0]const tar: RigItem = this.tarRig.chains[k][0]if (src === null || tar === null) return// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Compute offset position change from animation- const scl = this.tarRig.scalar / this.srcRig.scalar // Scale from Src to Tar+ const scale_delta: number = this.tarRig.scalar / this.srcRig.scalar // Scale from Src to Tarconst source_t_pose_joint: Joint = this.srcRig.tpose.joints[src.idx] // TPose Src Jointconst source_ws_transform: Transform = this.getWorld(this.srcRig.skel, src.idx) // WS Tranform of Src Bone// ( animated.joint.world.pos - tpose.joint.world.pos ) * ( tarHipHeight / srcHipHeight )- const offset: Vec3 = new Vec3()+ const transform_offset: Vec3 = new Vec3().fromSub(source_ws_transform.pos, source_t_pose_joint.world.pos)- .scale(scl)+ .scale(scale_delta)// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Neutral Transform- const ptran: Transform = this.pose.getWorld(tar.pidx)- const ctran: Transform = new Transform().fromMul(ptran, this.tarRig.tpose.joints[tar.idx].local)+ const parent_transform: Transform = this.pose.getWorld(tar.pidx)+ const ctran: Transform = new Transform().fromMul(parent_transform, this.tarRig.tpose.joints[tar.idx].local)// Add scaled offset translation- const pos: Vec3 = new Vec3().fromAdd(ctran.pos, offset)+ const pos: Vec3 = new Vec3().fromAdd(ctran.pos, transform_offset)// Save to local space- this.pose.setPos(tar.idx, ptran.toLocalPos(pos))+ this.pose.setPos(tar.idx, parent_transform.toLocalPos(pos))}// #endregion// #region THREEJS HELPERS- // Run 3KJS's GetWorld functions & return as a Transform Object+ /**+ * Run three.js GetWorld functions & return as a Transform Object+ * @param skel THREE.Skeleton to get bone from+ * @param bone_idx Bone index+ * @param trans Transform object to store result in+ * @returns transform object for chaining. Also mutates the original transform passed in+ */public getWorld (skel: THREE.Skeleton, bone_idx: number, trans: Transform = new Transform()): Transform {const b: THREE.Bone = skel.bones[bone_idx]const p: THREE.Vector3 = b.getWorldPosition(new THREE.Vector3())diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.tsindex 0b266d8..05ae97e 100644--- a/src/retarget/human-retargeting/Rig.ts+++ b/src/retarget/human-retargeting/Rig.ts@@ -2,6 +2,7 @@// allows to work with different skeletons with various joint// naming convention. This makes it easier to build a system+import { type Joint } from './Joint'import { Pose } from './Pose'import { RigItem } from './RigItem'import Vec3 from './Vec3'@@ -13,7 +14,7 @@ export class Rig {public readonly skel: THREE.Skeletonpublic readonly tpose: Posepublic readonly chains: Record<string, RigItem[]> = {}- public scalar: number = 1+ public scalar: number = 1.0constructor (skel: THREE.Skeleton) {this.skel = skel@@ -23,16 +24,36 @@ export class Rig {fromConfig (cfg = {}): this {for (const [k, v] of Object.entries(cfg)) {- switch (k) {- case 'pelvis' : this.buildItem(k, v.names, new Vec3(0, 0, 1), new Vec3(0, 1, 0)).buildScalar(k); break- case 'spine' : this.buildItem(k, v.names, new Vec3(0, 1, 0), new Vec3(0, 0, 1)); break- case 'head' : this.buildItem(k, v.names, new Vec3(0, 0, 1), new Vec3(0, 1, 0)); break-- case 'armL' : this.buildItem(k, v.names, new Vec3(1, 0, 0), new Vec3(0, 0, -1)); break- case 'armR' : this.buildItem(k, v.names, new Vec3(-1, 0, 0), new Vec3(0, 0, -1)); break+ // Get joint names for this chain+ const chain_names: string[] = v.names as string[]+ if (chain_names.length === 0) {+ console.log('Error - Rig.fromConfig : No joint names for chain ', k)+ continue+ }- case 'legL' : this.buildItem(k, v.names, new Vec3(0, 0, 1), new Vec3(0, -1, 0)); break- case 'legR' : this.buildItem(k, v.names, new Vec3(0, 0, 1), new Vec3(0, -1, 0)); break+ switch (k) {+ case 'pelvis' :+ this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, 1, 0))+ this.buildRigScalar(k)+ break+ case 'spine' :+ this.buildItem(k, chain_names, new Vec3(0, 1, 0), new Vec3(0, 0, 1))+ break+ case 'head' :+ this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, 1, 0))+ break+ case 'armL' :+ this.buildItem(k, chain_names, new Vec3(1, 0, 0), new Vec3(0, 0, -1))+ break+ case 'armR' :+ this.buildItem(k, chain_names, new Vec3(-1, 0, 0), new Vec3(0, 0, -1))+ break+ case 'legL' :+ this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, -1, 0))+ break+ case 'legR' :+ this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, -1, 0))+ break}}return this@@ -40,7 +61,7 @@ export class Rig {// k = chain key like 'pelvis', 'armL', etcbuildItem (k: string, names: string[], swing: Vec3, twist: Vec3): this {- const ary = []+ const rig_items: RigItem[] = []let jfor (const n of names) {@@ -50,17 +71,18 @@ export class Rig {continue}- ary.push(new RigItem().fromJoint(j, swing, twist))+ rig_items.push(new RigItem().fromJoint(j, swing, twist))}- this.chains[k] = ary+ this.chains[k] = rig_itemsreturn this}- // k = chain key like 'pelvis', 'armL', etc- buildScalar (k: string): this {- const ch = this.chains[k]- const j = this.tpose.joints[ch[0].idx]+ // This will only be used for the pelvis chain+ // it helps with calculations with hips where characters might have different heights+ private buildRigScalar (chain_key: string): this {+ const ch: RigItem[] = this.chains[chain_key]+ const j: Joint = this.tpose.joints[ch[0].idx]this.scalar = j.world.pos[1]return this}diff --git a/src/retarget/human-retargeting/Transform.ts b/src/retarget/human-retargeting/Transform.tsindex bce0524..2495026 100644--- a/src/retarget/human-retargeting/Transform.ts+++ b/src/retarget/human-retargeting/Transform.ts@@ -6,7 +6,7 @@ import Vec3 from './Vec3'export default class Transform {// #region MAIN- public rot: Quat = new Quat([0, 0, 0, 1])+ public rot: Quat = new Quat(0, 0, 0, 1)public pos: Vec3 = new Vec3()public scl: Vec3 = new Vec3(1, 1, 1)@@ -129,30 +129,38 @@ export default class Transform {// #endregion// #region FROM OPERATORS- fromMul (tp: Transform, tc: Transform): this {++ /**+ * combining hierarchical transformations, such as when calculating the world transform+ * of a child object given its local transform and its parent’s world transform.+ * @param transform_parent transform of the parent+ * @param transform_child transform of the child+ * @returns instance for chaining. Also mutates the original transform that calls this method+ */+ fromMul (transform_parent: Transform, transform_child: Transform): this {// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// POSITION - parent.position + ( ( parent.scale * child.position ) * parent.rotation )const v = new Vec3( // parent.scale * child.position- tp.scl[0] * tc.pos[0],- tp.scl[1] * tc.pos[1],- tp.scl[2] * tc.pos[2]+ transform_parent.scl[0] * transform_child.pos[0],+ transform_parent.scl[1] * transform_child.pos[1],+ transform_parent.scl[2] * transform_child.pos[2])- qTransform(tp.rot, v, v) // * parent.rotation- this.pos[0] = tp.pos[0] + v[0] // parent.position +- this.pos[1] = tp.pos[1] + v[1]- this.pos[2] = tp.pos[2] + v[2]+ qTransform(transform_parent.rot, v, v) // * parent.rotation+ this.pos[0] = transform_parent.pos[0] + v[0] // parent.position ++ this.pos[1] = transform_parent.pos[1] + v[1]+ this.pos[2] = transform_parent.pos[2] + v[2]// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// SCALE - parent.scale * child.scale- this.scl[0] = tp.scl[0] * tc.scl[0]- this.scl[1] = tp.scl[1] * tc.scl[1]- this.scl[2] = tp.scl[2] * tc.scl[2]+ this.scl[0] = transform_parent.scl[0] * transform_child.scl[0]+ this.scl[1] = transform_parent.scl[1] * transform_child.scl[1]+ this.scl[2] = transform_parent.scl[2] * transform_child.scl[2]// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// ROTATION - parent.rotation * child.rotation// this.rot.fromMul( tp.rot, tc.rot );- qMul(tp.rot, tc.rot, this.rot)+ qMul(transform_parent.rot, transform_child.rot, this.rot)return this}@@ -283,7 +291,7 @@ export default class Transform {return out}- toLocalRot (wq: Quat, out: Quat = new Quat([0, 0, 0, 1])): number[] {+ toLocalRot (wq: Quat, out: Quat = new Quat(0, 0, 0, 1)): number[] {return qMul(qInvert(this.rot), wq, out)}@@ -292,21 +300,30 @@ export default class Transform {// #region INDEPENDANCE FROM VEC3/QUATfunction qTransform (q: Quat, v: Vec3, out: Vec3 = new Vec3()): Vec3 {- const qx = q[0]; const qy = q[1]; const qz = q[2]; const qw = q[3]- const vx = v[0]; const vy = v[1]; const vz = v[2]+ const qx = q.x+ const qy = q.y+ const qz = q.z+ const qw = q.w++ const vx = v.x+ const vy = v.y+ const vz = v.z+const x1 = qy * vz - qz * vyconst y1 = qz * vx - qx * vzconst z1 = qx * vy - qy * vxconst x2 = qw * x1 + qy * z1 - qz * y1const y2 = qw * y1 + qz * x1 - qx * z1const z2 = qw * z1 + qx * y1 - qy * x1- out[0] = vx + 2 * x2- out[1] = vy + 2 * y2- out[2] = vz + 2 * z2++ // set the output vector3+ out.x = vx + 2 * x2+ out.y = vy + 2 * y2+ out.z = vz + 2 * z2return out}-function qInvert (q: Quat, out: Quat = new Quat([0, 0, 0, 1])): Quat {+function qInvert (q: Quat, out: Quat = new Quat(0, 0, 0, 1)): Quat {const a0 = q[0]const a1 = q[1]const a2 = q[2]@@ -323,7 +340,7 @@ function qInvert (q: Quat, out: Quat = new Quat([0, 0, 0, 1])): Quat {return out}-function qMul (a: Quat, b: Quat, out: Quat = new Quat([0, 0, 0, 1])): Quat {+function qMul (a: Quat, b: Quat, out: Quat = new Quat(0, 0, 0, 1)): Quat {const ax = a[0]; const ay = a[1]; const az = a[2]; const aw = a[3]const bx = b[0]; const by = b[1]; const bz = b[2]; const bw = b[3]out[0] = ax * bw + aw * bx + ay * bz - az * bydiff --git a/src/retarget/human-retargeting/Vec3.ts b/src/retarget/human-retargeting/Vec3.tsindex 7df3ea9..dd1114f 100644--- a/src/retarget/human-retargeting/Vec3.ts+++ b/src/retarget/human-retargeting/Vec3.ts@@ -57,6 +57,19 @@ export default class Vec3 extends Array {this[2] = o.zreturn this}++ set x (x: number) {+ this[0] = x+ }++ set y (y: number) {+ this[1] = y+ }++ set z (z: number) {+ this[2] = z+ }+// #endregion// #region GETTERS@@ -79,6 +92,12 @@ export default class Vec3 extends Array {// #endregion// #region FROM OPS+ /**+ * Adds two vectors and sets this vector to the result+ * @param a First Vec3 to add+ * @param b Second Vec3 to add+ * @returns vec3 object for chaining. It also mutates the original vector that calls this method+ */fromAdd (a: Vec3, b: Vec3): this {this[0] = a[0] + b[0]this[1] = a[1] + b[1]