VAT - How to start animation at offset 0?

Hello,

I baked many animations in a texture, when I play an animation I always set the animation offset to 0, but I observe that the animation offset depends on time.

I challenged ChatGPT and the IA told me this is the expected behavior since the first frame is computed with the uniform time injected in shader with this formula :

float frameIndex = mod(time + offset, durationInSeconds) * fps + startFrame;

So I tried many “hacks” by using deltaTime, performance.now, etc to compute an offset to start the animation from the beginning but without success.

How can we force a VAT animation to start at the offset 0 no matter what was playing before ?

Context : thin instances + VAT, baked animations do not have the same frame count (I hope this has not impact)

Illustration in video :

When I click on a button, I set a animation :

    this.thinInstancesAnimParameters.set(
      animationParams,
      thinIndex * 4);
    this.thinMeshes.forEach((m: Mesh): void => {
      m.thinInstanceSetBuffer(
        "bakedVertexAnimationSettingsInstanced",
        this.thinInstancesAnimParameters,
        4);

And animationParams offset is always 0.

But if you look carefully (easier to check in the walk animation), the animation does not always start at the frame 0 of the animation (at 0:16 for example, animal paw is right instead of left)

You can try to set BakedVertexAnimationManager.time = 0 when you start a new animation?

2 Likes

Totally forgot this manager, this is the solution thank you !

Babylon team 1 - ChatGPT 0 :grin:

2 Likes

@Evgeni_Popov Sadly I just noticed that setting BakedVertexAnimationManager.time = 0 impacts all currents played animations.
I suppose I could create an instance of BakedVertexAnimationManager for each thin instance but I don’t really like this idea…

Do you see another solution ?

See my answer here and the corresponding PG: the function computeOffsetInAnim() should be what you need.

1 Like

Nice thank you, I initially tried these kind of fomula but without success :sweat_smile:

The solution given in playground :

// This method will compute the VAT offset to use so that the animation starts at frame #0 for VAT time = time passed as 3rd parameter
computeOffsetInAnim(fromFrame, toFrame, time, fps = 60) {
  const totalFrames = toFrame - fromFrame + 1;
  const t = time * fps / totalFrames;
  const frame = Math.floor((t - Math.floor(t)) * totalFrames);

  return totalFrames - frame;
}

Yes, I think a documentation page in our documentation repository on VAT in real projects would be a nice addition, if anyone is up to the challenge!

1 Like
3 Likes