Skip to content

Commit b752f6f

Browse files
committed
⌛ Animation Preview Adjustment
The animation preview now more accurately replicates the in-game result of animations by locking interpolation to linear between each 20th of a second (each tick).
1 parent 5f6fa2b commit b752f6f

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
- [ ] trident
8080
- [ ] Change the Collection setting type to allow single-click swapping of items between lists.
8181
- [ ] Look into adding a color picker for tintable vanilla items.
82-
- [ ] Add Variants to the UndoSystem (Blocked by vanilla Blockbench not supporting custom undo actions)
82+
- [ ] Add Variants to the UndoSystem (Blocked by vanilla Blockbench not supporting custom undo actions).
83+
- [ ] Remove `easingArgs` and `easingMode` from saved keyframes if `easingType` is `linear`.
8384

8485
# Data Pack Compiler
8586

src/mods/bonePropertiesMod.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { isCurrentFormat as condition } from '../blueprintFormat'
1+
import { isCurrentFormat as condition, isCurrentFormat } from '../blueprintFormat'
22
import { PACKAGE } from '../constants'
3+
import { roundToNth } from '../util/misc'
34
import { type ContextProperty, createBlockbenchMod } from '../util/moddingTools'
45

56
createBlockbenchMod(
@@ -19,3 +20,71 @@ createBlockbenchMod(
1920
context.configs?.delete()
2021
}
2122
)
23+
24+
createBlockbenchMod(
25+
`${PACKAGE.name}:boneInterpolationMod`,
26+
{
27+
orignalInterpolate: BoneAnimator.prototype.interpolate,
28+
},
29+
context => {
30+
BoneAnimator.prototype.interpolate = function (
31+
this: BoneAnimator,
32+
channel,
33+
allowExpression,
34+
axis
35+
) {
36+
if (!isCurrentFormat()) {
37+
return context.orignalInterpolate.call(this, channel, allowExpression, axis)
38+
}
39+
40+
const actualTime = this.animation.time
41+
try {
42+
Timeline.time = roundToNth(this.animation.time, 20)
43+
44+
let before: ArrayVector3 | false
45+
let after: ArrayVector3 | false
46+
let beforeTime: number
47+
let afterTime: number
48+
49+
if (Timeline.time < actualTime) {
50+
beforeTime = Timeline.time
51+
before = context.orignalInterpolate.call(this, channel, allowExpression, axis)
52+
if (!before) return false
53+
54+
afterTime = roundToNth(Timeline.time + 0.05, 20)
55+
Timeline.time = afterTime
56+
after = context.orignalInterpolate.call(this, channel, allowExpression, axis)
57+
if (!after) return false
58+
} else {
59+
afterTime = Timeline.time
60+
after = context.orignalInterpolate.call(this, channel, allowExpression, axis)
61+
if (!after) return false
62+
63+
beforeTime = roundToNth(Timeline.time - 0.05, 20)
64+
Timeline.time = beforeTime
65+
before = context.orignalInterpolate.call(this, channel, allowExpression, axis)
66+
if (!before) return false
67+
}
68+
const diff = (actualTime - beforeTime) / (afterTime - beforeTime)
69+
70+
const result: ArrayVector3 = [
71+
Math.lerp(before[0], after[0], diff),
72+
Math.lerp(before[1], after[1], diff),
73+
Math.lerp(before[2], after[2], diff),
74+
]
75+
// console.log(diff)
76+
77+
return result
78+
79+
// context.orignalInterpolate.call(this, channel, allowExpression, axis)
80+
} finally {
81+
Timeline.time = actualTime
82+
}
83+
}
84+
85+
return context
86+
},
87+
context => {
88+
context.orignalInterpolate = BoneAnimator.prototype.interpolate
89+
}
90+
)

src/mods/keyframeMod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ createBlockbenchMod(
158158
): number {
159159
if (!isCurrentFormat())
160160
return context.originalGetLerp.call(this, other, axis, amount, allowExpression)
161+
161162
const easing = other.easing || 'linear'
162163
let easingFunc = easingFunctions[easing]
163164
if (hasArgs(easing)) {

src/systems/animationRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export function updatePreview(animation: _Animation, time: number) {
244244
Animator.resetLastValues()
245245
scene.updateMatrixWorld()
246246
if (animation.effects) animation.effects.displayFrame()
247-
Blockbench.dispatchEvent('display_animation_frame')
247+
// Blockbench.dispatchEvent('display_animation_frame')
248248
}
249249

250250
export function renderAnimation(animation: _Animation, rig: IRenderedRig) {

0 commit comments

Comments
 (0)