Example #1
0
        public static GenericMesh ToGenericMesh(byte[] data)
        {
            Level5_PRM prm = new Level5_PRM(data);

            return(prm.ToGenericMesh());
        }
Example #2
0
        public GenericModel ToGenericModel()
        {
            var model = new GenericModel();
            var skel  = new GenericSkeleton();

            model.Skeleton = skel;

            Level5_Resource resourceFile = null;
            var             textureList  = new List <GenericTexture>();

            foreach (var f in Files)
            {
                //Console.WriteLine(f.Key);
                if (f.Key.EndsWith("RES.bin"))
                {
                    resourceFile = new Level5_Resource(f.Value);
                    model.Name   = resourceFile.ModelName;
                }
                if (f.Key.EndsWith(".mbn"))
                {
                    skel.Bones.Add(Level5_MBN.ToBone(f.Value));
                }
                if (f.Key.EndsWith(".prm"))
                {
                    model.Meshes.Add(Level5_PRM.ToGenericMesh(f.Value));
                }
                if (f.Key.EndsWith(".atr"))
                {
                }
                if (f.Key.EndsWith(".xi"))
                {
                    var tex = Level5_XI.ToGenericTexture(f.Value);
                    tex.Name = f.Key;
                    textureList.Add(tex);
                }
            }

            if (resourceFile == null)
            {
                return(model);
            }


            // add materials
            foreach (var mat in resourceFile.Materials)
            {
                GenericMaterial material = new GenericMaterial();
                material.TextureDiffuse = mat.TexName;
                model.MaterialBank.Add(mat.Name, material);
            }

            // add textures
            for (int i = 0; i < textureList.Count; i++)
            {
                model.TextureBank.Add(resourceFile.TextureNames[i], textureList[i]);
            }

            // fix bones
            foreach (var bone in skel.Bones)
            {
                bone.Name = resourceFile.GetResourceName((uint)bone.ID);
                bone.ID   = 0;
            }
            foreach (var bone in skel.Bones)
            {
                if (bone.ParentIndex == 0)
                {
                    bone.ParentIndex = -1;
                }
                else
                {
                    var parentName = resourceFile.GetResourceName((uint)bone.ParentIndex);
                    bone.ParentIndex = skel.Bones.FindIndex(e => e.Name.Equals(parentName));
                }
            }
            var boneIndex = 0;

            foreach (var bone in skel.Bones)
            {
                if (bone.Name.Equals(""))
                {
                    bone.Name = "Bone_" + boneIndex++;
                }
            }

            foreach (var mesh in model.Meshes)
            {
                for (int i = 0; i < mesh.VertexCount; i++)
                {
                    var     vertex   = mesh.Vertices[i];
                    Vector4 newBones = new Vector4();
                    for (int j = 0; j < 4; j++)
                    {
                        if (vertex.Weights[j] > 0)
                        {
                            var hash = BitConverter.ToUInt32(BitConverter.GetBytes(vertex.Bones[j]), 0);
                            newBones[j] = skel.Bones.FindIndex(e => e.Name == resourceFile.GetResourceName(hash));
                        }
                    }
                    vertex.Bones     = newBones;
                    mesh.Vertices[i] = vertex;
                }
            }

            return(model);
        }