コード例 #1
0
        public static List <Bone> GetBoneHierarchy(CoreCollada.Collada file, Node current = null)
        {
            var bones = new List <Bone>();

            var children = (current == null) ?
                           file.LibraryVisualScenes[0].Nodes.Where(a => a.Matrix != null).Take(1)
                : current.Nodes;

            foreach (var node in children)
            {
                var bone = new Bone()
                {
                    Parent            = (current == null) ? "" : current.SId,
                    JointMatrix       = node.MatrixValue.TransposeMatrix(),
                    InverseBindMatrix = node.MatrixValue.TransposeMatrix().MatrixInverse(),
                    Id       = node.SId,
                    Name     = node.Name,
                    Children = node.Nodes.Select(a => a.SId).ToList()
                };

                bone.Keyframes = GetBoneKeyFrames(bone, file);

                bones.Add(bone);

                bones.AddRange(GetBoneHierarchy(file, node));
            }

            return(bones);
        }
コード例 #2
0
        }// Constructor()

        public Animator(CoreCollada.Collada file)
        {
            var bones = Bone.GetBoneHierarchy(file);

            var boneCurveNodes = new SortedList <string, List <CoreFBX.FBX.Animation.FBXAnimCurveNode> >();

            AnimationKeys = new Dictionary <string, List <AnimationKey> >();
            PoseKeys      = new Dictionary <string, AnimationKey>();

            foreach (var bone in bones)
            {
                AnimationKeys.Add(bone.Id, bone.Keyframes);
            }
        }// Constructor(Collada)
コード例 #3
0
        public static List <AnimationKey> GetBoneKeyFrames(Bone bone, CoreCollada.Collada file)
        {
            var result = new List <AnimationKey>();

            var animation = file.LibraryAnimations.Where(a => a.Name == bone.Id).FirstOrDefault();

            if (animation != null)
            {
                var channel = (animation.Animations.Count > 0 && animation.Animations[0].Channels.Count > 0) ?
                              animation.Animations[0].Channels[0]
                    : null;

                if (channel != null)
                {
                    var inputName         = animation.Animations[0].Samplers[0].Inputs[0].Source;
                    var outputName        = animation.Animations[0].Samplers[0].Inputs[1].Source;
                    var interpolationName = animation.Animations[0].Samplers[0].Inputs[2].Source;

                    // time array
                    var sourceInput = animation.Animations[0].Sources.Where(a => a.Id == inputName.Substring(1)).FirstOrDefault();
                    // actual values
                    var sourceOutput        = animation.Animations[0].Sources.Where(a => a.Id == outputName.Substring(1)).FirstOrDefault();
                    var sourceInterpolation = animation.Animations[0].Sources.Where(a => a.Id == interpolationName.Substring(1)).FirstOrDefault();

                    for (var i = 0; i < sourceInput.FloatValues.Length; i++)
                    {
                        result.Add(new AnimationKey()
                        {
                            Frame     = (int)Math.Round(sourceInput.FloatValues[i] * 30),
                            Transform = sourceOutput.FloatValues.Skip(i * 16).Take(16).ToArray().TransposeMatrix()
                        });
                    }
                }
            }

            return(result);
        }