Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SkeletonUtils: added trim #29424

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions examples/jsm/utils/SkeletonUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,31 @@ function retargetClip( target, source, clip, options = {} ) {
name;

mixer.clipAction( clip ).play();
mixer.update( 0 );

// trim

let start = 0, end = numFrames;

if ( options.trim !== undefined ) {

start = Math.round( options.trim[ 0 ] * options.fps );
end = Math.min( Math.round( options.trim[ 1 ] * options.fps ), numFrames ) - start;

mixer.update( options.trim[ 0 ] );

} else {

mixer.update( 0 );

}

source.updateMatrixWorld();

for ( let i = 0; i < numFrames; ++ i ) {
//

for ( let frame = 0; frame < end; ++ frame ) {

const time = i * delta;
const time = frame * delta;

retarget( target, source, options );

Expand All @@ -247,15 +265,15 @@ function retargetClip( target, source, clip, options = {} ) {
if ( ! boneData.pos ) {

boneData.pos = {
times: new Float32Array( numFrames ),
values: new Float32Array( numFrames * 3 )
times: new Float32Array( end ),
values: new Float32Array( end * 3 )
};

}

if ( options.useFirstFramePosition ) {

if ( i === 0 ) {
if ( frame === 0 ) {

positionOffset = bone.position.clone();

Expand All @@ -265,30 +283,30 @@ function retargetClip( target, source, clip, options = {} ) {

}

boneData.pos.times[ i ] = time;
boneData.pos.times[ frame ] = time;

bone.position.toArray( boneData.pos.values, i * 3 );
bone.position.toArray( boneData.pos.values, frame * 3 );

}

if ( ! boneData.quat ) {

boneData.quat = {
times: new Float32Array( numFrames ),
values: new Float32Array( numFrames * 4 )
times: new Float32Array( end ),
values: new Float32Array( end * 4 )
};

}

boneData.quat.times[ i ] = time;
boneData.quat.times[ frame ] = time;

bone.quaternion.toArray( boneData.quat.values, i * 4 );
bone.quaternion.toArray( boneData.quat.values, frame * 4 );

}

}

if ( i === numFrames - 2 ) {
if ( frame === end - 2 ) {

// last mixer update before final loop iteration
// make sure we do not go over or equal to clip duration
Expand Down
3 changes: 3 additions & 0 deletions examples/webgpu_animation_retargeting.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@
// use ( 0, 1, 0 ) to ignore xz hip movement.
//hipInfluence: new THREE.Vector3( 0, 1, 0 ),

// specify an animation range in seconds.
//trim: [ 3.0, 4.0 ],

// preserve the scale of the target model
scale: 1 / targetModel.scene.scale.y,

Expand Down