BabylonExporter.Mesh.cs 12 KB

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