public AnimationItem Add(string animationBaseName, bool allowRandomAnimationNumber, bool loop) { //remove from removedItemsForBlending if (blendingTime != 0) { for (int n = 0; n < removedItemsForBlending.Count; n++) { AnimationItem removedItem = removedItemsForBlending[n]; if (removedItem.AnimationBaseName == animationBaseName) { removedItem.animationState.Enable = false; removedItemsForBlending.RemoveAt(n); n--; continue; } } } string animationName = animationBaseName; if (allowRandomAnimationNumber) { int number = GetRandomAnimationNumber(animationBaseName, true); if (number != 1) { animationName += number.ToString(); } } MeshObject.AnimationState animationState = meshObject.GetAnimationState(animationName); if (animationState == null) { return(null); } animationState.Loop = loop; animationState.TimePosition = 0; animationState.Enable = true; AnimationItem item = new AnimationItem(this, animationBaseName, allowRandomAnimationNumber, loop); if (blendingTime != 0) { item.blendingWeightCoefficient = .001f; } else { item.blendingWeightCoefficient = 1; } item.animationState = animationState; activeItems.Add(item); UpdateAnimationStatesWeights(); return(item); }
public void DoRenderFrame() { float delta = RendererWorld.Instance.FrameRenderTimeStep; for (int n = 0; n < activeItems.Count; n++) { AnimationItem item = activeItems[n]; MeshObject.AnimationState animationState = item.animationState; //time progress animationState.AddTime(item.Velocity * delta); //has ended? if (!item.Loop) { if (animationState.TimePosition + blendingTime * 2 + .001f >= item.animationState.Length) { Remove(item); n--; continue; } } //change random animation if (item.Loop && item.AllowRandomAnimationNumber) { //detect rewind if (animationState.TimePosition < item.lastTimePosition) { string animationName = item.AnimationBaseName; int number = GetRandomAnimationNumber(animationName, true); if (number != 1) { animationName += number.ToString(); } MeshObject.AnimationState newAnimationState = meshObject.GetAnimationState(animationName); if (newAnimationState == null) { Log.Fatal("MeshObjectAnimationController: DoRenderFrame: " + "newAnimationState == null."); } animationState.Enable = false; newAnimationState.Loop = animationState.Loop; newAnimationState.TimePosition = animationState.TimePosition; newAnimationState.Weight = animationState.Weight; newAnimationState.Enable = true; item.animationState = newAnimationState; } } //update weight for blending if (blendingTime != 0) { item.blendingWeightCoefficient += delta / blendingTime; if (item.blendingWeightCoefficient > 1) { item.blendingWeightCoefficient = 1; } } item.lastTimePosition = animationState.TimePosition; } for (int n = 0; n < removedItemsForBlending.Count; n++) { AnimationItem item = removedItemsForBlending[n]; item.blendingWeightCoefficient -= delta / blendingTime; if (item.blendingWeightCoefficient <= 0) { item.animationState.Enable = false; removedItemsForBlending.RemoveAt(n); n--; continue; } } UpdateAnimationStatesWeights(); }