Skip to content
Open
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
38 changes: 38 additions & 0 deletions src/animation/Clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,44 @@ class Clip{
return clip;
}

static fromThree( anim: any, arm: any ) : Clip {
// Convert from THREE.AnimationClip to OSSOS.Clip (needs armature for bone names)
const clip = new Clip();
clip.name = anim.name;
clip.duration = anim.duration;
clip.frameCount = anim.tracks[0].times.length;
clip.timeStamps = [anim.tracks[0].times]; // Limit: Only one timestamp per clip

// Add tracks
clip.tracks = [];
let track : ITrack; // Animator Track
for ( const t of anim.tracks ) {

switch ( t.constructor.name ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encountered issues with the minified bundle when using constructor.name, as the bundler sometimes changes object names, leading to crashes. A possible solution is to adjust the bundler settings and use Terser. However, if there's a way to avoid relying on constructor.name, it might prevent this issue. Just wanted to share in case it's helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The safest solution I think might be to use instanceof conditions. There is only two Track types in ossos but more in 3js. Something it should be easy to just do

if( t instanceof VectorKeyframeTrack ) track = new Vec3Track();
else if( t instanceof QuaternionKeyframeTrack ) track = new QuatTrack();
else continue;

I've rebuilt the animation system for the next version of ossos, So there are big changes in alot of places.
https://github.com/sketchpunklabs/ossos/tree/ossos_next

case "VectorKeyframeTrack": track = new Vec3Track(); break;
case "QuaternionKeyframeTrack": track = new QuatTrack(); break;
default: continue; break;
}

switch( t.getInterpolation() ){
case 2300 : track.setInterpolation( ELerp.Step ); break;
case 2301 : track.setInterpolation( ELerp.Linear ); break;
case 2302 : track.setInterpolation( ELerp.Cubic ); break;
}

track.values = t.values;
track.timeStampIndex = 0;

// Find the bone index
const boneName = t.name.split( '.' )[0];
track.boneIndex = arm.names.get( boneName ) ?? -2;

clip.tracks.push( track )
}

return clip
}

//#endregion
}

Expand Down