Exemple #1
0
        public M2Array <M2Bone> GetBones()
        {
            var keybones = Enum.GetNames(typeof(M2Bone.KeyBone)).Select(x => x.ToLower()).ToList();

            M2Array <M2Bone> bones = new M2Array <M2Bone>();

            for (int i = 0; i < _model.Get <PIVT>().Count; i++)
            {
                var    obj  = _model.Hierachy[i];
                M2Bone bone = new M2Bone()
                {
                    KeyBoneId  = (M2Bone.KeyBone)keybones.IndexOf(obj.Name.ToLower().Replace("_", "").TrimStart('$')), //Dirty but works
                    ParentBone = (short)obj.ParentId,
                    Pivot      = _model.Get <PIVT>().ElementAt(i).ToC3Vector,
                };

                obj.RotationKeys?.PopulateM2Track(bone.Rotation, _model.Get <SEQS>());
                obj.ScaleKeys?.PopulateM2Track(bone.Scale, _model.Get <SEQS>());
                obj.TranslationKeys?.PopulateM2Track(bone.Translation, _model.Get <SEQS>());

                if (obj.TranslationKeys != null)
                {
                    bone.Flags |= M2Bone.BoneFlags.Transformed;
                }

                if (obj is Bone b)
                {
                    if (b.Flags.HasFlag(GENOBJECTFLAGS.BILLBOARD_LOCK_X))
                    {
                        bone.Flags |= M2Bone.BoneFlags.CylindricalBillboardLockX;
                    }
                    if (b.Flags.HasFlag(GENOBJECTFLAGS.BILLBOARD_LOCK_Y))
                    {
                        bone.Flags |= M2Bone.BoneFlags.CylindricalBillboardLockY;
                    }
                    if (b.Flags.HasFlag(GENOBJECTFLAGS.BILLBOARD_LOCK_Z))
                    {
                        bone.Flags |= M2Bone.BoneFlags.CylindricalBillboardLockZ;
                    }
                }

                bones.Add(bone);
            }

            return(bones);
        }
Exemple #2
0
        /// <inheritdoc/>
        public void LoadBinaryData(byte[] inData)
        {
            using (var ms = new MemoryStream(inData))
                using (var br = new BinaryReader(ms))
                {
                    br.ReadUInt32(); // Signature
                    Version = br.ReadUInt32();
                    Name    = br.ReadMD20String(br.ReadUInt32(), br.ReadUInt32());
                    Flags   = br.ReadUInt32(); // TODO: Implement Flags


                    // Global Sequences
                    UInt32 count     = br.ReadUInt32();
                    UInt32 offset    = br.ReadUInt32();
                    long   headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        UInt32 value = br.ReadUInt32();
                        GlobalSequences.Add(value);
                    }
                    br.BaseStream.Position = headerpos;

                    // Sequences
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Sequence seq = new M2Sequence();

                        seq.AnimationID         = br.ReadUInt16();
                        seq.SubAnimationID      = br.ReadUInt16();
                        seq.Length              = br.ReadUInt32();
                        seq.MovingSpeed         = br.ReadSingle();
                        seq.Flags               = br.ReadUInt32();
                        seq.Probability         = br.ReadInt16();
                        seq.Padding             = br.ReadUInt16();
                        seq.MinimumRepetitions  = br.ReadUInt32();
                        seq.MaximumRepetitions  = br.ReadUInt32();
                        seq.BlendTime           = br.ReadUInt32();
                        seq.BoundsMinimumExtend = new C3Vector(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        seq.BoundsMaximumExtend = new C3Vector(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        seq.BoundRadius         = br.ReadSingle();
                        seq.NextAnimation       = br.ReadInt16();
                        seq.aliasNext           = br.ReadUInt16();

                        Sequences.Add(seq);
                    }
                    br.BaseStream.Position = headerpos;

                    //SequencesLookups
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        SequencesLookups.Add(br.ReadInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Bones
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Bone bone = new M2Bone();

                        bone.KeyBoneID  = br.ReadInt32();
                        bone.Flags      = br.ReadUInt32();
                        bone.ParentBone = br.ReadInt16();
                        bone.SubmeshID  = br.ReadUInt16();

                        bone.CompressData[0] = br.ReadUInt16();
                        bone.CompressData[1] = br.ReadUInt16();

                        //translation

                        M2Track translation = new M2Track();
                        translation.readM2Track(br);
                        bone.translation = translation;

                        // rotation
                        M2Track rotation = new M2Track();
                        rotation.readM2Track(br);
                        bone.rotation = rotation;

                        // Scale
                        M2Track scale = new M2Track();
                        scale.readM2Track(br);
                        bone.scale = scale;

                        bone.pivot = new C3Vector(br.ReadUInt32(), br.ReadUInt32(), br.ReadUInt32());

                        Bones.Add(bone);
                    }
                    br.BaseStream.Position = headerpos;

                    // key_bone_lookup
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        KeyBoneLookup.Add(br.ReadInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Vetices
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Vertex temp = new M2Vertex();

                        temp.Pos = new C3Vector(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        for (int a = 0; a < 4; a++)
                        {
                            temp.BoneWeights.Add(br.ReadByte());
                        }
                        for (int a = 0; a < 4; a++)
                        {
                            temp.BoneIndices.Add(br.ReadByte());
                        }
                        temp.Normal = new C3Vector(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        temp.TexCords.Add(new C2Vector(br.ReadSingle(), br.ReadSingle()));
                        temp.TexCords.Add(new C2Vector(br.ReadSingle(), br.ReadSingle()));
                    }
                    br.BaseStream.Position = headerpos;

                    // Number of Skin profiles
                    NumberSkinProfiles = br.ReadUInt32();

                    // Colors
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Color temp = new M2Color();

                        // Color
                        M2Track color = new M2Track();
                        color.readM2Track(br);
                        temp.Color = color;

                        // Alpha
                        M2Track alpha = new M2Track();
                        alpha.readM2Track(br);
                        temp.Alpha = alpha;

                        Color.Add(temp);
                    }
                    br.BaseStream.Position = headerpos;

                    // Textures
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Texture temp = new M2Texture();

                        temp.Type  = br.ReadUInt32();
                        temp.Flags = br.ReadUInt32();

                        UInt32 tCount     = br.ReadUInt32();
                        UInt32 tOffset    = br.ReadUInt32();
                        long   tHeaderpos = br.BaseStream.Position;
                        br.BaseStream.Position = tOffset;

                        temp.Filename = "";
                        for (int a = 0; a < tCount; a++)
                        {
                            temp.Filename += br.ReadChar();
                        }
                        br.BaseStream.Position = tHeaderpos;

                        Texture.Add(temp);
                    }
                    br.BaseStream.Position = headerpos;

                    // Texture Weights
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Track temp = new M2Track();
                        temp.readM2Track(br);
                        TextureWeights.Add(temp);
                    }
                    br.BaseStream.Position = headerpos;

                    // UV Animations
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2TextureTransform temp = new M2TextureTransform();

                        M2Track translation = new M2Track();
                        translation.readM2Track(br);
                        temp.Translation = translation;

                        M2Track rotation = new M2Track();
                        rotation.readM2Track(br);
                        temp.Rotation = rotation;

                        M2Track scaling = new M2Track();
                        scaling.readM2Track(br);
                        temp.Scaling = scaling;

                        UvAnimations.Add(temp);
                    }
                    br.BaseStream.Position = headerpos;

                    // Texture Replacements
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        TextureReplacements.Add(br.ReadInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Materials
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        M2Material temp = new M2Material();

                        temp.Flags     = br.ReadUInt16();
                        temp.BlendMode = br.ReadUInt16();

                        Materials.Add(temp);
                    }
                    br.BaseStream.Position = headerpos;

                    // Bone Lookups
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        BoneLookups.Add(br.ReadUInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Texture Units
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        TextureUnits.Add(br.ReadUInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Texture Weights Lookups
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        TextureWeightsLookups.Add(br.ReadUInt16());
                    }
                    br.BaseStream.Position = headerpos;

                    // Animation Lookups
                    count     = br.ReadUInt32();
                    offset    = br.ReadUInt32();
                    headerpos = br.BaseStream.Position;
                    br.BaseStream.Position = offset;
                    for (int i = 0; i < count; i++)
                    {
                        UvAnimationLookups.Add(br.ReadInt16());
                    }
                    br.BaseStream.Position = headerpos;
                }
        }
Exemple #3
0
        /// <summary>
        /// Create data of a WoW M2 Bone with the values of this class.
        /// </summary>
        public M2Bone ToBone()
        {
            var bone = new M2Bone
            {
                KeyBoneId  = GetBoneType(),
                ParentBone = (short)(Parent?.GlobalIndex ?? -1),
                Pivot      = ConvertVector(AxisInvert(GetPivot()))
            };

            foreach (var list in Translation)
            {
                var timestamps = new M2Array <uint>();
                var values     = new M2Array <C3Vector>();
                foreach (var pair in list)
                {
                    timestamps.Add(pair.Item1);
                    values.Add(ConvertVector(AxisInvert(pair.Item2 - BaseTranslation)));
                }
                bone.Translation.Timestamps.Add(timestamps);
                bone.Translation.Values.Add(values);
            }
            foreach (var list in Rotation)
            {
                var timestamps = new M2Array <uint>();
                var values     = new M2Array <C4Quaternion>();
                foreach (var pair in list)
                {
                    timestamps.Add(pair.Item1);
                    values.Add(ConvertVector(AxisInvert(pair.Item2)));
                }
                bone.Rotation.Timestamps.Add(timestamps);
                bone.Rotation.Values.Add(values);
            }
            foreach (var list in Scale)
            {
                var timestamps = new M2Array <uint>();
                var values     = new M2Array <C3Vector>();
                foreach (var pair in list)
                {
                    timestamps.Add(pair.Item1);
                    values.Add(ConvertVector(ScaleAxisInvert(pair.Item2)));
                }
                bone.Scale.Timestamps.Add(timestamps);
                bone.Scale.Values.Add(values);
            }
            //TODO maybe bezier ?
            if (Translation.Count > 0)
            {
                bone.Translation.InterpolationType = M2Track <C3Vector> .InterpolationTypes.Linear;
            }
            if (Rotation.Count > 0)
            {
                bone.Rotation.InterpolationType = M2Track <C4Quaternion> .InterpolationTypes.Linear;
            }
            if (Scale.Count > 0)
            {
                bone.Scale.InterpolationType = M2Track <C3Vector> .InterpolationTypes.Linear;
            }
            if (Translation.Count > 0 || Rotation.Count > 0 || Scale.Count > 0)
            {
                bone.Flags |= M2Bone.BoneFlags.Transformed;
            }
            return(bone);
        }