Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

convert more to typescript

Scott Petrovic35148b5af22b
diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.ts
index 1b3c42c..3d62cf0 100644
--- a/src/retarget/human-retargeting/Rig.ts
+++ b/src/retarget/human-retargeting/Rig.ts
@@ -1,8 +1,19 @@
// Setup a standard definition of what a Humanoid is. The rig
// allows to work with different skeletons with various joint
// naming convention. This makes it easier to build a system
+
+import { Pose } from './Pose'
+import { RigItem } from './RigItem'
+import Transform from './Transform'
+import Vec3 from './Vec3'
+
// that speaks a single language of whats what.
export class Rig {
+ private readonly skel: any
+ private readonly tpose: Pose
+ private readonly chains: Record<string, RigItem[]>
+ private scalar: number
+
constructor (skel) {
this.skel = skel
this.chains = {}
@@ -11,7 +22,7 @@ export class Rig {
// this.tpose.debug();
}
- fromConfig (cfg = {}) {
+ fromConfig (cfg = {}): this {
for (const [k, v] of Object.entries(cfg)) {
switch (k) {
case 'pelvis' : this.buildItem(k, v.names, [0, 0, 1], [0, 1, 0]).buildScalar(k); break
@@ -28,7 +39,7 @@ export class Rig {
return this
}
- buildItem (k, names, swing, twist) {
+ buildItem (k, names: string[], swing, twist): this {
const ary = []
let j
@@ -43,14 +54,14 @@ export class Rig {
return this
}
- buildScalar (k) {
+ buildScalar (k): this {
const ch = this.chains[k]
const j = this.tpose.joints[ch[0].idx]
this.scalar = j.world.pos[1]
return this
}
- debugSkelVectors () {
+ debugSkelVectors (): void {
// const bAry = this.skel.bones;
const tran = new Transform()
const v = new Vec3()
@@ -69,9 +80,9 @@ export class Rig {
}
}
- debugTPoseVectors () {
+ debugTPoseVectors (): void {
// const bAry = this.skel.bones;
- const tran = new Transform()
+ const tran: Transform = new Transform()
const v = new Vec3()
for (const [chName, ch] of Object.entries(this.chains)) {
diff --git a/src/retarget/human-retargeting/RigItem.ts b/src/retarget/human-retargeting/RigItem.ts
index efa6590..cea6d45 100644
--- a/src/retarget/human-retargeting/RigItem.ts
+++ b/src/retarget/human-retargeting/RigItem.ts
@@ -1,13 +1,15 @@
+import { type Joint } from './Joint'
+import Quat from './Quat'
+import Vec3 from './Vec3'
+
// Rig data about a single joint
export class RigItem {
- constructor () {
- this.idx = -1 // Joint Index
- this.pidx = -1 // Parent Joint Index
- this.swing = new Vec3(0, 0, 1) // Swing Direction - Z
- this.twist = new Vec3(0, 1, 0) // Twist Direction - Y
- }
+ private idx: number = -1 // Joint Index
+ private pidx: number = -1 // Parent Joint Index
+ private readonly swing: Vec3 = new Vec3(0, 0, 1) // Swing Direction - Z
+ private readonly twist: Vec3 = new Vec3(0, 1, 0) // Twist Direction - Y
- fromJoint (j, swing = null, twist = null) {
+ public fromJoint (j: Joint, swing = null, twist = null): this {
this.idx = j.index
this.pidx = j.pindex
diff --git a/src/retarget/human-retargeting/Transform.ts b/src/retarget/human-retargeting/Transform.ts
index 9564e3f..9760657 100644
--- a/src/retarget/human-retargeting/Transform.ts
+++ b/src/retarget/human-retargeting/Transform.ts
@@ -1,19 +1,32 @@
// https://gabormakesgames.com/blog_transforms_transforms.html
// https://gabormakesgames.com/blog_transforms_transform_world.html
+import Quat from './Quat'
+import Vec3 from './Vec3'
+
export default class Transform {
// #region MAIN
- rot = [0, 0, 0, 1]
- pos = [0, 0, 0]
- scl = [1, 1, 1]
- constructor (rot, pos, scl) {
- if (rot instanceof Transform) this.copy(rot)
- else if (rot && pos && scl) this.set(rot, pos, scl)
+ private rot: Quat = new Quat([0, 0, 0, 1])
+ public pos: Vec3 = new Vec3()
+ public scl: Vec3 = new Vec3(1, 1, 1)
+
+ /**
+ * There are multiple constructor formats, so the parameters look a bit odd.
+ * @param rot - can be a Quat or a Transform to copy from
+ * @param pos - position vector if rot is a Quat
+ * @param scl - scale vector if rot is a Quat
+ */
+ constructor (rot?: Quat | Transform, pos?: Vec3, scl?: Vec3) {
+ if (rot instanceof Transform) {
+ this.copy(rot)
+ } else if ((rot != null) && (pos != null) && (scl != null)) {
+ this.set(rot, pos, scl)
+ }
}
// #endregion
// #region SETTERS / GETTERS
- copy (t) {
+ copy (t: Transform): this {
this.rot[0] = t.rot[0]
this.rot[1] = t.rot[1]
this.rot[2] = t.rot[2]
@@ -29,19 +42,19 @@ export default class Transform {
return this
}
- set (r, p, s) {
- if (r) {
+ set (r?: Quat, p?: Vec3, s?: Vec3): this {
+ if (r != null) {
this.rot[0] = r[0]
this.rot[1] = r[1]
this.rot[2] = r[2]
this.rot[3] = r[3]
}
- if (p) {
+ if (p != null) {
this.pos[0] = p[0]
this.pos[1] = p[1]
this.pos[2] = p[2]
}
- if (s) {
+ if (s != null) {
this.scl[0] = s[0]
this.scl[1] = s[1]
this.scl[2] = s[2]
@@ -49,11 +62,13 @@ export default class Transform {
return this
}
- clone () { return new Transform(this) }
+ clone (): Transform {
+ return new Transform(this)
+ }
// #endregion
// #region OPERATIONS
- mul (tran) {
+ mul (tran: Transform): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// POSITION - parent.position + ( parent.rotation * ( parent.scale * child.position ) )
const p = [
@@ -80,41 +95,8 @@ export default class Transform {
return this
}
- // mulInv( tran ){
- // // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // // POSITION - parent.position + ( parent.rotation * ( parent.scale * child.position ) )
- // // const p = [
- // // tran.pos[0] * this.scl[0], // Scale
- // // tran.pos[1] * this.scl[1],
- // // tran.pos[2] * this.scl[2],
- // // ];
-
- // // qTransform( this.rot, p ); // Rotation
- // // this.pos[0] += p[0]; // Translation
- // // this.pos[1] += p[1];
- // // this.pos[2] += p[2];
-
- // const p = [
- // tran.pos[0] + this.pos[0],
- // tran.pos[1] + this.pos[1],
- // tran.pos[2] + this.pos[2],
- // ];
-
- // qTransform( this.rot, p, this.pos );
-
- // p[0] *= this.scl[0];
- // p[1] *= this.scl[1];
- // p[2] *= this.scl[2];
-
- // // this.pos[0] /= this.scl[0];
- // // this.pos[1] /= this.scl[1];
- // // this.pos[2] /= this.scl[2];
-
- // return this;
- // }
-
// Computing Transforms in reverse, Child - > Parent
- pmul (tran) {
+ pmul (tran: Transform): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// POSITION - parent.position + ( parent.rotation * ( parent.scale * child.position ) )
// The only difference for this func, We use the IN.scl & IN.rot instead of THIS.scl * THIS.rot
@@ -147,7 +129,7 @@ export default class Transform {
// #endregion
// #region FROM OPERATORS
- fromMul (tp, tc) {
+ fromMul (tp: Transform, tc: Transform): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// POSITION - parent.position + ( ( parent.scale * child.position ) * parent.rotation )
const v = [ // parent.scale * child.position
@@ -175,7 +157,7 @@ export default class Transform {
return this
}
- fromInvert (t) {
+ fromInvert (t: Transform): this {
// Invert Rotation
qInvert(t.rot, this.rot)
@@ -211,7 +193,7 @@ export default class Transform {
// Regular Applying transform, Does not work well for inversed transforms
// when dealing with World to Local Transformation
- transformVec3 (v, out = [0, 0, 0]) {
+ transformVec3 (v: Vec3, out: Vec3 = new Vec3()): Vec3 {
// GLSL - vecQuatRotation(model.rotation, a_position.xyz * model.scale) + model.position;
// Vector * Scale
@@ -240,7 +222,7 @@ export default class Transform {
// When using an inversed transform, use this to transform
// WorldSpace vectors to local space
- transformVec3Rev (v, out = [0, 0, 0]) {
+ transformVec3Rev (v: Vec3, out: Vec3 = new Vec3()): Vec3 {
// Translation
const vx = v[0] + this.pos[0]
const vy = v[1] + this.pos[1]
@@ -264,7 +246,7 @@ export default class Transform {
return out
}
- toLocalPos (wp, out = [0, 0, 0]) {
+ toLocalPos (wp: Vec3, out: Vec3 = new Vec3()): Vec3 {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Invert Transform Rot & Scl
const qi = qInvert(this.rot)
@@ -300,7 +282,7 @@ export default class Transform {
return out
}
- toLocalRot (wq, out = [0, 0, 0, 1]) {
+ toLocalRot (wq: number[], out: number[] = [0, 0, 0, 1]): number[] {
return qMul(qInvert(this.rot), wq, out)
}
@@ -308,7 +290,7 @@ export default class Transform {
}
// #region INDEPENDANCE FROM VEC3/QUAT
-function qTransform (q, v, out = [0, 0, 0]) {
+function 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 x1 = qy * vz - qz * vy
@@ -323,14 +305,14 @@ function qTransform (q, v, out = [0, 0, 0]) {
return out
}
-function qInvert (q, out = [0, 0, 0, 1]) {
+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]
const a3 = q[3]
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3
- if (dot == 0) { out[0] = out[1] = out[2] = out[3] = 0; return out }
+ if (dot === 0) { out[0] = out[1] = out[2] = out[3] = 0; return out }
const iDot = 1.0 / dot // let invDot = dot ? 1.0/dot : 0;
out[0] = -a0 * iDot
@@ -340,7 +322,7 @@ function qInvert (q, out = [0, 0, 0, 1]) {
return out
}
-function qMul (a, b, out = [0, 0, 0, 1]) {
+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 * by
diff --git a/tsconfig.json b/tsconfig.json
index 260ccbe..5009057 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
- "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
+ "target": "es2017", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */