public ModelBoneCollection(ModelBoneCollection bones) { foreach (ModelBone bone in bones) { Add(bone.Clone()); } }
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(); }
protected void GetChildren(ModelBone parent, ref ModelBoneCollection list) { foreach (ModelBone child in parent.Children) { list.Add(child); child.GetChildren(child, ref list); } }
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); } }
public ModelBoneCollection AllChildren(bool includeSelf = true) { ModelBoneCollection childs = new ModelBoneCollection(); if (includeSelf) { childs.Add(this); } GetChildren(this, ref childs); return(childs); }
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; } } }
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); } } }
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); } } }