Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Stop stepped modifier holding one step too long at exact boundaries (issue #690)

Nguyen Xuan Anfc7014a47bcc
diff --git a/src/animation-modifiers.js b/src/animation-modifiers.js
index 5d65ebdf..5795b3f1 100644
--- a/src/animation-modifiers.js
+++ b/src/animation-modifiers.js
@@ -144,7 +144,7 @@ export function remapTime(effects, keyframes, time, offsets) {
let remapped = time;
for (const modifier of effects.timeModifiers) {
if (modifier.type === 'stepped') {
- remapped = Math.floor((remapped - modifier.offset) / modifier.step) * modifier.step + modifier.offset;
+ remapped = Math.floor((remapped - modifier.offset) / modifier.step + 1e-9) * modifier.step + modifier.offset;
continue;
}
if (length <= 0 || remapped >= firstTime && remapped <= firstTime + length) continue;
diff --git a/tests/animation-modifiers.test.js b/tests/animation-modifiers.test.js
index 2413569e..a7c872a0 100644
--- a/tests/animation-modifiers.test.js
+++ b/tests/animation-modifiers.test.js
@@ -164,3 +164,13 @@ test('recipe motion tracks keep modifiers, sample them at the same times in ever
assert.ok(Math.abs(forward[1].cube[0] - 1.5) <= 0.1);
assert.ok(Math.abs(forward[2].eye[0] - 2.5) < 1e-6);
});
+
+test('stepped lands on the new step at frame times that are inexact in binary', () => {
+ for (const step of [1 / 30, 0.1]) {
+ const stepped = sampler({ modifiers: [{ type: 'stepped', step }] }, [{ time: 0, value: 0 }, { time: 4, value: 4 }]);
+ for (let stepIndex = 0; stepIndex * step < 3.9; stepIndex += 1) {
+ const expected = stepIndex * step;
+ assert.ok(Math.abs(stepped(expected) - expected) < 1e-9, `step ${step} index ${stepIndex}`);
+ }
+ }
+});