using System; using System.Collections.Generic; using BabylonExport.Core.Exporters; using SharpDX; namespace BabylonExport.Core { public class Mesh where T : struct, IDumpable, IQueryable { readonly List vertices = new List(); readonly List indices = new List(); string concatenatedName = ""; public Guid ID { get; private set; } public StandardMaterial Material { get; private set; } public Mesh(StandardMaterial material) { ID = Guid.NewGuid(); Material = material; } public void AddPart(string name, List addedVertices, List addedIndices) { if (concatenatedName == "") concatenatedName += " # "; concatenatedName += name; var offset = vertices.Count; vertices.AddRange(addedVertices); foreach (int index in addedIndices) { indices.Add(index + offset); } } public void CreateBabylonMesh(BabylonScene scene, Guid? parentID = null, BabylonSkeleton skeleton = null) { var babylonMesh = new BabylonMesh(); scene.MeshesList.Add(babylonMesh); // Guid babylonMesh.id = ID.ToString(); // Name babylonMesh.name = concatenatedName; // Parent if (parentID.HasValue) babylonMesh.parentId = parentID.Value.ToString(); else babylonMesh.parentId = ""; // Material ID if (Material == null) { babylonMesh.materialId = ""; } else { babylonMesh.materialId = Material.ID.ToString(); } // Skeleton ID if (skeleton != null) { babylonMesh.skeletonId = skeleton.id; } // Position babylonMesh.position = Vector3.Zero.ToArray(); // Vertices var positions = new List(); var normals = new List(); var uvs = new List(); var uvs2 = new List(); var colors = new List(); var matricesIndices = new List(); var matricesWeights = new List(); for (int index = 0; index < vertices.Count; index++) { var position = vertices[index].GetPosition(); scene.MinVector = Vector3.Min(scene.MinVector, position); scene.MaxVector = Vector3.Max(scene.MaxVector, position); vertices[index].DumpPositions(positions); vertices[index].DumpNormals(normals); vertices[index].DumpUVs(uvs); vertices[index].DumpUVs2(uvs2); vertices[index].DumpColors(colors); vertices[index].DumpMatricesIndices(matricesIndices); vertices[index].DumpMatricesWeights(matricesWeights); } if (positions.Count > 0) { babylonMesh.positions = positions.ToArray(); } if (normals.Count > 0) { babylonMesh.normals = normals.ToArray(); } if (uvs.Count > 0) { babylonMesh.uvs = uvs.ToArray(); } if (uvs2.Count > 0) { babylonMesh.uvs2 = uvs2.ToArray(); } if (colors.Count > 0) { babylonMesh.colors = colors.ToArray(); } if (matricesIndices.Count > 0) { babylonMesh.matricesIndices = matricesIndices.ToArray(); } if (matricesWeights.Count > 0) { babylonMesh.matricesWeights = matricesWeights.ToArray(); } // Faces babylonMesh.indices = indices.ToArray(); } } }