From 03f6a726fb99e9f1c26a2742f548a4058c977943 Mon Sep 17 00:00:00 2001 From: JannisX11 Date: Sun, 27 Sep 2020 10:52:31 +0200 Subject: [PATCH] Catmull rom interpolation preview --- js/animations/animation.js | 35 ++++++++++++++++++++++++----------- js/animations/keyframe.js | 23 ++++++++++++++++++++--- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/js/animations/animation.js b/js/animations/animation.js index 53f0e307..fc994d6d 100644 --- a/js/animations/animation.js +++ b/js/animations/animation.js @@ -878,17 +878,30 @@ class BoneAnimator extends GeneralAnimator { } else if (!before && !after) { // } else { - let alpha = Math.lerp(before.time, after.time, time) - if (Blockbench.hasFlag('no_interpolations')) { - alpha = Math.round(alpha) - } - result = [ - before.getLerp(after, 'x', alpha, allow_expression), - before.getLerp(after, 'y', alpha, allow_expression), - before.getLerp(after, 'z', alpha, allow_expression) - ] - if (before.isQuaternion && after.isQuaternion) { - result[3] = before.getLerp(after, 'q', alpha, allow_expression) + let no_interpolations = Blockbench.hasFlag('no_interpolations') + + if (no_interpolations || (before.interpolation == Keyframe.interpolation.linear && after.interpolation == Keyframe.interpolation.linear)) { + let alpha = Math.lerp(before.time, after.time, time) + if (no_interpolations) { + alpha = Math.round(alpha) + } + result = [ + before.getLerp(after, 'x', alpha, allow_expression), + before.getLerp(after, 'y', alpha, allow_expression), + before.getLerp(after, 'z', alpha, allow_expression) + ] + } else { + + let sorted = this[channel].slice().sort((kf1, kf2) => (kf1.time - kf2.time)); + let before_index = sorted.indexOf(before); + let before_plus = sorted[before_index-1]; + let after_plus = sorted[before_index+2]; + + result = [ + before.getCatmullromLerp(before_plus, before, after, after_plus, 'x'), + before.getCatmullromLerp(before_plus, before, after, after_plus, 'y'), + before.getCatmullromLerp(before_plus, before, after, after_plus, 'z'), + ] } } if (result && result.type === 'keyframe') { diff --git a/js/animations/keyframe.js b/js/animations/keyframe.js index f42083e8..3797d306 100644 --- a/js/animations/keyframe.js +++ b/js/animations/keyframe.js @@ -30,19 +30,19 @@ class KeyframeDataPoint { if (this.instructions != undefined) Merge.string(this, data, 'instructions') } get x_string() { - return typeof this.x == 'number' ? trimFloatNumber(this.x) : this.x; + return typeof this.x == 'number' ? trimFloatNumber(this.x) || '0' : this.x; } set x_string(val) { this.x = val; } get y_string() { - return typeof this.y == 'number' ? trimFloatNumber(this.y) : this.y; + return typeof this.y == 'number' ? trimFloatNumber(this.y) || '0' : this.y; } set y_string(val) { this.y = val; } get z_string() { - return typeof this.z == 'number' ? trimFloatNumber(this.z) : this.z; + return typeof this.z == 'number' ? trimFloatNumber(this.z) || '0' : this.z; } set z_string(val) { this.z = val; @@ -173,6 +173,19 @@ class Keyframe { return calc + (other.calc(axis, other_data_point) - calc) * amount; } } + getCatmullromLerp(before_plus, before, after, after_plus, axis) { + var vectors = []; + + if (before_plus) vectors.push(new THREE.Vector2(before_plus.time, before_plus.calc(axis, 0))) + if (before) vectors.push(new THREE.Vector2(before.time, before.calc(axis, 0))) + if (after) vectors.push(new THREE.Vector2(after.time, after.calc(axis, 0))) + if (after_plus) vectors.push(new THREE.Vector2(after_plus.time, after_plus.calc(axis, 0))) + + var curve = new THREE.SplineCurve(vectors); + let time = Math.lerp(vectors[0].x, vectors.last().x, Timeline.time) + + return curve.getPoint(time).y; + } getArray(data_point = 0) { var arr = [ this.get('x', data_point), @@ -403,6 +416,10 @@ class Keyframe { new Property(Keyframe, 'number', 'color', {default: -1}) new Property(Keyframe, 'string', 'interpolation', {default: 'linear'}) Keyframe.selected = []; + Keyframe.interpolation = { + linear: 'linear', + catmullrom: 'catmullrom', + } // Misc Functions function updateKeyframeValue(axis, value, data_point) {