From f51b5be167c3744b060bf4d9a8211b2045c15102 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 5 Aug 2015 11:00:40 -0700 Subject: [PATCH] Simplify fade normalization, eliminating a loop. --- libraries/animation/src/Rig.cpp | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/libraries/animation/src/Rig.cpp b/libraries/animation/src/Rig.cpp index c01e60abd9..43b2892cbf 100644 --- a/libraries/animation/src/Rig.cpp +++ b/libraries/animation/src/Rig.cpp @@ -464,35 +464,25 @@ void Rig::updateAnimations(float deltaTime, glm::mat4 parentTransform) { } } } - // collect the remaining fade data - float fadeSum = 0.0f; - std::vector normalizedFades(_runningAnimations.count()); - int animationIndex = 0; + // sum the remaining fade data + float fadeTotal = 0.0f; foreach (const AnimationHandlePointer& handle, _runningAnimations) { - float fade = handle->getFade(); - normalizedFades[animationIndex++] = fade; - fadeSum += fade; + fadeTotal += handle->getFade(); } - // normalize our copy of the fade data - for (int i = 0; i < _runningAnimations.count(); i++) { - normalizedFades[i] /= fadeSum; - } - // set the mix based on the normalized fade data - animationIndex = 0; - fadeSum = 0.0f; + float fadeSumSoFar = 0.0f; foreach (const AnimationHandlePointer& handle, _runningAnimations) { handle->setPriority(1.0f); - float normalizedFade = normalizedFades[animationIndex++]; + float normalizedFade = handle->getFade() / fadeTotal; // simulate() will blend each animation result into the result so far, based on the pairwise mix at at each step. // i.e., slerp the 'mix' distance from the result so far towards this iteration's animation result. // The formula here for mix is based on the idea that, at each step: // fadeSum is to normalizedFade, as (1 - mix) is to mix - // i.e., fadeSum/normalizedFade = (1 - mix)/mix + // i.e., fadeSumSoFar/normalizedFade = (1 - mix)/mix // Then we solve for mix. // Sanity check: For the first animation, fadeSum = 0, and the mix will always be 1. // Sanity check: For equal blending, the formula is equivalent to mix = 1 / nAnimationsSoFar++ - float mix = 1.0f / ((fadeSum / normalizedFade) + 1.0f); - fadeSum += normalizedFade; + float mix = 1.0f / ((fadeSumSoFar / normalizedFade) + 1.0f); + fadeSumSoFar += normalizedFade; handle->setMix(mix); handle->simulate(deltaTime); }