簡體   English   中英

ThreeJS - 如何在動畫中設置當前時間

[英]ThreeJS - how to set current time in Animation

我在ThreeJS中使用皮膚/骨骼動畫。 我有一個動畫,我希望能夠前后移動,並跳轉到其中的不同位置,而不是通常的循環行為。

像這樣創建動畫,如下例所示:

var animation = new THREE.Animation( mesh, geometry.animation.name );

我嘗試使用負增量更新動畫,以及直接設置animation.currentTime:

animation.currentTime = animationLocation;

這些似乎只有在我向前推進時才有效,但是如果我向后移動動畫會中斷並且我收到錯誤:

THREE.Animation.update: Warning! Scale out of bounds: ... on bone ... 

實際上沒有錯誤的一件事是調用stop()然后使用新的開始時間play()

animation.stop();
animation.play( true, animationLocation );

...但是,當我看到這些函數實際上在做什么時,它們涉及許多函數調用,循環,重置變換等。這似乎是一種可怕的方式,即使它作為一個黑客工作。

可能這個功能還沒有存在,在這種情況下我會嘗試挖掘並創建一個執行最少量工作的函數,但我希望還有另一種我沒有找到的方法。

有人能幫忙嗎?

[UPDATE]

作為我的進展的最新消息,我將發布我此時的最佳解決方案......

我拿出了stop()play()函數的內容,並刪除了我能做的一切,對已經由'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;

}

函數在有用性方面的主要限制是你不能從一個任意時間插入到另一個時間。 你基本上可以在動畫中擦洗。

您可以使用THREE.Clock並分配startTimeoldTimeelapsedTime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM