public static AnimationFile ExtractPartOfSkeleton(GameSkeleton skeleton, string skeletonName, int[] bones)
        {
            // Header
            var animFile = new AnimationFile();

            animFile.Header.SkeletonName = skeletonName;
            animFile.Header.AnimationTotalPlayTimeInSec = 0.1f;
            animFile.Bones = new AnimationFile.BoneInfo[bones.Length];

            CreateBoneTable(skeleton, bones, ref animFile);

            // Create the keyframe
            animFile.DynamicFrames = new List <AnimationFile.Frame>();
            var frame = new AnimationFile.Frame();

            animFile.DynamicFrames.Add(frame);

            // Populate the keyframe
            for (int i = 0; i < bones.Length; i++)
            {
                var originalBoneIndex = bones[i];

                if (i == 0)
                {
                    var worldTrans = skeleton.GetWorldTransform(originalBoneIndex);
                    var res        = worldTrans.Decompose(out Vector3 scale, out var rot, out var trans);
                    frame.Transforms.Add(new RmvVector3(trans.X, trans.Y, trans.Z));
                    frame.Quaternion.Add(new RmvVector4(rot.X, rot.Y, rot.Z, rot.W));
                }
                else
                {
                    var trans = skeleton.Translation[originalBoneIndex];
                    var rot   = skeleton.Rotation[originalBoneIndex];

                    frame.Transforms.Add(new RmvVector3(trans.X, trans.Y, trans.Z));
                    frame.Quaternion.Add(new RmvVector4(rot.X, rot.Y, rot.Z, rot.W));
                }
            }

            return(animFile);
            //var sample = AnimationSampler.Sample(0, 0, skeleton, new List<AnimationClip>() { animation }, true, true);
        }
        public static AnimationFrame Sample(int frameIndex, float frameIterpolation, GameSkeleton skeleton, List <AnimationClip> animationClips)
        {
            try
            {
                if (skeleton == null)
                {
                    return(null);
                }

                var currentFrame = skeleton.CreateAnimationFrame();

                if (animationClips != null)
                {
                    foreach (var animation in animationClips)
                    {
                        ApplyAnimation(animation.StaticFrame, null, 0, currentFrame, animation.RotationMappings, animation.TranslationMappings, AnimationBoneMappingType.Static);
                    }

                    if (animationClips.Any())
                    {
                        if (animationClips[0].DynamicFrames.Count > frameIndex)
                        {
                            var currentFrameKeys = GetKeyFrameFromIndex(animationClips[0].DynamicFrames, frameIndex);
                            var nextFrameKeys    = GetKeyFrameFromIndex(animationClips[0].DynamicFrames, frameIndex + 1);
                            ApplyAnimation(currentFrameKeys, nextFrameKeys, frameIterpolation, currentFrame, animationClips[0].RotationMappings, animationClips[0].TranslationMappings, AnimationBoneMappingType.Dynamic);

                            // Apply skeleton scale
                            for (int i = 0; i < currentFrame.BoneTransforms.Count(); i++)
                            {
                                currentFrame.BoneTransforms[i].Scale = animationClips[0].DynamicFrames[0].Scale[i];
                            }
                        }
                    }
                }

                for (int i = 0; i < currentFrame.BoneTransforms.Count(); i++)
                {
                    Quaternion rotation    = currentFrame.BoneTransforms[i].Rotation;
                    Vector3    translation = currentFrame.BoneTransforms[i].Translation;
                    currentFrame.BoneTransforms[i].WorldTransform =
                        Matrix.CreateScale(currentFrame.BoneTransforms[i].Scale) *
                        Matrix.CreateFromQuaternion(rotation) *
                        Matrix.CreateTranslation(translation);

                    var parentindex = currentFrame.BoneTransforms[i].ParentBoneIndex;
                    if (parentindex == -1)
                    {
                        //var scale = Matrix.CreateScale(0.1f);
                        //currentFrame.BoneTransforms[i].WorldTransform = (scale * currentFrame.BoneTransforms[i].WorldTransform);
                        continue;
                    }

                    currentFrame.BoneTransforms[i].WorldTransform = currentFrame.BoneTransforms[i].WorldTransform * currentFrame.BoneTransforms[parentindex].WorldTransform;
                }

                for (int i = 0; i < skeleton.BoneCount; i++)
                {
                    var inv = Matrix.Invert(skeleton.GetWorldTransform(i));
                    currentFrame.BoneTransforms[i].WorldTransform = Matrix.Multiply(inv, currentFrame.BoneTransforms[i].WorldTransform);
                }
                return(currentFrame);
            }
            catch (Exception e)
            {
                ILogger logger = Logging.Create <AnimationSampler>();
                logger.Error(e.Message);
                throw;
            }
        }
Example #3
0
        public Matrix GetSkeletonAnimatedWorld(GameSkeleton gameSkeleton, int boneIndex)
        {
            Matrix output = gameSkeleton.GetWorldTransform(boneIndex) * BoneTransforms[boneIndex].WorldTransform;

            return(output);
        }