ThreeJS - 如何在动画中设置当前时间

nul*_*ull 7 javascript three.js

我在ThreeJS中使用皮肤/骨骼动画.我有一个动画,我希望能够前后移动,并跳转到其中的不同位置,而不是通常的循环行为.

像这样创建动画,如下例所示:

var animation = new THREE.Animation( mesh, geometry.animation.name );
Run Code Online (Sandbox Code Playgroud)

我尝试使用负增量更新动画,以及直接设置animation.currentTime:

animation.currentTime = animationLocation;
Run Code Online (Sandbox Code Playgroud)

这些似乎只有在我向前推进时才有效,但是如果我向后移动动画会中断并且我收到错误:

THREE.Animation.update: Warning! Scale out of bounds: ... on bone ... 
Run Code Online (Sandbox Code Playgroud)

实际上没有错误的一件事是调用stop()然后play()使用新的开始时间:

animation.stop();
animation.play( true, animationLocation );
Run Code Online (Sandbox Code Playgroud)

...但是,当我看到这些函数实际上在做什么时,它们涉及许多函数调用,循环,重置转换等.这似乎是一种可怕的方式,即使它作为一个黑客工作.

可能这个功能还没有存在,在这种情况下我会尝试挖掘并创建一个执行最少量工作的函数,但我希望还有另一种我没有找到的方法.

有人能帮忙吗?

[UPDATE]

作为我的进展的最新消息,我将发布我此时的最佳解决方案......

我拿出的内容stop()play()功能,并剥离出尽我所能,作出关于特定值的一些假设已经已经被设置"播放()".

这看起来似乎不是最好的方法,但它的工作量比通过调用stop()那么少play().

这是我能够把它归结为:

THREE.Animation.prototype.gotoTime = function( time ) {

    //clamp to duration of the animation:
    time = THREE.Math.clamp( time, 0, this.length );

    this.currentTime = time;

    // reset key cache
    var h, hl = this.hierarchy.length,
        object;

    for ( h = 0; h < hl; h ++ ) {

        object = this.hierarchy[ h ];

        var prevKey = object.animationCache.prevKey;
        var nextKey = object.animationCache.nextKey;

        prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
        prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
        prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];

        nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
        nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
        nextKey.scl = this.getNextKeyWith( "scl", h, 1 );

    }

    //isPlaying must be true for update to work due to "early out"
    //so remember the current play state:
    var wasPlaying = this.isPlaying;
    this.isPlaying = true; 

    //update with a delta time of zero:
    this.update( 0 );

    //reset the play state:
    this.isPlaying = wasPlaying;

}
Run Code Online (Sandbox Code Playgroud)

函数在有用性方面的主要限制是你不能从一个任意时间插入另一个时间.你基本上可以在动画中擦洗.

Val*_*lay 3

您可以使用THREE.Clock和分配startTime, oldTime, elapsedTime