//Sets the animations playback speed as close as possible to the desired playback speed public ModelAnimation setAnimationPlaybackAsCloseAsPossible(ModelAnimation theAnimation, float pBackSpeed) { if (pBackSpeed <= 0) { Debug.Log("Resolution should be greater than 0"); return(theAnimation); } ModelAnimation equalizedAnimation = new ModelAnimation(); List <AnimationFrame> newFrameList = new List <AnimationFrame>(); float averageAnimationFrameLength = 100 / theAnimation.getAveragePlaybackSpeed(); //get closest float that divides evenly into animation length, rounds down and increases number of frames, gets better resolut float actualPbackSpeed = pBackSpeed - ((Mathf.CeilToInt(averageAnimationFrameLength) % (int)pBackSpeed) / 2); int numberOfFrames = (int)(averageAnimationFrameLength / actualPbackSpeed); //initialize animation name, transforms, playback speed, and priority equalizedAnimation.name = theAnimation.name; equalizedAnimation.modelTransforms = theAnimation.modelTransforms; equalizedAnimation.playbackSpeed = actualPbackSpeed; equalizedAnimation.priority = theAnimation.priority; //Add origin frame AnimationFrame newFrame = new AnimationFrame(equalizedAnimation.modelTransforms.Length, "Origin Frame"); newFrame.positionStates = theAnimation.frames[0].positionStates; newFrame.rotationStates = theAnimation.frames[0].rotationStates; newFrameList.Add(newFrame); for (int f = 1; f < numberOfFrames; ++f) { newFrame = new AnimationFrame(equalizedAnimation.modelTransforms.Length, "Frame " + f); newFrame.positionStates = theAnimation.getPositionsForPercentComplete((float)f / (numberOfFrames - 1)); newFrame.rotationStates = theAnimation.getRotationsForPercentComplete((float)f / (numberOfFrames - 1)); newFrameList.Add(newFrame); } equalizedAnimation.frames = newFrameList.ToArray(); return(equalizedAnimation); }
//equalize animations and add or remove frames so that each frame takes the same number of cycles public ModelAnimation equalizeAnimation(ModelAnimation theAnimation, int resolution = 1) { if (resolution <= 0) { Debug.Log("Resolution should be greater than 0"); return(theAnimation); } ModelAnimation equalizedAnimation = new ModelAnimation(); List <AnimationFrame> newFrameList = new List <AnimationFrame>(); //initialize animation name, transforms, playback speed, and priority equalizedAnimation.name = theAnimation.name; equalizedAnimation.modelTransforms = theAnimation.modelTransforms; equalizedAnimation.playbackSpeed = theAnimation.getAveragePlaybackSpeed() * resolution; equalizedAnimation.priority = theAnimation.priority; int numberOfFrames = theAnimation.frames.Length * resolution; //Add origin frame AnimationFrame newFrame = new AnimationFrame(equalizedAnimation.modelTransforms.Length, "Origin Frame"); newFrame.positionStates = theAnimation.frames[0].positionStates; newFrame.rotationStates = theAnimation.frames[0].rotationStates; newFrameList.Add(newFrame); for (int f = 1; f < numberOfFrames; ++f) { newFrame = new AnimationFrame(equalizedAnimation.modelTransforms.Length, "Frame " + f); newFrame.positionStates = theAnimation.getPositionsForPercentComplete((float)f / (numberOfFrames - 1)); newFrame.rotationStates = theAnimation.getRotationsForPercentComplete((float)f / (numberOfFrames - 1)); newFrameList.Add(newFrame); } equalizedAnimation.frames = newFrameList.ToArray(); return(equalizedAnimation); }