Skip to content

Commit d09f520

Browse files
committed
Fix inaccurate animation duration
Animations in glTF are not required to start at zero time, so we need to subtract the first time from the last time.
1 parent d6a3a4e commit d09f520

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Sources/GateEngine/Resources/Import & Export/Importers/GLTransmissionFormat.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ extension GLTransmissionFormat: SkeletalAnimationImporter {
786786
addChildren(gltfNode: rootNode, parentJoint: rootJoint)
787787
}
788788

789-
var timeMax: Float = -1_000_000_000
789+
var timeMax: Float = .nan
790+
var timeMin: Float = .nan
790791

791792
for channel in animation.channels {
792793
let jointAnimation = jointAnimation(forTarget: channel.target.node)
@@ -849,10 +850,20 @@ extension GLTransmissionFormat: SkeletalAnimationImporter {
849850
break
850851
}
851852

852-
timeMax = .maximum(times.max()!, timeMax)
853+
if let max = times.max() {
854+
timeMax = .maximum(max, timeMax)
855+
}
856+
if let min = times.min() {
857+
timeMin = .minimum(min, timeMin)
858+
}
859+
}
860+
861+
var duration = timeMax - timeMin
862+
if duration.isFinite == false {
863+
duration = 0
853864
}
854865

855-
return SkeletalAnimationBackend(name: animation.name, duration: timeMax, animations: animations)
866+
return SkeletalAnimationBackend(name: animation.name, duration: duration, animations: animations)
856867
}
857868
}
858869

0 commit comments

Comments
 (0)