Mesh.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using BabylonExport.Core.Exporters;
  4. using SharpDX;
  5. namespace BabylonExport.Core
  6. {
  7. public class Mesh<T> where T : struct, IDumpable, IQueryable
  8. {
  9. readonly List<T> vertices = new List<T>();
  10. readonly List<int> indices = new List<int>();
  11. string concatenatedName = "";
  12. public Guid ID
  13. {
  14. get;
  15. private set;
  16. }
  17. public StandardMaterial Material { get; private set; }
  18. public Mesh(StandardMaterial material)
  19. {
  20. ID = Guid.NewGuid();
  21. Material = material;
  22. }
  23. public void AddPart(string name, List<T> addedVertices, List<int> addedIndices)
  24. {
  25. if (concatenatedName == "")
  26. concatenatedName += " # ";
  27. concatenatedName += name;
  28. var offset = vertices.Count;
  29. vertices.AddRange(addedVertices);
  30. foreach (int index in addedIndices)
  31. {
  32. indices.Add(index + offset);
  33. }
  34. }
  35. public void CreateBabylonMesh(BabylonScene scene, Guid? parentID = null, BabylonSkeleton skeleton = null)
  36. {
  37. var babylonMesh = new BabylonMesh();
  38. scene.MeshesList.Add(babylonMesh);
  39. // Guid
  40. babylonMesh.id = ID.ToString();
  41. // Name
  42. babylonMesh.name = concatenatedName;
  43. // Parent
  44. if (parentID.HasValue)
  45. babylonMesh.parentId = parentID.Value.ToString();
  46. else
  47. babylonMesh.parentId = "";
  48. // Material ID
  49. if (Material == null)
  50. {
  51. babylonMesh.materialId = "";
  52. }
  53. else
  54. {
  55. babylonMesh.materialId = Material.ID.ToString();
  56. }
  57. // Skeleton ID
  58. if (skeleton != null)
  59. {
  60. babylonMesh.skeletonId = skeleton.id;
  61. }
  62. // Position
  63. babylonMesh.position = Vector3.Zero.ToArray();
  64. // Vertices
  65. var positions = new List<float>();
  66. var normals = new List<float>();
  67. var uvs = new List<float>();
  68. var uvs2 = new List<float>();
  69. var colors = new List<float>();
  70. var matricesIndices = new List<int>();
  71. var matricesWeights = new List<float>();
  72. for (int index = 0; index < vertices.Count; index++)
  73. {
  74. var position = vertices[index].GetPosition();
  75. scene.MinVector = Vector3.Min(scene.MinVector, position);
  76. scene.MaxVector = Vector3.Max(scene.MaxVector, position);
  77. vertices[index].DumpPositions(positions);
  78. vertices[index].DumpNormals(normals);
  79. vertices[index].DumpUVs(uvs);
  80. vertices[index].DumpUVs2(uvs2);
  81. vertices[index].DumpColors(colors);
  82. vertices[index].DumpMatricesIndices(matricesIndices);
  83. vertices[index].DumpMatricesWeights(matricesWeights);
  84. }
  85. if (positions.Count > 0)
  86. {
  87. babylonMesh.positions = positions.ToArray();
  88. }
  89. if (normals.Count > 0)
  90. {
  91. babylonMesh.normals = normals.ToArray();
  92. }
  93. if (uvs.Count > 0)
  94. {
  95. babylonMesh.uvs = uvs.ToArray();
  96. }
  97. if (uvs2.Count > 0)
  98. {
  99. babylonMesh.uvs2 = uvs2.ToArray();
  100. }
  101. if (colors.Count > 0)
  102. {
  103. babylonMesh.colors = colors.ToArray();
  104. }
  105. if (matricesIndices.Count > 0)
  106. {
  107. babylonMesh.matricesIndices = matricesIndices.ToArray();
  108. }
  109. if (matricesWeights.Count > 0)
  110. {
  111. babylonMesh.matricesWeights = matricesWeights.ToArray();
  112. }
  113. // Faces
  114. babylonMesh.indices = indices.ToArray();
  115. }
  116. }
  117. }