Esempio n. 1
0
 public ModelBoneCollection(ModelBoneCollection bones)
 {
     foreach (ModelBone bone in bones)
     {
         Add(bone.Clone());
     }
 }
Esempio n. 2
0
        public void ImportBone(ModelBone bone, int parentBoneIndex)
        {
            ModelBone           cloneBone = bone.Clone();
            ModelBoneCollection boneList  = cloneBone.AllChildren();

            ModelBone parent = Bones[parentBoneIndex];

            for (int i = 0; i < boneList.Count; i++)
            {
                Bones.Insert(parentBoneIndex + i + 1, boneList[i]);

                if (boneList[i].Mesh != null)
                {
                    ModelMesh mesh = boneList[i].Mesh;
                    mesh.Parent = boneList[i];
                    Meshes.Add(mesh);
                }

                if (i == 0)
                {
                    boneList[i].Parent = parent;
                    parent.Children.Add(boneList[i]);
                }
            }

            Bones.ReIndex();
        }
Esempio n. 3
0
        protected void GetChildren(ModelBone parent, ref ModelBoneCollection list)
        {
            foreach (ModelBone child in parent.Children)
            {
                list.Add(child);

                child.GetChildren(child, ref list);
            }
        }
Esempio n. 4
0
        public static void FlipAxis(Model model, Axis axis, bool bApplyToHierarchy = false)
        {
            Vector3       transform = Vector3.One;
            List <string> processed = new List <string>();

            ModelBoneCollection bones = (bApplyToHierarchy ? model.Root.AllChildren() : new ModelBoneCollection {
                model.Root
            });

            switch (axis)
            {
            case Axis.X:
                transform.X = -1.0f;
                break;

            case Axis.Y:
                transform.Y = -1.0f;
                break;

            case Axis.Z:
                transform.Z = -1.0f;
                break;
            }

            foreach (ModelBone bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    ModelMesh mesh = bone.Mesh;

                    foreach (ModelMeshPart meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++)
                        {
                            meshpart.VertexBuffer.ModifyVertexPosition(i, meshpart.VertexBuffer.Data[i].Position * transform);
                            meshpart.VertexBuffer.ModifyVertexNormal(i, meshpart.VertexBuffer.Data[i].Normal * transform);
                        }

                        for (int i = 0; i < meshpart.IndexBuffer.Data.Count; i += 3)
                        {
                            meshpart.IndexBuffer.SwapIndices(i + 1, i + 2);
                        }

                        meshpart.IndexBuffer.Initialise();
                        meshpart.VertexBuffer.Initialise();
                    }

                    mesh.BoundingBox.Calculate(mesh);

                    processed.Add(mesh.Name);
                }

                bone.Transform = Matrix4D.CreateScale(transform) * bone.Transform * Matrix4D.CreateScale(transform);
            }
        }
Esempio n. 5
0
        public ModelBoneCollection AllChildren(bool includeSelf = true)
        {
            ModelBoneCollection childs = new ModelBoneCollection();

            if (includeSelf)
            {
                childs.Add(this);
            }
            GetChildren(this, ref childs);

            return(childs);
        }
Esempio n. 6
0
        public static void Scale(ModelBoneCollection bones, Matrix4D scale, bool applyToHierarchy = false)
        {
            List <string> processed = new List <string>();

            foreach (ModelBone bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    ModelMesh mesh = bone.Mesh;

                    foreach (ModelMeshPart meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++)
                        {
                            Vector3 position = Vector3.TransformVector(meshpart.VertexBuffer.Data[i].Position, scale);
                            meshpart.VertexBuffer.ModifyVertexPosition(i, position);
                        }

                        meshpart.VertexBuffer.Initialise();
                    }

                    processed.Add(mesh.Name);
                }

                if (applyToHierarchy)
                {
                    Matrix4D transform = bone.Transform;
                    Vector3  position  = Vector3.TransformPosition(transform.ExtractTranslation(), scale);

                    transform.M41 = position.X;
                    transform.M42 = position.Y;
                    transform.M43 = position.Z;

                    bone.Transform = transform;
                }
            }
        }
Esempio n. 7
0
        public static void MungeMeshWithBone(ModelBoneCollection bones)
        {
            List <string> processed = new List <string>();

            Vector3 offset = (bones[0].Parent != null ? bones[0].Parent.Transform.ExtractTranslation() : Vector3.Zero);

            foreach (ModelBone bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    ModelMesh mesh       = bone.Mesh;
                    Vector3   meshoffset = mesh.BoundingBox.Centre;

                    foreach (ModelMeshPart meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++)
                        {
                            meshpart.VertexBuffer.ModifyVertexPosition(i, meshpart.VertexBuffer.Data[i].Position - meshoffset);
                        }
                        meshpart.VertexBuffer.Initialise();
                    }
                    mesh.BoundingBox.Calculate(mesh);

                    Vector3 moffset = meshoffset - offset;
                    offset = meshoffset;

                    Matrix4D m = bone.Transform;
                    m.M41         += moffset.X;
                    m.M42         += moffset.Y;
                    m.M43         += moffset.Z;
                    bone.Transform = m;

                    processed.Add(mesh.Name);
                }
            }
        }
Esempio n. 8
0
        public static void FlipFaces(ModelBoneCollection bones, bool applyToHierarchy = false)
        {
            List <string> processed = new List <string>();

            foreach (ModelBone bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    ModelMesh mesh = bone.Mesh;

                    foreach (ModelMeshPart meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.IndexBuffer.Data.Count; i += 3)
                        {
                            meshpart.IndexBuffer.SwapIndices(i + 1, i + 2);
                        }

                        meshpart.IndexBuffer.Initialise();
                    }

                    processed.Add(mesh.Name);
                }
            }
        }