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 static void FlipAxis(ModelMesh model, Axis axis, bool bApplyToHierarchy = false) { var transform = Vector3.One; var processed = new List <string>(); var bones = (bApplyToHierarchy ? model.Parent.AllChildren() : new ModelBoneCollection { model.Parent }); 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 (var bone in bones) { if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name)) { var mesh = bone.Mesh; foreach (var meshpart in mesh.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.Multiply(meshpart.VertexBuffer.Data[i].Position, transform)); meshpart.VertexBuffer.ModifyVertexNormal(i, Vector3.Multiply(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 = Matrix4.CreateScale(transform) * bone.Transform * Matrix4.CreateScale(transform); } }
public static void SetVertexColour(ModelMesh model, Color colour) { foreach (var meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexColour(i, colour); } meshpart.VertexBuffer.Initialise(); } }
public static void FlipAxis(ModelMesh model, Axis axis, bool bApplyToHierarchy = false) { var transform = Vector3.One; var processed = new List<string>(); var bones = (bApplyToHierarchy ? model.Parent.AllChildren() : new ModelBoneCollection { model.Parent }); 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 (var bone in bones) { if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name)) { var mesh = bone.Mesh; foreach (var meshpart in mesh.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.Multiply(meshpart.VertexBuffer.Data[i].Position, transform)); meshpart.VertexBuffer.ModifyVertexNormal(i, Vector3.Multiply(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 = Matrix4.CreateScale(transform) * bone.Transform * Matrix4.CreateScale(transform); } }
public static void FlipUVs(ModelMesh model) { var flip = new Vector4(1, -1, 1, -1); foreach (var meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexUVs(i, Vector4.Multiply(meshpart.VertexBuffer.Data[i].UV, flip)); } meshpart.VertexBuffer.Initialise(); } }
public static void Freeze(ModelMesh model, FreezeComponents flags) { bool bFreezePos = ((flags & FreezeComponents.Position) == FreezeComponents.Position); bool bFreezeRot = ((flags & FreezeComponents.Rotation) == FreezeComponents.Rotation); bool bFreezeSca = ((flags & FreezeComponents.Scale) == FreezeComponents.Scale); var mPosition = Matrix4.CreateTranslation(model.Parent.Transform.ExtractTranslation()); var mRotation = Matrix4.CreateFromQuaternion(model.Parent.Transform.ExtractRotation()); var mScale = Matrix4.CreateScale(model.Parent.Transform.ExtractScale()); if (bFreezePos) { model.Parent.Transform = model.Parent.Transform.ClearTranslation(); } if (bFreezeRot) { model.Parent.Transform = model.Parent.Transform.ClearRotation(); } if (bFreezeSca) { model.Parent.Transform = model.Parent.Transform.ClearScale(); } foreach (var meshpart in model.MeshParts) { for (int i = 0; i < meshpart.VertexCount; i++) { if (bFreezePos) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.Transform(meshpart.VertexBuffer.Data[i].Position, mPosition)); } if (bFreezeRot) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.Transform(meshpart.VertexBuffer.Data[i].Position, mRotation)); meshpart.VertexBuffer.ModifyVertexNormal(i, Vector3.TransformNormal(meshpart.VertexBuffer.Data[i].Normal, mRotation)); } if (bFreezeSca) { meshpart.VertexBuffer.ModifyVertexPosition(i, Vector3.Transform(meshpart.VertexBuffer.Data[i].Position, mScale)); } } meshpart.VertexBuffer.Initialise(); } model.BoundingBox.Calculate(model); }
public int AddMesh(ModelMesh mesh, int ParentBoneIndex = 0) { bool bAddBone = true; ModelBone b = new ModelBone(); b.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; bAddBone = false; bones.Insert(index, b); b.Index = index; } } if (bAddBone) { 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 int AddMesh(ModelMesh mesh, int ParentBoneIndex = 0) { bool bAddBone = true; ModelBone b = new ModelBone(); b.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; bAddBone = false; bones.Insert(index, b); b.Index = index; } } if (bAddBone) { 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 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 SetVertexColour(ModelMesh model, int R, int G, int B, int A) { SetVertexColour(model, Color.FromArgb(A, R, G, B)); }
public void Draw() { if (Linked) { var parentTransform = ((ModelBone)link).CombinedTransform; if (linkType == LinkType.All) { transform = parentTransform; } else { transform = Matrix4.Identity; var v = parentTransform.ExtractTranslation(); parentTransform.Normalize(); parentTransform.M41 = v.X; parentTransform.M42 = v.Y; parentTransform.M43 = v.Z; if ((linkType & LinkType.Rotation) == LinkType.Rotation) { transform *= Matrix4.CreateFromQuaternion(parentTransform.ExtractRotation()); } if ((linkType & LinkType.Scale) == LinkType.Scale) { transform *= Matrix4.CreateScale(parentTransform.ExtractScale()); } if ((linkType & LinkType.Position) == LinkType.Position) { transform *= Matrix4.CreateTranslation(parentTransform.ExtractTranslation()); } } } var mS = SceneManager.Current.Transform; var mT = transform; switch (assetType) { case AssetType.Model: var model = asset as Model; if (model != null) { GL.PushMatrix(); GL.MultMatrix(ref mS); GL.MultMatrix(ref mT); model.Draw(); GL.PopMatrix(); } break; case AssetType.Sprite: if (asset == null) { var sprite = new ModelMeshPart(); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3(0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3(0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.AddVertex(new Vector3(0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3(0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.IndexBuffer.Initialise(); sprite.VertexBuffer.Initialise(); sprite.Material = new Material { Name = "Entity.Asset", Texture = SceneManager.Current.Content.Load <Texture, PNGImporter>("entity_" + entityType.ToString().ToLower(), Path.GetDirectoryName(Application.ExecutablePath) + @"\data\icons\") }; sprite.PrimitiveType = PrimitiveType.Quads; var spritemesh = new ModelMesh(); spritemesh.AddModelMeshPart(sprite); var spritemodel = new Model(); spritemodel.AddMesh(spritemesh); asset = spritemodel; } GL.PushMatrix(); var position = Matrix4.CreateTranslation(mT.ExtractTranslation()); GL.MultMatrix(ref mS); GL.MultMatrix(ref position); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); ((Model)asset).Draw(); GL.Disable(EnableCap.Blend); GL.PopMatrix(); break; } }
public static void MungeMeshWithBone(ModelMesh mesh, bool bIncludeChildren = true) { MungeMeshWithBone(bIncludeChildren ? mesh.Parent.AllChildren() : new ModelBoneCollection { mesh.Parent }); }
public void Frame(ModelMesh mesh) { target = Vector3.Transform(Vector3.Transform(mesh.BoundingBox.Centre, mesh.Parent.CombinedTransform), SceneManager.Current.Transform); }
public BoundingBox(ModelMesh mesh) { this.mesh = mesh; Calculate(mesh); }
public ModelMesh(ModelMesh from) { this.meshParts = new List <ModelMeshPart>(from.meshParts); this.name = from.name; this.parent = new ModelBone(); }
public void Draw() { if (Linked) { var parentTransform = ((ModelBone)link).CombinedTransform; if (linkType == LinkType.All) { transform = parentTransform; } else { transform = Matrix4.Identity; var v = parentTransform.ExtractTranslation(); parentTransform.Normalize(); parentTransform.M41 = v.X; parentTransform.M42 = v.Y; parentTransform.M43 = v.Z; if ((linkType & LinkType.Rotation) == LinkType.Rotation) { transform *= Matrix4.CreateFromQuaternion(parentTransform.ExtractRotation()); } if ((linkType & LinkType.Scale) == LinkType.Scale) { transform *= Matrix4.CreateScale(parentTransform.ExtractScale()); } if ((linkType & LinkType.Position) == LinkType.Position) { transform *= Matrix4.CreateTranslation(parentTransform.ExtractTranslation()); } } } var mS = SceneManager.Current.Transform; var mT = transform; switch (assetType) { case AssetType.Model: var model = asset as Model; if (model != null) { GL.PushMatrix(); GL.MultMatrix(ref mS); GL.MultMatrix(ref mT); model.Draw(); GL.PopMatrix(); } break; case AssetType.Sprite: if (asset == null) { var sprite = new ModelMeshPart(); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3( 0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3( 0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.AddVertex(new Vector3( 0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 1)); sprite.AddVertex(new Vector3( 0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(0, 0)); sprite.AddVertex(new Vector3(-0.25f, 0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 0)); sprite.AddVertex(new Vector3(-0.25f, -0.25f, 0.0f), Vector3.UnitY, new Vector2(1, 1)); sprite.IndexBuffer.Initialise(); sprite.VertexBuffer.Initialise(); sprite.Material = new Material { Name = "Entity.Asset", Texture = SceneManager.Current.Content.Load<Texture, PNGImporter>("entity_" + entityType.ToString().ToLower(), Path.GetDirectoryName(Application.ExecutablePath) + @"\data\icons\") }; sprite.PrimitiveType = PrimitiveType.Quads; var spritemesh = new ModelMesh(); spritemesh.AddModelMeshPart(sprite); var spritemodel = new Model(); spritemodel.AddMesh(spritemesh); asset = spritemodel; } GL.PushMatrix(); var position = Matrix4.CreateTranslation(mT.ExtractTranslation()); GL.MultMatrix(ref mS); GL.MultMatrix(ref position); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); ((Model)asset).Draw(); GL.Disable(EnableCap.Blend); GL.PopMatrix(); break; } }
public ModelMesh(ModelMesh from) { this.meshParts = new List<ModelMeshPart>(from.meshParts); this.name = from.name; this.parent = new ModelBone(); }