Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add types to the custom Vec3 and Quat classes

Scott Petrovic58afff1d86d1
diff --git a/src/retarget/human-retargeting/Quat.ts b/src/retarget/human-retargeting/Quat.ts
index aed6453..f171874 100644
--- a/src/retarget/human-retargeting/Quat.ts
+++ b/src/retarget/human-retargeting/Quat.ts
@@ -1,14 +1,17 @@
import Vec3 from './Vec3.js'
+// Since this extending Array, don't need to create members for x,y,z,w
+// they are implicitly created by the Array base class.
export default class Quat extends Array {
- constructor (v = null) {
+ // optional copy contructor
+ constructor (v: number[] | null = null) {
super(4)
if (v?.length === 4) this.copy(v)
else this.identity()
}
// #region SETTERS
- identity () {
+ identity (): this {
this[0] = 0
this[1] = 0
this[2] = 0
@@ -16,7 +19,8 @@ export default class Quat extends Array {
return this
}
- copy (v) {
+ // typescript enforce 4 elements for array
+ copy (v: Quat): this {
this[0] = v[0]
this[1] = v[1]
this[2] = v[2]
@@ -24,7 +28,8 @@ export default class Quat extends Array {
return this
}
- copyTo (v) {
+ // typescript enforce 4 elements for array
+ copyTo (v: Quat): this {
v[0] = this[0]
v[1] = this[1]
v[2] = this[2]
@@ -32,7 +37,9 @@ export default class Quat extends Array {
return this
}
- copyObj (o) {
+ // object needs to contain x,y,z,w keys. Ok to have other properties since
+ // we only care about these four.
+ copyObj (o: { x: number, y: number, z: number, w: number, [key: string]: unknown }): this {
this[0] = o.x
this[1] = o.y
this[2] = o.z
@@ -42,12 +49,12 @@ export default class Quat extends Array {
// #endregion
// #region GETTERS
- clone () { return new Quat(this) }
+ clone (): Quat { return new Quat(this) }
// #endregion
// #region OPERATIONS
/** Multiple Quaternion onto this Quaternion */
- mul (q) {
+ mul (q: Quat): this {
const ax = this[0]; const ay = this[1]; const az = this[2]; const aw = this[3]
const bx = q[0]; const by = q[1]; const bz = q[2]; const bw = q[3]
this[0] = ax * bw + aw * bx + ay * bz - az * by
@@ -58,7 +65,7 @@ export default class Quat extends Array {
}
/** PreMultiple Quaternions onto this Quaternion */
- pmul (q) {
+ pmul (q: Quat): this {
const ax = q[0]; const ay = q[1]; const az = q[2]; const aw = q[3]
const bx = this[0]; const by = this[1]; const bz = this[2]; const bw = this[3]
this[0] = ax * bw + aw * bx + ay * bz - az * by
@@ -68,8 +75,10 @@ export default class Quat extends Array {
return this
}
- norm () {
- let len = this[0] ** 2 + this[1] ** 2 + this[2] ** 2 + this[3] ** 2
+ /** Normalize the quaternion, making it a unit quaternion 0-1 range for each component
+ * Important for proper rotation behavior since unit quaternions represent valid rotations */
+ norm (): this {
+ let len = (this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2) + (this[3] ** 2)
if (len > 0) {
len = 1 / Math.sqrt(len)
this[0] *= len
@@ -80,27 +89,35 @@ export default class Quat extends Array {
return this
}
- invert () {
+ invert (): this {
const a0 = this[0]
const a1 = this[1]
const a2 = this[2]
const a3 = this[3]
- const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3
- if (dot == 0) { this[0] = this[1] = this[2] = this[3] = 0; return this }
+ // dot product for quaternion
+ const dot = (a0 * a0) + (a1 * a1) + (a2 * a2) + (a3 * a3)
+
+ // handle zero length quaternion.
+ // inverting a zero length quat is not valid.
+ if (dot === 0) {
+ this[0] = this[1] = this[2] = this[3] = 0
+ console.warn('Quat.invert(): zero length quaternion, cannot invert.')
+ return this
+ }
- const invDot = 1.0 / dot // let invDot = dot ? 1.0/dot : 0;
- this[0] = -a0 * invDot
- this[1] = -a1 * invDot
- this[2] = -a2 * invDot
- this[3] = a3 * invDot
+ const inverted_dot_product = 1.0 / dot
+ this[0] = -a0 * inverted_dot_product
+ this[1] = -a1 * inverted_dot_product
+ this[2] = -a2 * inverted_dot_product
+ this[3] = a3 * inverted_dot_product
return this
}
// #endregion
// #region SPECIAL OPERATORS
/** Inverts the quaternion passed in, then pre multiplies to this quaternion. */
- pmulInvert (q) {
+ pmulInvert (q: Quat): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// q.invert()
let ax = q[0]
@@ -133,7 +150,11 @@ export default class Quat extends Array {
return this
}
- pmulAxisAngle (axis, rad) {
+ /**
+ * Pre-multiplies this quaternion by a rotation defined by an axis and angle.
+ * Commonly used for applying incremental rotations to an existing orientation.
+ */
+ pmulAxisAngle (axis: Vec3, rad: number): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Quat.AxisAngle()
const half = rad * 0.5
@@ -149,14 +170,15 @@ export default class Quat extends Array {
const by = this[1]
const bz = this[2]
const bw = this[3]
- this[0] = ax * bw + aw * bx + ay * bz - az * by
- this[1] = ay * bw + aw * by + az * bx - ax * bz
- this[2] = az * bw + aw * bz + ax * by - ay * bx
- this[3] = aw * bw - ax * bx - ay * by - az * bz
+ this[0] = (ax * bw) + (aw * bx) + (ay * bz) - (az * by)
+ this[1] = (ay * bw) + (aw * by) + (az * bx) - (ax * bz)
+ this[2] = (az * bw) + (aw * bz) + (ax * by) - (ay * bx)
+ this[3] = (aw * bw) - (ax * bx) - (ay * by) - (az * bz)
return this
}
- pmulSwing (a, b) {
+ /** Pre-multiplies this quaternion by the shortest rotation from vector a to vector b */
+ pmulSwing (a: Vec3, b: Vec3): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// fromSwing
const dot = Vec3.dot(a, b)
@@ -211,7 +233,7 @@ export default class Quat extends Array {
return this
}
- dotNegate (chk) {
+ dotNegate (chk: Quat): this {
// quat.dot
const dot = this[0] * chk[0] +
this[1] * chk[1] +
@@ -231,9 +253,16 @@ export default class Quat extends Array {
// #endregion
// #region FROM OPS
- fromMul (a, b) {
- 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]
+ fromMul (a: Quat, b: Quat): this {
+ 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]
this[0] = ax * bw + aw * bx + ay * bz - az * by
this[1] = ay * bw + aw * by + az * bx - ax * bz
@@ -242,95 +271,47 @@ export default class Quat extends Array {
return this
}
- fromInvert (q) {
+ fromInvert (q: Quat): this {
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) { this[0] = this[1] = this[2] = this[3] = 0; return this }
+ if (dot === 0) {
+ this[0] = this[1] = this[2] = this[3] = 0
+ return this
+ }
- const invDot = 1.0 / dot // let invDot = dot ? 1.0/dot : 0;
- this[0] = -a0 * invDot
- this[1] = -a1 * invDot
- this[2] = -a2 * invDot
- this[3] = a3 * invDot
+ const inverted_dot_product: number = 1.0 / dot // let invDot = dot ? 1.0/dot : 0;
+ this[0] = -a0 * inverted_dot_product
+ this[1] = -a1 * inverted_dot_product
+ this[2] = -a2 * inverted_dot_product
+ this[3] = a3 * inverted_dot_product
return this
}
- fromPolar (lon, lat, up = null) {
+ fromPolar (lon: number, lat: number, up = null): this {
lat = Math.max(Math.min(lat, 89.999999), -89.999999) // Clamp lat, going to 90+ makes things spring around.
const phi = (90 - lat) * 0.01745329251 // PI / 180
const theta = lon * 0.01745329251
- const phi_s = Math.sin(phi)
- const v = [
+ const phi_s = Math.sin(phi)
+
+ const v: Vec3 = [
-(phi_s * Math.sin(theta)),
Math.cos(phi),
phi_s * Math.cos(theta)
- ]
+ ] as Vec3
this.fromLook(v, up || [0, 1, 0])
return this
}
- fromLookOLD (dir, up = [0, 1, 0]) {
- // Ported to JS from C# example at https://pastebin.com/ubATCxJY
- // TODO, if Dir and Up are equal, a roll happends. Need to find a way to fix this.
- const zAxis = new Vec3(dir).norm() // Forward
- const xAxis = new Vec3().fromCross(up, zAxis).norm() // Right
- const yAxis = new Vec3().fromCross(zAxis, xAxis).norm() // Up
-
- // fromAxis - Mat3 to Quat
- const m00 = xAxis[0]; const m01 = xAxis[1]; const m02 = xAxis[2]
- const m10 = yAxis[0]; const m11 = yAxis[1]; const m12 = yAxis[2]
- const m20 = zAxis[0]; const m21 = zAxis[1]; const m22 = zAxis[2]
- const t = m00 + m11 + m22
-
- let x, y, z, w, s
-
- if (t > 0.0) {
- s = Math.sqrt(t + 1.0)
- w = s * 0.5 // |w| >= 0.5
- s = 0.5 / s
- x = (m12 - m21) * s
- y = (m20 - m02) * s
- z = (m01 - m10) * s
- } else if ((m00 >= m11) && (m00 >= m22)) {
- s = Math.sqrt(1.0 + m00 - m11 - m22)
- x = 0.5 * s// |x| >= 0.5
- s = 0.5 / s
- y = (m01 + m10) * s
- z = (m02 + m20) * s
- w = (m12 - m21) * s
- } else if (m11 > m22) {
- s = Math.sqrt(1.0 + m11 - m00 - m22)
- y = 0.5 * s // |y| >= 0.5
- s = 0.5 / s
- x = (m10 + m01) * s
- z = (m21 + m12) * s
- w = (m20 - m02) * s
- } else {
- s = Math.sqrt(1.0 + m22 - m00 - m11)
- z = 0.5 * s // |z| >= 0.5
- s = 0.5 / s
- x = (m20 + m02) * s
- y = (m21 + m12) * s
- w = (m01 - m10) * s
- }
-
- this[0] = x
- this[1] = y
- this[2] = z
- this[3] = w
- return this
- }
-
- fromLook (fwd, up = [0, 1, 0]) {
+ fromLook (fwd: Vec3, up = [0, 1, 0]): this {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Orthogonal axes to make a mat3x3
- const zAxis = new Vec3(fwd)
+ const zAxis = new Vec3(fwd)
const xAxis = new Vec3().fromCross(up, zAxis).norm() // Right
// Z & UP are parallel
@@ -354,35 +335,35 @@ export default class Quat extends Array {
if (fTrace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
- fRoot = Math.sqrt(fTrace + 1.0) // 2w
- this[3] = 0.5 * fRoot
+ fRoot = Math.sqrt(fTrace + 1.0) // 2w
+ this[3] = 0.5 * fRoot
- fRoot = 0.5 / fRoot // 1/(4w)
- this[0] = (m[5] - m[7]) * fRoot
- this[1] = (m[6] - m[2]) * fRoot
- this[2] = (m[1] - m[3]) * fRoot
+ fRoot = 0.5 / fRoot // 1/(4w)
+ this[0] = (m[5] - m[7]) * fRoot
+ this[1] = (m[6] - m[2]) * fRoot
+ this[2] = (m[1] - m[3]) * fRoot
} else {
// |w| <= 1/2
let i = 0
- if (m[4] > m[0]) i = 1
- if (m[8] > m[i * 3 + i]) i = 2
+ if (m[4] > m[0]) i = 1
+ if (m[8] > m[i * 3 + i]) i = 2
const j = (i + 1) % 3
const k = (i + 2) % 3
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0)
- this[i] = 0.5 * fRoot
+ this[i] = 0.5 * fRoot
fRoot = 0.5 / fRoot
- this[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot
- this[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot
- this[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot
+ this[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot
+ this[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot
+ this[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot
}
return this
}
/** Using unit vectors, Shortest swing rotation from Direction A to Direction B */
- fromSwing (a, b) {
+ fromSwing (a: Vec3, b: Vec3): this {
// http://physicsforgames.blogspot.com/2010/03/Quat-tricks.html
const dot = Vec3.dot(a, b)
@@ -409,7 +390,7 @@ export default class Quat extends Array {
}
/** Axis must be normlized, Angle in Radians */
- fromAxisAngle (axis, rad) {
+ fromAxisAngle (axis: Vec3, rad: number): this {
const half = rad * 0.5
const s = Math.sin(half)
this[0] = axis[0] * s
@@ -419,7 +400,7 @@ export default class Quat extends Array {
return this
}
- fromAxes (xAxis, yAxis, zAxis) {
+ fromAxes (xAxis: Vec3, yAxis: Vec3, zAxis: Vec3): this {
const m00 = xAxis[0]; const m01 = xAxis[1]; const m02 = xAxis[2]
const m10 = yAxis[0]; const m11 = yAxis[1]; const m12 = yAxis[2]
const m20 = zAxis[0]; const m21 = zAxis[1]; const m22 = zAxis[2]
@@ -465,7 +446,7 @@ export default class Quat extends Array {
// #endregion
// #region ROTATIONS
- rotX (rad) {
+ rotX (rad: number): this {
// https://github.com/toji/gl-matrix/blob/master/src/gl-matrix/quat.js
rad *= 0.5
@@ -479,7 +460,7 @@ export default class Quat extends Array {
return this
}
- rotY (rad) {
+ rotY (rad: number): this {
rad *= 0.5
const ax = this[0]; const ay = this[1]; const az = this[2]; const aw = this[3]
@@ -492,7 +473,7 @@ export default class Quat extends Array {
return this
}
- rotZ (rad) {
+ rotZ (rad: number): this {
rad *= 0.5
const ax = this[0]; const ay = this[1]; const az = this[2]; const aw = this[3]
@@ -508,39 +489,44 @@ export default class Quat extends Array {
// #endregion
// #region CONVERT
- fromMat3 (m) {
+
+ /* Convert a 3x3 rotation matrix to a quaternion
+ * @param m - 3x3 rotation matrix as a flat number array (length 9)
+ * @returns This quaternion set to represent the rotation of the matrix
+ */
+ fromMat3 (m: number[]): this {
// https://github.com/toji/gl-matrix/blob/master/src/gl-matrix/quat.js#L305
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quat Calculus and Fast Animation".
- let fRoot
- const fTrace = m[0] + m[4] + m[8]
+ let fRoot: number
+ const fTrace: number = m[0] + m[4] + m[8]
if (fTrace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
- fRoot = Math.sqrt(fTrace + 1.0) // 2w
- this[3] = 0.5 * fRoot
+ fRoot = Math.sqrt(fTrace + 1.0) // 2w
+ this[3] = 0.5 * fRoot
- fRoot = 0.5 / fRoot // 1/(4w)
- this[0] = (m[5] - m[7]) * fRoot
- this[1] = (m[6] - m[2]) * fRoot
- this[2] = (m[1] - m[3]) * fRoot
+ fRoot = 0.5 / fRoot // 1/(4w)
+ this[0] = (m[5] - m[7]) * fRoot
+ this[1] = (m[6] - m[2]) * fRoot
+ this[2] = (m[1] - m[3]) * fRoot
} else {
// |w| <= 1/2
let i = 0
- if (m[4] > m[0]) i = 1
- if (m[8] > m[i * 3 + i]) i = 2
+ if (m[4] > m[0]) i = 1
+ if (m[8] > m[i * 3 + i]) i = 2
const j = (i + 1) % 3
const k = (i + 2) % 3
- fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0)
- this[i] = 0.5 * fRoot
+ fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0)
+ this[i] = 0.5 * fRoot
- fRoot = 0.5 / fRoot
- this[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot
- this[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot
- this[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot
+ fRoot = 0.5 / fRoot
+ this[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot
+ this[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot
+ this[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot
}
return this
}
diff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.ts
index c79f42d..cb99e0e 100644
--- a/src/retarget/human-retargeting/Retargeter.ts
+++ b/src/retarget/human-retargeting/Retargeter.ts
@@ -6,6 +6,9 @@ import Vec3 from './Vec3'
import Quat from './Quat'
import Transform from './Transform'
+// example and library functions taken from sketchpunklabs
+// https://github.com/sketchpunklabs/threejs_proto/blob/main/code/webgl/anim/002_retarget_4m2m.html
+
export class Retargeter {
clip: THREE.AnimationClip | null = null
mixer: THREE.AnimationMixer
diff --git a/src/retarget/human-retargeting/Vec3.ts b/src/retarget/human-retargeting/Vec3.ts
index 729cb54..c5f9cf2 100644
--- a/src/retarget/human-retargeting/Vec3.ts
+++ b/src/retarget/human-retargeting/Vec3.ts
@@ -1,40 +1,57 @@
+import type Quat from './Quat'
+
export default class Vec3 extends Array {
- constructor (v = null) {
+ // constructor can be in the following formats:
+ // -> new Vec3()
+ // -> new Vec3(x,y,z)
+ // -> new Vec3(vec3)
+ constructor (a?: number | Vec3 | null, b?: number, c?: number) {
super(3)
- if (v?.length === 3) this.copy(v)
- else this.zero()
+
+ // if all the args are numbers, set them as the x,y,z values
+ if (typeof a === 'number' && typeof b === 'number' && typeof c === 'number') {
+ this[0] = a
+ this[1] = b
+ this[2] = c
+ } else if (a !== undefined && typeof a === 'object') {
+ // we passed in an object, so copy/clone it
+ this.copy(a)
+ } else {
+ // empty constructor. return a 0,0,0 vector
+ this.zero()
+ }
}
// #region SETTERS
- zero () {
+ zero (): this {
this[0] = 0
this[1] = 0
this[2] = 0
return this
}
- copy (v) {
+ copy (v: Vec3): this {
this[0] = v[0]
this[1] = v[1]
this[2] = v[2]
return this
}
- copyTo (v) {
+ copyTo (v: Vec3): this {
v[0] = this[0]
v[1] = this[1]
v[2] = this[2]
return this
}
- xyz (x, y, z) {
+ xyz (x: number, y: number, z: number): this {
this[0] = x
this[1] = y
this[2] = z
return this
}
- copyObj (o) {
+ copyObj (o: { x: number, y: number, z: number, [key: string]: unknown }): this {
this[0] = o.x
this[1] = o.y
this[2] = o.z
@@ -43,44 +60,57 @@ export default class Vec3 extends Array {
// #endregion
// #region GETTERS
- get len () { return Math.sqrt(this[0] ** 2 + this[1] ** 2 + this[2] ** 2) }
- get lenSqr () { return this[0] ** 2 + this[1] ** 2 + this[2] ** 2 }
+ get len (): number {
+ return Math.sqrt(this[0] ** 2 + this[1] ** 2 + this[2] ** 2)
+ }
+
+ get lenSqr (): number {
+ return this[0] ** 2 + this[1] ** 2 + this[2] ** 2
+ }
- clone () { return new Vec3(this) }
+ clone (): Vec3 {
+ return new Vec3(this)
+ }
// #endregion
// #region FROM OPS
- fromAdd (a, b) {
+ fromAdd (a: Vec3, b: Vec3): this {
this[0] = a[0] + b[0]
this[1] = a[1] + b[1]
this[2] = a[2] + b[2]
return this
}
- fromSub (a, b) {
+ fromSub (a: Vec3, b: Vec3): this {
this[0] = a[0] - b[0]
this[1] = a[1] - b[1]
this[2] = a[2] - b[2]
return this
}
- fromScale (v, s) {
- this[0] = v[0] * s
- this[1] = v[1] * s
- this[2] = v[2] * s
+ /**
+ * Scale a vector by a scalar
+ * @param v Vector to scale
+ * @param scalar amount to scale 1.0 is 100%
+ * @returns new scaled vector
+ */
+ fromScale (v: Vec3, scalar: number): this {
+ this[0] = v[0] * scalar
+ this[1] = v[1] * scalar
+ this[2] = v[2] * scalar
return this
}
- fromScaleThenAdd (scale, a, b) {
+ fromScaleThenAdd (scale: number, a: Vec3, b: Vec3): this {
this[0] = a[0] * scale + b[0]
this[1] = a[1] * scale + b[1]
this[2] = a[2] * scale + b[2]
return this
}
- fromNorm (v) {
+ fromNorm (v: Vec3): this {
let mag = Math.sqrt(v[0] ** 2 + v[1] ** 2 + v[2] ** 2)
- if (mag == 0) return this
+ if (mag === 0) return this
mag = 1 / mag
this[0] = v[0] * mag
@@ -89,7 +119,7 @@ export default class Vec3 extends Array {
return this
}
- fromCross (a, b) {
+ fromCross (a: Vec3, b: Vec3): this {
const ax = a[0]; const ay = a[1]; const az = a[2]
const bx = b[0]; const by = b[1]; const bz = b[2]
@@ -99,7 +129,7 @@ export default class Vec3 extends Array {
return this
}
- fromQuat (q, v = [0, 0, 1]) {
+ fromQuat (q: Quat, v = new Vec3(0, 0, 1)): this {
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
@@ -114,7 +144,7 @@ export default class Vec3 extends Array {
return this
}
- fromLerp (a, b, t) {
+ fromLerp (a: Vec3, b: Vec3, t: number): this {
const ti = 1 - t
this[0] = a[0] * ti + b[0] * t
this[1] = a[1] * ti + b[1] * t
@@ -122,8 +152,9 @@ export default class Vec3 extends Array {
return this
}
- fromPlaneSnap (pnt, planeNorm, planePos = [0, 0, 0]) {
- if (!pnt) pnt = this
+ fromPlaneSnap (pnt: Vec3 | null, planeNorm: Vec3, planePos: Vec3 = new Vec3(0, 0, 0)): this {
+ // if there is no point, clone this vector
+ if (pnt == null) pnt = this.clone()
// Dot Product between pnt vector & normal
const dot =
@@ -142,49 +173,49 @@ export default class Vec3 extends Array {
// #endregion
// #region OPERATORS
- add (a) {
+ add (a: Vec3): this {
this[0] += a[0]
this[1] += a[1]
this[2] += a[2]
return this
}
- sub (v) {
+ sub (v: Vec3): this {
this[0] -= v[0]
this[1] -= v[1]
this[2] -= v[2]
return this
}
- mul (v) {
+ mul (v: Vec3): this {
this[0] *= v[0]
this[1] *= v[1]
this[2] *= v[2]
return this
}
- scale (v) {
+ scale (v: number): this {
this[0] *= v
this[1] *= v
this[2] *= v
return this
}
- invScale (v) {
+ invScale (v: number): this {
this[0] /= v
this[1] /= v
this[2] /= v
return this
}
- scaleThenAdd (s, v) {
+ scaleThenAdd (s: number, v: Vec3): this {
this[0] += v[0] * s
this[1] += v[1] * s
this[2] += v[2] * s
return this
}
- cross (b) {
+ cross (b: Vec3): this {
const ax = this[0]; const ay = this[1]; const az = this[2]
const bx = b[0]; const by = b[1]; const bz = b[2]
@@ -194,9 +225,9 @@ export default class Vec3 extends Array {
return this
}
- norm () {
+ norm (): this {
let mag = Math.sqrt(this[0] ** 2 + this[1] ** 2 + this[2] ** 2)
- if (mag != 0) {
+ if (mag !== 0) {
mag = 1 / mag
this[0] *= mag
this[1] *= mag
@@ -205,14 +236,14 @@ export default class Vec3 extends Array {
return this
}
- negate () {
+ negate (): this {
this[0] = -this[0]
this[1] = -this[1]
this[2] = -this[2]
return this
}
- transformQuat (q) {
+ transformQuat (q: Quat): this {
const qx = q[0]; const qy = q[1]; const qz = q[2]; const qw = q[3]
const vx = this[0]; const vy = this[1]; const vz = this[2]
const x1 = qy * vz - qz * vy
@@ -227,7 +258,7 @@ export default class Vec3 extends Array {
return this
}
- axisAngle (axis, rad) {
+ axisAngle (axis: Vec3, rad: number): this {
// Rodrigues Rotation formula:
// v_rot = v * cos(theta) + cross( axis, v ) * sin(theta) + axis * dot( axis, v) * (1-cos(theta))
const cp = new Vec3().fromCross(axis, this)
@@ -244,16 +275,23 @@ export default class Vec3 extends Array {
// #endregion
// #region STATIC OPS
- static len (a) { return Math.sqrt(a[0] ** 2 + a[1] ** 2 + a[2] ** 2) }
- static lenSqr (a) { return a[0] ** 2 + a[1] ** 2 + a[2] ** 2 }
+ static len (a: Vec3): number { return Math.sqrt(a[0] ** 2 + a[1] ** 2 + a[2] ** 2) }
- static dist (a, b) { return Math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2) }
- static distSqr (a, b) { return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2 }
+ static lenSqr (a: Vec3): number { return a[0] ** 2 + a[1] ** 2 + a[2] ** 2 }
- static dot (a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] }
- static cross (a, b, out = new Vec3()) {
- const ax = a[0]; const ay = a[1]; const az = a[2]
- const bx = b[0]; const by = b[1]; const bz = b[2]
+ static dist (a: Vec3, b: Vec3): number { return Math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2) }
+
+ static distSqr (a: Vec3, b: Vec3): number { return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2 }
+
+ static dot (a: Vec3, b: Vec3): number { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] }
+
+ static cross (a: Vec3, b: Vec3, out = new Vec3()): Vec3 {
+ const ax = a[0]
+ const ay = a[1]
+ const az = a[2]
+ const bx = b[0]
+ const by = b[1]
+ const bz = b[2]
out[0] = ay * bz - az * by
out[1] = az * bx - ax * bz
@@ -261,7 +299,7 @@ export default class Vec3 extends Array {
return out
}
- static angle (a, b) {
+ static angle (a: Vec3, b: Vec3): number {
// acos(dot(a,b)/(len(a)*len(b)))
// const theta = this.dot( a, b ) / ( Math.sqrt( Vec3.lenSqr(a) * Vec3.lenSqr(b) ) );
// return Math.acos( Math.max( -1, Math.min( 1, theta ) ) ); // clamp ( t, -1, 1 )
@@ -283,14 +321,19 @@ export default class Vec3 extends Array {
// else return Math.acos( cosine / ( Math.sqrt( a.lenSqr * b.lenSqr() ) ) );
}
- static look (fwd, up = [0, 1, 0]) {
+ static look (fwd: Vec3, up: Vec3 = new Vec3(0, 1, 0)): [Vec3, Vec3, Vec3] {
const zAxis = new Vec3(fwd).norm()
const xAxis = new Vec3().fromCross(up, zAxis).norm() // Right
// Z & UP are parallel
if (xAxis.lenSqr === 0) {
- if (Math.abs(up[2]) === 1) { zAxis[0] += 0.0001 } // shift x when Fwd or Bak
- else { zAxis[2] += 0.0001 } // shift z
+ if (Math.abs(up[2]) === 1) {
+ // shift x when Fwd or Bak
+ zAxis[0] += 0.0001
+ } else {
+ // shift z
+ zAxis[2] += 0.0001
+ }
zAxis.norm() // ReNormalize
xAxis.fromCross(up, zAxis).norm() // Redo Right