Skip to content

Commit

Permalink
Anim folder circular dependency (#4745)
Browse files Browse the repository at this point in the history
* fix the circular dep in the anim folder
  • Loading branch information
ellthompson authored Oct 13, 2022
1 parent 2421759 commit 5188888
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 97 deletions.
90 changes: 90 additions & 0 deletions src/framework/anim/evaluator/anim-blend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class AnimBlend {
static dot(a, b) {
const len = a.length;
let result = 0;
for (let i = 0; i < len; ++i) {
result += a[i] * b[i];
}
return result;
}

static normalize(a) {
let l = AnimBlend.dot(a, a);
if (l > 0) {
l = 1.0 / Math.sqrt(l);
const len = a.length;
for (let i = 0; i < len; ++i) {
a[i] *= l;
}
}
}

static set(a, b, type) {
const len = a.length;

if (type === 'quaternion') {
let l = AnimBlend.dot(b, b);
if (l > 0) {
l = 1.0 / Math.sqrt(l);
}
for (let i = 0; i < len; ++i) {
a[i] = b[i] * l;
}
} else {
for (let i = 0; i < len; ++i) {
a[i] = b[i];
}
}
}

static blendVec(a, b, t, additive) {
const it = additive ? 1.0 : 1.0 - t;
const len = a.length;
for (let i = 0; i < len; ++i) {
a[i] = a[i] * it + b[i] * t;
}
}

static blendQuat(a, b, t, additive) {
const len = a.length;
const it = additive ? 1.0 : 1.0 - t;

// negate b if a and b don't lie in the same winding (due to
// double cover). if we don't do this then often rotations from
// one orientation to another go the long way around.
if (AnimBlend.dot(a, b) < 0) {
t = -t;
}

for (let i = 0; i < len; ++i) {
a[i] = a[i] * it + b[i] * t;
}

if (!additive) {
AnimBlend.normalize(a);
}
}

static blend(a, b, t, type, additive) {
if (type === 'quaternion') {
AnimBlend.blendQuat(a, b, t, additive);
} else {
AnimBlend.blendVec(a, b, t, additive);
}
}

static stableSort(a, lessFunc) {
const len = a.length;
for (let i = 0; i < len - 1; ++i) {
for (let j = i + 1; j < len; ++j) {
if (lessFunc(a[j], a[i])) {
const tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
}

export { AnimBlend };
96 changes: 5 additions & 91 deletions src/framework/anim/evaluator/anim-evaluator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AnimTargetValue } from './anim-target-value.js';
import { AnimBlend } from './anim-blend.js';

/** @typedef {import('../binder/anim-binder.js').AnimBinder} AnimBinder */
/** @typedef {import('./anim-clip.js').AnimClip} AnimClip */
Expand Down Expand Up @@ -31,93 +32,6 @@ class AnimEvaluator {
return this._clips;
}

static _dot(a, b) {
const len = a.length;
let result = 0;
for (let i = 0; i < len; ++i) {
result += a[i] * b[i];
}
return result;
}

static _normalize(a) {
let l = AnimEvaluator._dot(a, a);
if (l > 0) {
l = 1.0 / Math.sqrt(l);
const len = a.length;
for (let i = 0; i < len; ++i) {
a[i] *= l;
}
}
}

static _set(a, b, type) {
const len = a.length;

if (type === 'quaternion') {
let l = AnimEvaluator._dot(b, b);
if (l > 0) {
l = 1.0 / Math.sqrt(l);
}
for (let i = 0; i < len; ++i) {
a[i] = b[i] * l;
}
} else {
for (let i = 0; i < len; ++i) {
a[i] = b[i];
}
}
}

static _blendVec(a, b, t, additive) {
const it = additive ? 1.0 : 1.0 - t;
const len = a.length;
for (let i = 0; i < len; ++i) {
a[i] = a[i] * it + b[i] * t;
}
}

static _blendQuat(a, b, t, additive) {
const len = a.length;
const it = additive ? 1.0 : 1.0 - t;

// negate b if a and b don't lie in the same winding (due to
// double cover). if we don't do this then often rotations from
// one orientation to another go the long way around.
if (AnimEvaluator._dot(a, b) < 0) {
t = -t;
}

for (let i = 0; i < len; ++i) {
a[i] = a[i] * it + b[i] * t;
}

if (!additive) {
AnimEvaluator._normalize(a);
}
}

static _blend(a, b, t, type, additive) {
if (type === 'quaternion') {
AnimEvaluator._blendQuat(a, b, t, additive);
} else {
AnimEvaluator._blendVec(a, b, t, additive);
}
}

static _stableSort(a, lessFunc) {
const len = a.length;
for (let i = 0; i < len - 1; ++i) {
for (let j = i + 1; j < len; ++j) {
if (lessFunc(a[j], a[i])) {
const tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}

/**
* Add a clip to the evaluator.
*
Expand Down Expand Up @@ -280,7 +194,7 @@ class AnimEvaluator {
const order = clips.map(function (c, i) {
return i;
});
AnimEvaluator._stableSort(order, function (a, b) {
AnimBlend.stableSort(order, function (a, b) {
return clips[a].blendOrder < clips[b].blendOrder;
});

Expand All @@ -306,7 +220,7 @@ class AnimEvaluator {
output = outputs[j];
value = output.value;

AnimEvaluator._set(value, input, output.target.type);
AnimBlend.set(value, input, output.target.type);

output.blendCounter++;
}
Expand All @@ -317,9 +231,9 @@ class AnimEvaluator {
value = output.value;

if (output.blendCounter === 0) {
AnimEvaluator._set(value, input, output.target.type);
AnimBlend.set(value, input, output.target.type);
} else {
AnimEvaluator._blend(value, input, blendWeight, output.target.type);
AnimBlend.blend(value, input, blendWeight, output.target.type);
}

output.blendCounter++;
Expand Down
12 changes: 6 additions & 6 deletions src/framework/anim/evaluator/anim-target-value.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Quat } from '../../../core/math/quat.js';
import { ANIM_LAYER_ADDITIVE, ANIM_LAYER_OVERWRITE } from '../controller/constants.js';
import { AnimEvaluator } from './anim-evaluator.js';
import { AnimBlend } from './anim-blend.js';
import { math } from '../../../core/math/math.js';

/**
Expand Down Expand Up @@ -86,9 +86,9 @@ class AnimTargetValue {
updateValue(index, value) {
// always reset the value of the target when the counter is 0
if (this.counter === 0) {
AnimEvaluator._set(this.value, AnimTargetValue.IDENTITY_QUAT_ARR, this.valueType);
AnimBlend.set(this.value, AnimTargetValue.IDENTITY_QUAT_ARR, this.valueType);
if (!this._normalizeWeights) {
AnimEvaluator._blend(this.value, this.baseValue, 1, this.valueType);
AnimBlend.blend(this.value, this.baseValue, 1, this.valueType);
}
}
if (!this.mask[index] || this.getWeight(index) === 0) return;
Expand All @@ -108,15 +108,15 @@ class AnimTargetValue {
AnimTargetValue.quatArr[1] = v.y;
AnimTargetValue.quatArr[2] = v.z;
AnimTargetValue.quatArr[3] = v.w;
AnimEvaluator._set(this.value, AnimTargetValue.quatArr, this.valueType);
AnimBlend.set(this.value, AnimTargetValue.quatArr, this.valueType);
} else {
AnimTargetValue.vecArr[0] = value[0] - this.baseValue[0];
AnimTargetValue.vecArr[1] = value[1] - this.baseValue[1];
AnimTargetValue.vecArr[2] = value[2] - this.baseValue[2];
AnimEvaluator._blend(this.value, AnimTargetValue.vecArr, this.getWeight(index), this.valueType, true);
AnimBlend.blend(this.value, AnimTargetValue.vecArr, this.getWeight(index), this.valueType, true);
}
} else {
AnimEvaluator._blend(this.value, value, this.getWeight(index), this.valueType);
AnimBlend.blend(this.value, value, this.getWeight(index), this.valueType);
}
if (this.setter) this.setter(this.value);
}
Expand Down

0 comments on commit 5188888

Please sign in to comment.