public void Init(MyAnimationClip.Bone bone, AnimationPlayer player)
            {
                this.ClipBone = bone;
                Player        = player;

                SetKeyframes();
                SetPosition(0);

                m_isConst = ClipBone.Keyframes.Count == 1;
            }
 public void Clear()
 {
     m_currentKeyframe = 0;
     m_isConst         = false;
     m_assignedBone    = null;
     Rotation          = default(Quaternion);
     Translation       = Vector3.Zero;
     Player            = null;
     Keyframe1         = null;
     Keyframe2         = null;
     m_clipBone        = null;
 }
Example #3
0
        private static MyAnimationClip ReadClip(BinaryReader reader)
        {
            var myAnimationClip = new MyAnimationClip();

            myAnimationClip.Name     = reader.BaseStream.ReadString(Encoding.ASCII);
            myAnimationClip.Duration = reader.ReadDouble();
            var num1 = reader.ReadInt32();

            while (num1-- > 0)
            {
                var bone = new MyAnimationClip.Bone();
                bone.Name = reader.BaseStream.ReadString(Encoding.ASCII);
                var num2 = reader.ReadInt32();
                while (num2-- > 0)
                {
                    var keyframe = new MyAnimationClip.Keyframe();
                    keyframe.Time     = reader.ReadDouble();
                    keyframe.Rotation = ImportQuaternion(reader);
                    if (NORMALIZE_QUATERNIONS)
                    {
                        keyframe.Rotation.Normalize();
                    }
                    keyframe.Translation = ImportVector3(reader);
                    bone.Keyframes.Add(keyframe);
                }

                myAnimationClip.Bones.Add(bone);
                var count = bone.Keyframes.Count;
                var num3  = 0;
                if (count > 3)
                {
                    if (USE_LINEAR_KEYFRAME_REDUCTION)
                    {
                        var linkedList = new LinkedList <MyAnimationClip.Keyframe>();
                        foreach (var keyframe in bone.Keyframes)
                        {
                            linkedList.AddLast(keyframe);
                        }
                        LinearKeyframeReduction(linkedList, 1E-08f, 0.9999999f);
                        bone.Keyframes.Clear();
                        bone.Keyframes.AddCollection <MyAnimationClip.Keyframe>(
                            linkedList.ToArray <MyAnimationClip.Keyframe>());
                        num3 = bone.Keyframes.Count;
                    }
                }

                CalculateKeyframeDeltas(bone.Keyframes);
            }

            return(myAnimationClip);
        }
Example #4
0
        private static ModelAnimations ReadModelAnimations(BinaryReader reader)
        {
            var modelAnimations = new ModelAnimations {
                Clips = new List <MyAnimationClip>()
            };
            var animationCount = reader.ReadInt32();

            for (var i = 0; i < animationCount; i++)
            {
                var clipName      = reader.ReadString();
                var duration      = reader.ReadDouble();
                var animationClip = new MyAnimationClip()
                {
                    Name = clipName, Duration = duration
                };

                var boneCount = reader.ReadInt32();
                for (var j = 0; j < boneCount; j++)
                {
                    var boneName = reader.ReadString();
                    var bone     = new MyAnimationClip.Bone()
                    {
                        Name = boneName
                    };
                    var keyFrameCount = reader.ReadInt32();

                    for (var k = 0; k < keyFrameCount; k++)
                    {
                        var time        = reader.ReadDouble();
                        var vector      = ReadVector4(reader);
                        var rotation    = new Quaternion(vector.X, vector.Y, vector.Z, vector.W);
                        var translation = ReadVector3(reader);
                        bone.Keyframes.Add(new MyAnimationClip.Keyframe()
                        {
                            Time = time, Rotation = rotation, Translation = translation
                        });
                    }

                    animationClip.Bones.Add(bone);
                }

                modelAnimations.Clips.Add(animationClip);
            }

            modelAnimations.Skeleton = ReadArrayOfInt(reader).ToList();
            return(modelAnimations);
        }
Example #5
0
 private MyAnimationClip.Bone FindBone(List <MyAnimationClip.Bone> bones, string name)
 {
     using (List <MyAnimationClip.Bone> .Enumerator enumerator = bones.GetEnumerator())
     {
         while (true)
         {
             if (!enumerator.MoveNext())
             {
                 break;
             }
             MyAnimationClip.Bone current = enumerator.Current;
             if (current.Name == name)
             {
                 return(current);
             }
         }
     }
     return(null);
 }
Example #6
0
        static MyAnimationClip ReadClip(BinaryReader reader)
        {
            MyAnimationClip clip = new MyAnimationClip();

            clip.Name     = reader.ReadString();
            clip.Duration = reader.ReadDouble();

            int bonesCount = reader.ReadInt32();

            while (bonesCount-- > 0)
            {
                MyAnimationClip.Bone bone = new MyAnimationClip.Bone();
                bone.Name = reader.ReadString();

                int keyframesCount = reader.ReadInt32();
                while (keyframesCount-- > 0)
                {
                    MyAnimationClip.Keyframe keyframe = new MyAnimationClip.Keyframe();
                    keyframe.Time        = reader.ReadDouble();
                    keyframe.Rotation    = ImportQuaternion(reader);
                    keyframe.Translation = ImportVector3(reader);
                    bone.Keyframes.Add(keyframe);
                }

                clip.Bones.Add(bone);

                int originalCount = bone.Keyframes.Count;
                int newCount      = 0;
                if (originalCount > 3)
                {
                    if (USE_LINEAR_KEYFRAME_REDUCTION)
                    {
                        LinkedList <MyAnimationClip.Keyframe> linkedList = new LinkedList <MyAnimationClip.Keyframe>();
                        foreach (var kf in bone.Keyframes)
                        {
                            linkedList.AddLast(kf);
                        }
                        //LinearKeyframeReduction(linkedList, 0.000001f, 0.985f);
                        //PercentageKeyframeReduction(linkedList, 0.9f);
                        LinearKeyframeReduction(linkedList, TinyLength, TinyCosAngle);
                        bone.Keyframes.Clear();
                        bone.Keyframes.AddArray(linkedList.ToArray());
                        newCount = bone.Keyframes.Count;
                    }
                    if (LINEAR_KEYFRAME_REDUCTION_STATS)
                    {
                        ReductionInfo ri = new ReductionInfo()
                        {
                            BoneName      = bone.Name,
                            OriginalKeys  = originalCount,
                            OptimizedKeys = newCount
                        };

                        List <ReductionInfo> riList;
                        if (!ReductionStats.TryGetValue(m_debugAssetName, out riList))
                        {
                            riList = new List <ReductionInfo>();
                            ReductionStats.Add(m_debugAssetName, riList);
                        }

                        riList.Add(ri);
                    }
                }

                CalculateKeyframeDeltas(bone.Keyframes);
            }

            return(clip);
        }
 public BoneInfo(MyAnimationClip.Bone bone, AnimationPlayer player)
 {
     Init(bone, player);
 }
Example #8
0
        public void Initialize(MyModel animationModel, string playerName, int clipIndex, MySkinnedEntity skinnedEntity, float weight, float timeScale, MyFrameOption frameOption, string[] explicitBones = null, Dictionary <float, string[]> boneLODs = null)
        {
            List <BoneInfo> list;

            if (this.m_hash != 0)
            {
                CachedAnimationPlayers.Remove(this.m_hash);
                this.m_hash = 0;
            }
            MyAnimationClip clip = animationModel.Animations.Clips[clipIndex];

            this.Name            = MyStringId.GetOrCompute(animationModel.AssetName + " : " + playerName);
            this.m_duration      = (float)clip.Duration;
            this.m_skinnedEntity = skinnedEntity;
            this.m_weight        = weight;
            this.m_timeScale     = timeScale;
            this.m_frameOption   = frameOption;
            using (Dictionary <float, List <BoneInfo> > .ValueCollection.Enumerator enumerator = this.m_boneLODs.Values.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    enumerator.Current.Clear();
                }
            }
            if (!this.m_boneLODs.TryGetValue(0f, out list))
            {
                list = new List <BoneInfo>();
                this.m_boneLODs.Add(0f, list);
            }
            int num = (explicitBones == null) ? clip.Bones.Count : explicitBones.Length;

            if ((this.m_boneInfos == null) || (this.m_boneInfos.Length < num))
            {
                this.m_boneInfos = new BoneInfo[num];
            }
            int index = 0;

            for (int i = 0; i < num; i++)
            {
                MyAnimationClip.Bone bone = (explicitBones == null) ? clip.Bones[i] : this.FindBone(clip.Bones, explicitBones[i]);
                if ((bone != null) && (bone.Keyframes.Count != 0))
                {
                    BoneInfo item = this.m_boneInfos[index];
                    if (this.m_boneInfos[index] == null)
                    {
                        item = new BoneInfo(bone, this);
                    }
                    else
                    {
                        item.Clear();
                        item.Init(bone, this);
                    }
                    this.m_boneInfos[index] = item;
                    this.m_boneInfos[index].SetModel(skinnedEntity);
                    if (item.ModelBone != null)
                    {
                        list.Add(item);
                        if (boneLODs != null)
                        {
                            foreach (KeyValuePair <float, string[]> pair in boneLODs)
                            {
                                List <BoneInfo> list2;
                                if (!this.m_boneLODs.TryGetValue(pair.Key, out list2))
                                {
                                    list2 = new List <BoneInfo>();
                                    this.m_boneLODs.Add(pair.Key, list2);
                                }
                                foreach (string str in pair.Value)
                                {
                                    if (item.ModelBone.Name == str)
                                    {
                                        list2.Add(item);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    index++;
                }
            }
            this.m_boneCount   = index;
            this.Position      = 0f;
            this.m_initialized = true;
        }