public void SetMesh(ModelMesh mesh, int boneIndex = 0) { Bones[boneIndex].Type = BoneType.Mesh; Bones[boneIndex].Attachment = mesh; mesh.Parent = Bones[boneIndex]; Meshes.Add(mesh); }
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(); }
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 ModelMesh(ModelMesh from) { MeshParts = new List <ModelMeshPart>(from.MeshParts.Count); foreach (ModelMeshPart part in from.MeshParts) { MeshParts.Add(part.Clone()); } Name = from.Name; Parent = new ModelBone(); }
public static void SetVertexColour(ModelMesh model, Colour colour) { foreach (ModelMeshPart meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexColour(i, colour); } meshpart.VertexBuffer.Initialise(); } }
public static void FlipUVs(ModelMesh model) { Vector4 flip = new Vector4(1, -1, 1, -1); foreach (ModelMeshPart meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexUVs(i, meshpart.VertexBuffer.Data[i].UV * flip); } meshpart.VertexBuffer.Initialise(); } }
public static void Freeze(ModelMesh model, FreezeComponents flags) { bool freezePos = flags.HasFlag(FreezeComponents.Position); bool freezeRot = flags.HasFlag(FreezeComponents.Rotation); bool freezeSca = flags.HasFlag(FreezeComponents.Scale); Matrix4D mPosition = Matrix4D.CreateTranslation(model.Parent.Transform.ExtractTranslation()); Matrix4D mRotation = Matrix4D.CreateFromQuaternion(model.Parent.Transform.ExtractRotation()); Matrix4D mScale = Matrix4D.CreateScale(model.Parent.Transform.ExtractScale()); if (freezePos) { model.Parent.Transform = model.Parent.Transform.ClearTranslation(); } if (freezeRot) { model.Parent.Transform = model.Parent.Transform.ClearRotation(); } if (freezeSca) { model.Parent.Transform = model.Parent.Transform.ClearScale(); } foreach (ModelMeshPart meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { if (freezePos) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.TransformVector(meshpart.VertexBuffer.Data[i].Position, mPosition)); } if (freezeRot) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.TransformVector(meshpart.VertexBuffer.Data[i].Position, mRotation)); meshpart.VertexBuffer.ModifyVertexNormal(i, Vector3.TransformNormal(meshpart.VertexBuffer.Data[i].Normal, mRotation)); } if (freezeSca) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.TransformVector(meshpart.VertexBuffer.Data[i].Position, mScale)); } } meshpart.VertexBuffer.Initialise(); } model.BoundingBox.Calculate(model); }
//public void CopyBoneTransformsFrom(Matrix4[] sourceBoneTransforms) { } //public void CopyBoneTransformsTo(Matrix4[] destinationBoneTransforms) { } public int AddMesh(ModelMesh mesh, int parentBoneIndex = 0) { bool addBone = true; ModelBone b = new ModelBone { Index = -1 }; if (Bones.Count > 0) { b.Parent = Bones[parentBoneIndex]; Bones[parentBoneIndex].Children.Add(b); int childBoneCount = CountChildrenOf(parentBoneIndex); if (parentBoneIndex + childBoneCount < Bones.Count) { int index = parentBoneIndex + childBoneCount; addBone = false; Bones.Insert(index, b); b.Index = index; } } if (addBone) { Bones.Add(b); b.Index = Bones.Count - 1; } else { Bones.ReIndex(); } if (mesh != null) { b.Type = BoneType.Mesh; b.Attachment = mesh; mesh.Parent = Bones[b.Index]; Meshes.Add(mesh); } else { // Just a bone } return(b.Index); }
public void Frame() { if (SceneManager.Current.Models.Count == 0) { return; } ModelMesh mesh = SceneManager.Current.Models[SceneManager.Current.SelectedModelIndex].Bones[SceneManager.Current.SelectedBoneIndex].Mesh; if (mesh != null) { foreach (Viewport viewport in viewports) { viewport.Camera.Frame(mesh); } } }
public void Calculate(ModelMesh mesh) { Min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Max = new Vector3(float.MinValue, float.MinValue, float.MinValue); foreach (var part in mesh.MeshParts) { foreach (var vertex in part.VertexBuffer.Data) { Min.X = Math.Min(Min.X, vertex.Position.X); Min.Y = Math.Min(Min.Y, vertex.Position.Y); Min.Z = Math.Min(Min.Z, vertex.Position.Z); Max.X = Math.Max(Max.X, vertex.Position.X); Max.Y = Math.Max(Max.Y, vertex.Position.Y); Max.Z = Math.Max(Max.Z, vertex.Position.Z); } } }
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); } } }
public static void SetVertexColour(ModelMesh model, int R, int G, int B, int A) { SetVertexColour(model, Colour.FromArgb(A / 255f, R / 255f, G / 255f, B / 255f)); }
public static void MungeMeshWithBone(ModelMesh mesh, bool includeChildren = true) { MungeMeshWithBone(includeChildren ? mesh.Parent.AllChildren() : new ModelBoneCollection { mesh.Parent }); }
public BoundingBox(ModelMesh mesh) { Mesh = mesh; Calculate(mesh); }