BabylonExporter.Mesh.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using Autodesk.Max;
  6. using BabylonExport.Entities;
  7. using MaxSharp;
  8. namespace Max2Babylon
  9. {
  10. partial class BabylonExporter
  11. {
  12. private BabylonMesh ExportMesh(Node meshNode, BabylonScene babylonScene)
  13. {
  14. var babylonMesh = new BabylonMesh();
  15. int vx1, vx2, vx3;
  16. RaiseMessage(meshNode.Name, true);
  17. babylonMesh.name = meshNode.Name;
  18. babylonMesh.id = meshNode.GetGuid().ToString();
  19. if (meshNode.HasParent())
  20. {
  21. babylonMesh.parentId = meshNode.Parent.GetGuid().ToString();
  22. }
  23. // Misc.
  24. babylonMesh.isVisible = meshNode._Node.Renderable == 1;
  25. babylonMesh.pickable = meshNode._Node.GetBoolProperty("babylonjs_checkpickable");
  26. babylonMesh.receiveShadows = meshNode._Node.RcvShadows == 1;
  27. // Collisions
  28. babylonMesh.checkCollisions = meshNode._Node.GetBoolProperty("babylonjs_checkcollisions");
  29. // Position / rotation / scaling
  30. var wm = meshNode.GetWorldMatrix(0, meshNode.HasParent());
  31. babylonMesh.position = wm.Trans.ToArraySwitched();
  32. var parts = Loader.Global.AffineParts.Create();
  33. Loader.Global.DecompAffine(wm, parts);
  34. //var rotate = new float[3];
  35. //IntPtr xPtr = Marshal.AllocHGlobal(sizeof(float));
  36. //IntPtr yPtr = Marshal.AllocHGlobal(sizeof(float));
  37. //IntPtr zPtr = Marshal.AllocHGlobal(sizeof(float));
  38. //parts.Q.GetEuler(xPtr, yPtr, zPtr);
  39. //Marshal.Copy(xPtr, rotate, 0, 1);
  40. //Marshal.Copy(yPtr, rotate, 1, 1);
  41. //Marshal.Copy(zPtr, rotate, 2, 1);
  42. //var temp = -rotate[1];
  43. //rotate[0] = rotate[0] * parts.F;
  44. //rotate[1] = -rotate[2] * parts.F;
  45. //rotate[2] = temp * parts.F;
  46. //babylonMesh.rotation = rotate;
  47. babylonMesh.rotationQuaternion = parts.Q.ToArray();
  48. babylonMesh.scaling = parts.K.ToArraySwitched();
  49. if (wm.Parity)
  50. {
  51. vx1 = 2;
  52. vx2 = 1;
  53. vx3 = 0;
  54. }
  55. else
  56. {
  57. vx1 = 0;
  58. vx2 = 1;
  59. vx3 = 2;
  60. }
  61. // Pivot
  62. //var pivotMatrix = Matrix3.Identity._IMatrix3;
  63. //pivotMatrix.PreTranslate(meshNode._Node.ObjOffsetPos);
  64. //Loader.Global.PreRotateMatrix(pivotMatrix, meshNode._Node.ObjOffsetRot);
  65. //Loader.Global.ApplyScaling(pivotMatrix, meshNode._Node.ObjOffsetScale);
  66. //babylonMesh.localMatrix = pivotMatrix.ToArray();
  67. // Mesh
  68. var objectState = meshNode._Node.EvalWorldState(0, false);
  69. var mesh = objectState.Obj.GetMesh();
  70. var computedMesh = meshNode.GetMesh();
  71. if (mesh != null)
  72. {
  73. mesh.BuildNormals();
  74. if (mesh.NumFaces < 1)
  75. {
  76. RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name));
  77. }
  78. if (mesh.NumVerts < 3)
  79. {
  80. RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name));
  81. }
  82. if (mesh.NumVerts >= 65536)
  83. {
  84. RaiseError(string.Format("Mesh {0} has too many vertices (more than 65535)", babylonMesh.name));
  85. }
  86. // Material
  87. var mtl = meshNode.Material;
  88. var multiMatsCount = 1;
  89. if (mtl != null)
  90. {
  91. babylonMesh.materialId = mtl.GetGuid().ToString();
  92. if (!referencedMaterials.Contains(mtl))
  93. {
  94. referencedMaterials.Add(mtl);
  95. }
  96. multiMatsCount = Math.Max(mtl.NumSubMaterials, 1);
  97. }
  98. babylonMesh.visibility = meshNode._Node.GetVisibility(0, Interval.Forever._IInterval);
  99. var vertices = new List<GlobalVertex>();
  100. var indices = new List<int>();
  101. var matIDs = new List<int>();
  102. var hasUV = mesh.NumTVerts > 0;
  103. var hasUV2 = mesh.GetNumMapVerts(2) > 0;
  104. for (var face = 0; face < mesh.NumFaces; face++)
  105. {
  106. indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx1, vertices, hasUV, hasUV2));
  107. indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx2, vertices, hasUV, hasUV2));
  108. indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx3, vertices, hasUV, hasUV2));
  109. matIDs.Add(mesh.Faces[face].MatID % multiMatsCount);
  110. }
  111. // Buffers
  112. babylonMesh.positions = vertices.SelectMany(v => v.Position.ToArraySwitched()).ToArray();
  113. babylonMesh.normals = vertices.SelectMany(v => v.Normal.ToArraySwitched()).ToArray();
  114. if (hasUV)
  115. {
  116. babylonMesh.uvs = vertices.SelectMany(v => v.UV.ToArray()).ToArray();
  117. }
  118. if (hasUV2)
  119. {
  120. babylonMesh.uvs2 = vertices.SelectMany(v => v.UV2.ToArray()).ToArray();
  121. }
  122. // Submeshes
  123. var sortedIndices = new List<int>();
  124. var subMeshes = new List<BabylonSubMesh>();
  125. var indexStart = 0;
  126. for (var index = 0; index < multiMatsCount; index++)
  127. {
  128. var subMesh = new BabylonSubMesh();
  129. var indexCount = 0;
  130. var minVertexIndex = int.MaxValue;
  131. var maxVertexIndex = int.MinValue;
  132. subMesh.indexStart = indexStart;
  133. subMesh.materialIndex = index;
  134. for (var face = 0; face < matIDs.Count; face++)
  135. {
  136. if (matIDs[face] == index)
  137. {
  138. var a = indices[3*face];
  139. var b = indices[3*face + 1];
  140. var c = indices[3*face + 2];
  141. sortedIndices.Add(a);
  142. sortedIndices.Add(b);
  143. sortedIndices.Add(c);
  144. indexCount += 3;
  145. if (a < minVertexIndex)
  146. {
  147. minVertexIndex = a;
  148. }
  149. if (b < minVertexIndex)
  150. {
  151. minVertexIndex = b;
  152. }
  153. if (c < minVertexIndex)
  154. {
  155. minVertexIndex = c;
  156. }
  157. if (a > maxVertexIndex)
  158. {
  159. maxVertexIndex = a;
  160. }
  161. if (b > maxVertexIndex)
  162. {
  163. maxVertexIndex = b;
  164. }
  165. if (c > maxVertexIndex)
  166. {
  167. maxVertexIndex = c;
  168. }
  169. }
  170. }
  171. subMesh.indexCount = indexCount;
  172. subMesh.verticesStart = minVertexIndex;
  173. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  174. indexStart += indexCount;
  175. subMeshes.Add(subMesh);
  176. }
  177. babylonMesh.subMeshes = subMeshes.ToArray();
  178. // Buffers - Indices
  179. babylonMesh.indices = sortedIndices.ToArray();
  180. }
  181. babylonScene.MeshesList.Add(babylonMesh);
  182. return babylonMesh;
  183. }
  184. int CreateGlobalVertex(IMesh mesh, Mesh computedMesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2)
  185. {
  186. var vertexIndex = (int)mesh.Faces[face].V[facePart];
  187. var vertex = new GlobalVertex
  188. {
  189. Position = mesh.Verts[vertexIndex],
  190. Normal = computedMesh.vnormals[vertexIndex]._IPoint3
  191. };
  192. if (hasUV)
  193. {
  194. var tvertexIndex = (int)mesh.TvFace[face].T[facePart];
  195. vertex.UV = Loader.Global.Point2.Create(mesh.TVerts[tvertexIndex].X, mesh.TVerts[tvertexIndex].Y);
  196. }
  197. if (hasUV2)
  198. {
  199. var tvertexIndex = (int)mesh.MapFaces(2)[face].T[facePart];
  200. vertex.UV2 = Loader.Global.Point2.Create(mesh.MapVerts(2)[tvertexIndex].X, mesh.MapVerts(2)[tvertexIndex].Y);
  201. }
  202. var index = vertices.IndexOf(vertex);
  203. if (index > -1)
  204. {
  205. return index;
  206. }
  207. vertices.Add(vertex);
  208. return vertices.Count - 1;
  209. }
  210. }
  211. }