Utsira / RealityMorpher

Brings Morph Target/ Shape Key/ Blend Shape animations to RealityKit
MIT License
25 stars 2 forks source link

setTargetWeights does not work with a duration of 0 #2

Closed Reality-Dev closed 8 months ago

Reality-Dev commented 11 months ago

Using this method: public mutating func setTargetWeights(_ targetWeights: MorphWeights, duration: TimeInterval = 0) And passing in a duration of 0 does not update the Entity's mesh.

Reality-Dev commented 11 months ago

Proposed solution:

struct LinearAnimator: MorphAnimating {
    private var timeElapsed: TimeInterval = .zero
    private let origin: MorphWeights
    private let target: MorphWeights
    private let duration: TimeInterval

    /// Used for when duration == 0, so that one update loop is run to apply the final weights to the mesh.
    private var hasUpdated = false

    init(origin: MorphWeights, target: MorphWeights, duration: TimeInterval) {
        self.origin = origin
        self.target = target
        self.duration = duration
    }

    mutating func update(with deltaTime: TimeInterval) -> MorphEvent {
        if timeElapsed >= duration {

            if (hasUpdated || duration > 0) {

                return MorphEvent(status: .completed, weights: target)

            } else {

                hasUpdated = true

                // Perform one update loop to apply the final weights to the mesh.
                return MorphEvent(status: .running, weights: target)
            }
        }

        timeElapsed += deltaTime

        let value = mix(origin.values, target.values, t: Float(timeElapsed / duration))

        if !hasUpdated { hasUpdated = true }

        return MorphEvent(status: .running, weights: MorphWeights(values: value))
    }

    func mix(_ x: simd_float8, _ y: simd_float8, t: Float) -> simd_float8 {
        let valueLow = simd.mix(origin.values.lowHalf, target.values.lowHalf, t: t)
        let valueHigh = simd.mix(origin.values.highHalf, target.values.highHalf, t: t)
        return .init(lowHalf: valueLow, highHalf: valueHigh)
    }
}
Utsira commented 8 months ago

Thank you for reporting this, and sorry it took me so long to get round to looking at it. Should be fixed now (release 0.0.2), please re-open this issue if you encounter issues