Skip to content

Commit

Permalink
Merge pull request #65 from ux3d/fix/BoxAnimationsShouldBeInSync
Browse files Browse the repository at this point in the history
Fix sync issue inside each individual animation (GSVN-157)
  • Loading branch information
UX3D-haertl committed Jan 14, 2021
2 parents edf2e36 + 09e7439 commit df09445
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
24 changes: 20 additions & 4 deletions src/gltf/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class gltfAnimation extends GltfObject

// not gltf
this.interpolators = [];
this.maxTime = 0;
}

fromJson(jsonAnimation)
Expand Down Expand Up @@ -44,6 +45,21 @@ class gltfAnimation extends GltfObject
return;
}

if(this.maxTime == 0)
{
for(let i = 0; i < this.channels.length; ++i)
{
const channel = this.channels[i];
const sampler = this.samplers[channel.sampler];
const input = gltf.accessors[sampler.input].getDeinterlacedView(gltf);
const max = input[input.length - 1];
if(max > this.maxTime)
{
this.maxTime = max;
}
}
}

for(let i = 0; i < this.interpolators.length; ++i)
{
const channel = this.channels[i];
Expand All @@ -55,18 +71,18 @@ class gltfAnimation extends GltfObject
switch(channel.target.path)
{
case InterpolationPath.TRANSLATION:
node.applyTranslation(interpolator.interpolate(gltf, channel, sampler, totalTime, 3));
node.applyTranslation(interpolator.interpolate(gltf, channel, sampler, totalTime, 3, this.maxTime));
break;
case InterpolationPath.ROTATION:
node.applyRotation(interpolator.interpolate(gltf, channel, sampler, totalTime, 4));
node.applyRotation(interpolator.interpolate(gltf, channel, sampler, totalTime, 4, this.maxTime));
break;
case InterpolationPath.SCALE:
node.applyScale(interpolator.interpolate(gltf, channel, sampler, totalTime, 3));
node.applyScale(interpolator.interpolate(gltf, channel, sampler, totalTime, 3, this.maxTime));
break;
case InterpolationPath.WEIGHTS:
{
const mesh = gltf.meshes[node.mesh];
mesh.weights = interpolator.interpolate(gltf, channel, sampler, totalTime, mesh.weights.length);
mesh.weights = interpolator.interpolate(gltf, channel, sampler, totalTime, mesh.weights.length, this.maxTime);
break;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/gltf/interpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class gltfInterpolator
this.prevKey = 0;
}

interpolate(gltf, channel, sampler, t, stride)
interpolate(gltf, channel, sampler, t, stride, maxTime)
{
const input = gltf.accessors[sampler.input].getDeinterlacedView(gltf);
const output = gltf.accessors[sampler.output].getDeinterlacedView(gltf);
Expand All @@ -96,8 +96,9 @@ class gltfInterpolator
}

// Wrap t around, so the animation loops.
// Make sure that t is never earlier than the first keyframe.
t = Math.max(t % input[input.length - 1], input[0]);
// Make sure that t is never earlier than the first keyframe and never later then the last keyframe.
t = t % maxTime;
t = clamp(t, input[0], input[input.length - 1]);

if (this.prevT > t)
{
Expand Down

0 comments on commit df09445

Please sign in to comment.