BabylonExporter.Mesh.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Autodesk.Max;
  5. using BabylonExport.Entities;
  6. using System.Runtime.InteropServices;
  7. using System.Threading.Tasks;
  8. namespace Max2Babylon
  9. {
  10. partial class BabylonExporter
  11. {
  12. Dictionary<IIGameSkin, List<int>> skinSortedBones = new Dictionary<IIGameSkin, List<int>>();
  13. List<int> SortBones(IIGameSkin skin)
  14. {
  15. List<int> boneIds = new List<int>();
  16. Dictionary<int, IIGameNode> boneIndex = new Dictionary<int, IIGameNode>();
  17. for (var index = 0; index < skin.TotalSkinBoneCount; index++)
  18. {
  19. var bone = skin.GetIGameBone(index, false);
  20. if (bone == null)
  21. {
  22. // non bone in skeletton
  23. boneIds.Add(-2);
  24. }
  25. else
  26. {
  27. boneIds.Add(bone.NodeID);
  28. boneIndex[bone.NodeID] = bone;
  29. }
  30. }
  31. while (true)
  32. {
  33. bool foundMisMatch = false;
  34. for (int i = 0; i < boneIds.Count; ++i)
  35. {
  36. var id = boneIds[i];
  37. if (id == -2)
  38. {
  39. continue;
  40. }
  41. var parent = boneIndex[id].NodeParent;
  42. if (parent != null)
  43. {
  44. var parentId = parent.NodeID;
  45. if (boneIds.IndexOf(parentId) > i)
  46. {
  47. boneIds.RemoveAt(i);
  48. boneIds.Insert(boneIds.IndexOf(parentId) + 1, id);
  49. foundMisMatch = true;
  50. break;
  51. }
  52. }
  53. }
  54. if (!foundMisMatch)
  55. {
  56. break;
  57. }
  58. }
  59. return boneIds;
  60. }
  61. private int bonesCount;
  62. private void ExportMesh(IIGameScene scene, IIGameNode meshNode, BabylonScene babylonScene)
  63. {
  64. if (meshNode.MaxNode.IsInstance())
  65. {
  66. return;
  67. }
  68. if (meshNode.MaxNode.GetBoolProperty("babylonjs_noexport"))
  69. {
  70. return;
  71. }
  72. if (!ExportHiddenObjects && meshNode.MaxNode.IsHidden(NodeHideFlags.None, false))
  73. {
  74. return;
  75. }
  76. var gameMesh = meshNode.IGameObject.AsGameMesh();
  77. bool initialized = gameMesh.InitializeData; //needed, the property is in fact a method initializing the exporter that has wrongly been auto
  78. // translated into a property because it has no parameters
  79. var babylonMesh = new BabylonMesh();
  80. babylonMesh.name = meshNode.Name;
  81. babylonMesh.id = meshNode.MaxNode.GetGuid().ToString();
  82. if (meshNode.NodeParent != null)
  83. {
  84. // parent node type (if a known object type, export it as an empty mesh, so it can be in the scene hierarchy)
  85. var parentType = meshNode.NodeParent.IGameObject.IGameType;
  86. var parentId = meshNode.NodeParent.MaxNode.GetGuid().ToString();
  87. switch (parentType)
  88. {
  89. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  90. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  91. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  92. break;
  93. default:
  94. if (!babylonScene.MeshesList.Where(m => m.id == parentId).Any())
  95. {
  96. ExportMesh(scene, meshNode.NodeParent, babylonScene);
  97. }
  98. break;
  99. }
  100. babylonMesh.parentId = parentId;
  101. }
  102. // Misc.
  103. babylonMesh.isVisible = meshNode.MaxNode.Renderable == 1;
  104. babylonMesh.pickable = meshNode.MaxNode.GetBoolProperty("babylonjs_checkpickable");
  105. babylonMesh.receiveShadows = meshNode.MaxNode.RcvShadows == 1;
  106. babylonMesh.showBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showboundingbox");
  107. babylonMesh.showSubMeshesBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showsubmeshesboundingbox");
  108. babylonMesh.applyFog = meshNode.MaxNode.ApplyAtmospherics == 1;
  109. babylonMesh.alphaIndex = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_alphaindex", 1000);
  110. // Collisions
  111. babylonMesh.checkCollisions = meshNode.MaxNode.GetBoolProperty("babylonjs_checkcollisions");
  112. bool isSkinned = gameMesh.IsObjectSkinned;
  113. var skin = gameMesh.IGameSkin;
  114. var unskinnedMesh = gameMesh;
  115. IGMatrix skinInitPoseMatrix = Loader.Global.GMatrix.Create(Loader.Global.Matrix3.Create(true));
  116. List<int> boneIds = null;
  117. if (isSkinned)
  118. {
  119. bonesCount = skin.TotalSkinBoneCount;
  120. skins.Add(skin);
  121. skinnedNodes.Add(meshNode);
  122. babylonMesh.skeletonId = skins.IndexOf(skin);
  123. skin.GetInitSkinTM(skinInitPoseMatrix);
  124. boneIds = SortBones(skin);
  125. skinSortedBones[skin] = boneIds;
  126. }
  127. // Position / rotation / scaling
  128. {
  129. var localTM = meshNode.GetObjectTM(0);
  130. if (meshNode.NodeParent != null)
  131. {
  132. var parentWorld = meshNode.NodeParent.GetObjectTM(0);
  133. localTM.MultiplyBy(parentWorld.Inverse);
  134. }
  135. var meshTrans = localTM.Translation;
  136. var meshRotation = localTM.Rotation;
  137. var meshScale = localTM.Scaling;
  138. babylonMesh.position = new float[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  139. babylonMesh.rotationQuaternion = new float[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
  140. babylonMesh.scaling = new float[] { meshScale.X, meshScale.Y, meshScale.Z };
  141. }
  142. // Mesh
  143. RaiseMessage(meshNode.Name, 1);
  144. if (unskinnedMesh != null && unskinnedMesh.IGameType == Autodesk.Max.IGameObject.ObjectTypes.Mesh && unskinnedMesh.MaxMesh != null)
  145. {
  146. if (unskinnedMesh.NumberOfFaces < 1)
  147. {
  148. RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name), 2);
  149. }
  150. if (unskinnedMesh.NumberOfVerts < 3)
  151. {
  152. RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name), 2);
  153. }
  154. if (unskinnedMesh.NumberOfVerts >= 65536)
  155. {
  156. RaiseWarning(string.Format("Mesh {0} has tmore than 65536 vertices which means that it will require specific WebGL extension to be rendered. This may impact portability of your scene on low end devices.", babylonMesh.name), 2);
  157. }
  158. // Material
  159. var mtl = meshNode.NodeMaterial;
  160. var multiMatsCount = 1;
  161. if (mtl != null)
  162. {
  163. babylonMesh.materialId = mtl.MaxMaterial.GetGuid().ToString();
  164. if (!referencedMaterials.Contains(mtl))
  165. {
  166. referencedMaterials.Add(mtl);
  167. }
  168. multiMatsCount = Math.Max(mtl.SubMaterialCount, 1);
  169. }
  170. babylonMesh.visibility = meshNode.MaxNode.GetVisibility(0, Tools.Forever);
  171. var vertices = new List<GlobalVertex>();
  172. var indices = new List<int>();
  173. var mappingChannels = unskinnedMesh.ActiveMapChannelNum;
  174. bool hasUV = false;
  175. bool hasUV2 = false;
  176. for (int i = 0; i < mappingChannels.Count; ++i)
  177. {
  178. IntPtr indexer = new IntPtr(i);
  179. var channelNum = mappingChannels[indexer];
  180. if (channelNum == 1)
  181. {
  182. hasUV = true;
  183. }
  184. else if (channelNum == 2)
  185. {
  186. hasUV2 = true;
  187. }
  188. }
  189. var hasColor = unskinnedMesh.NumberOfColorVerts > 0;
  190. var hasAlpha = unskinnedMesh.GetNumberOfMapVerts(-2) > 0;
  191. var optimizeVertices = meshNode.MaxNode.GetBoolProperty("babylonjs_optimizevertices");
  192. // Compute normals
  193. List<GlobalVertex>[] verticesAlreadyExported = null;
  194. if (optimizeVertices)
  195. {
  196. verticesAlreadyExported = new List<GlobalVertex>[unskinnedMesh.NumberOfVerts];
  197. }
  198. var subMeshes = new List<BabylonSubMesh>();
  199. var indexStart = 0;
  200. for (int i = 0; i < multiMatsCount; ++i)
  201. {
  202. if (meshNode.NodeMaterial == null)
  203. {
  204. continue;
  205. }
  206. int materialId = meshNode.NodeMaterial.GetMaterialID(i);
  207. ITab<IFaceEx> materialFaces = null;
  208. var indexCount = 0;
  209. var minVertexIndex = int.MaxValue;
  210. var maxVertexIndex = int.MinValue;
  211. var subMesh = new BabylonSubMesh();
  212. subMesh.indexStart = indexStart;
  213. subMesh.materialIndex = i;
  214. if (multiMatsCount == 1)
  215. {
  216. for (int j = 0; j < unskinnedMesh.NumberOfFaces; ++j)
  217. {
  218. var face = unskinnedMesh.GetFace(j);
  219. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  220. }
  221. }
  222. else
  223. {
  224. materialFaces = unskinnedMesh.GetFacesFromMatID(materialId);
  225. for (int j = 0; j < materialFaces.Count; ++j)
  226. {
  227. var faceIndexer = new IntPtr(j);
  228. var face = materialFaces[faceIndexer];
  229. Marshal.FreeHGlobal(faceIndexer);
  230. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  231. }
  232. }
  233. if (indexCount != 0)
  234. {
  235. subMesh.indexCount = indexCount;
  236. subMesh.verticesStart = minVertexIndex;
  237. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  238. indexStart += indexCount;
  239. subMeshes.Add(subMesh);
  240. }
  241. }
  242. if (vertices.Count >= 65536)
  243. {
  244. RaiseWarning(string.Format("Mesh {0} has {1} vertices. This may prevent your scene to work on low end devices where 32 bits indice are not supported", babylonMesh.name, vertices.Count), 2);
  245. if (!optimizeVertices)
  246. {
  247. RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
  248. }
  249. }
  250. RaiseMessage(string.Format("{0} vertices, {1} faces", vertices.Count, indices.Count / 3), 2);
  251. // Buffers
  252. babylonMesh.positions = vertices.SelectMany(v => new float[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
  253. babylonMesh.normals = vertices.SelectMany(v => new float[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
  254. if (hasUV)
  255. {
  256. babylonMesh.uvs = vertices.SelectMany(v => new float[] { v.UV.X, 1 - v.UV.Y }).ToArray();
  257. }
  258. if (hasUV2)
  259. {
  260. babylonMesh.uvs2 = vertices.SelectMany(v => new float[] { v.UV2.X, 1 - v.UV2.Y }).ToArray();
  261. }
  262. if (skin != null)
  263. {
  264. babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
  265. babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
  266. }
  267. if (hasColor)
  268. {
  269. babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
  270. babylonMesh.hasVertexAlpha = hasAlpha;
  271. }
  272. babylonMesh.subMeshes = subMeshes.ToArray();
  273. // Buffers - Indices
  274. babylonMesh.indices = indices.ToArray();
  275. }
  276. // Instances
  277. var tabs = Loader.Global.NodeTab.Create();
  278. Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs);
  279. var instances = new List<BabylonAbstractMesh>();
  280. for (var index = 0; index < tabs.Count; index++)
  281. {
  282. var indexer = new IntPtr(index);
  283. var tab = tabs[indexer];
  284. Marshal.FreeHGlobal(indexer);
  285. if (meshNode.MaxNode.GetGuid() == tab.GetGuid())
  286. {
  287. continue;
  288. }
  289. var instanceGameNode = scene.GetIGameNode(tab);
  290. if (instanceGameNode == null)
  291. {
  292. continue;
  293. }
  294. tab.MarkAsInstance();
  295. var instance = new BabylonAbstractMesh { name = tab.Name };
  296. {
  297. var localTM = meshNode.GetObjectTM(0);
  298. var meshTrans = localTM.Translation;
  299. var meshRotation = localTM.Rotation;
  300. var meshScale = localTM.Scaling;
  301. instance.position = new float[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  302. float rotx = 0, roty = 0, rotz = 0;
  303. unsafe
  304. {
  305. meshRotation.GetEuler(new IntPtr(&rotx), new IntPtr(&roty), new IntPtr(&rotz));
  306. }
  307. instance.rotation = new float[] { rotx, roty, rotz };
  308. instance.scaling = new float[] { meshScale.X, meshScale.Y, meshScale.Z };
  309. }
  310. var instanceAnimations = new List<BabylonAnimation>();
  311. GenerateCoordinatesAnimations(meshNode, instanceAnimations);
  312. instance.animations = instanceAnimations.ToArray();
  313. instances.Add(instance);
  314. }
  315. babylonMesh.instances = instances.ToArray();
  316. // Animations
  317. var animations = new List<BabylonAnimation>();
  318. GenerateCoordinatesAnimations(meshNode, animations);
  319. if (!ExportFloatController(meshNode.MaxNode.VisController, "visibility", animations))
  320. {
  321. ExportFloatAnimation("visibility", animations, key => new[] { meshNode.MaxNode.GetVisibility(key, Tools.Forever) });
  322. }
  323. babylonMesh.animations = animations.ToArray();
  324. if (meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimate", 1))
  325. {
  326. babylonMesh.autoAnimate = true;
  327. babylonMesh.autoAnimateFrom = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  328. babylonMesh.autoAnimateTo = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to", 100);
  329. babylonMesh.autoAnimateLoop = meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop", 1);
  330. }
  331. babylonScene.MeshesList.Add(babylonMesh);
  332. }
  333. private void ExtractFace(IIGameSkin skin, IIGameMesh unskinnedMesh, List<GlobalVertex> vertices, List<int> indices, bool hasUV, bool hasUV2, bool hasColor, bool hasAlpha, List<GlobalVertex>[] verticesAlreadyExported, ref int indexCount, ref int minVertexIndex, ref int maxVertexIndex, IFaceEx face, List<int> boneIds)
  334. {
  335. var a = CreateGlobalVertex(unskinnedMesh, face, 0, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  336. var b = CreateGlobalVertex(unskinnedMesh, face, 2, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  337. var c = CreateGlobalVertex(unskinnedMesh, face, 1, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  338. indices.Add(a);
  339. indices.Add(b);
  340. indices.Add(c);
  341. if (a < minVertexIndex)
  342. {
  343. minVertexIndex = a;
  344. }
  345. if (b < minVertexIndex)
  346. {
  347. minVertexIndex = b;
  348. }
  349. if (c < minVertexIndex)
  350. {
  351. minVertexIndex = c;
  352. }
  353. if (a > maxVertexIndex)
  354. {
  355. maxVertexIndex = a;
  356. }
  357. if (b > maxVertexIndex)
  358. {
  359. maxVertexIndex = b;
  360. }
  361. if (c > maxVertexIndex)
  362. {
  363. maxVertexIndex = c;
  364. }
  365. indexCount += 3;
  366. CheckCancelled();
  367. }
  368. public static void GenerateCoordinatesAnimations(IIGameNode meshNode, List<BabylonAnimation> animations)
  369. {
  370. ExportVector3Animation("position", animations, key =>
  371. {
  372. var worldMatrix = meshNode.GetObjectTM(key);
  373. if (meshNode.NodeParent != null)
  374. {
  375. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  376. worldMatrix.MultiplyBy(parentWorld.Inverse);
  377. }
  378. var trans = worldMatrix.Translation;
  379. return new float[] { trans.X, trans.Y, trans.Z };
  380. });
  381. ExportQuaternionAnimation("rotationQuaternion", animations, key =>
  382. {
  383. var worldMatrix = meshNode.GetObjectTM(key);
  384. if (meshNode.NodeParent != null)
  385. {
  386. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  387. worldMatrix.MultiplyBy(parentWorld.Inverse);
  388. }
  389. var rot = worldMatrix.Rotation;
  390. return new float[] { rot.X, rot.Y, rot.Z, -rot.W };
  391. });
  392. ExportVector3Animation("scaling", animations, key =>
  393. {
  394. var worldMatrix = meshNode.GetObjectTM(key);
  395. if (meshNode.NodeParent != null)
  396. {
  397. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  398. worldMatrix.MultiplyBy(parentWorld.Inverse);
  399. }
  400. var scale = worldMatrix.Scaling;
  401. return new float[] { scale.X, scale.Y, scale.Z };
  402. });
  403. }
  404. int CreateGlobalVertex(IIGameMesh mesh, IFaceEx face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, bool hasColor, bool hasAlpha, List<GlobalVertex>[] verticesAlreadyExported, IIGameSkin skin, List<int> boneIds)
  405. {
  406. var vertexIndex = (int)face.Vert[facePart];
  407. var vertex = new GlobalVertex
  408. {
  409. BaseIndex = vertexIndex,
  410. Position = mesh.GetVertex(vertexIndex, true),
  411. Normal = mesh.GetNormal((int)face.Norm[facePart], true)
  412. };
  413. if (hasUV)
  414. {
  415. int[] indices = new int[3];
  416. unsafe
  417. {
  418. fixed (int* indicesPtr = indices)
  419. {
  420. mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
  421. }
  422. }
  423. var texCoord = mesh.GetMapVertex(1, indices[facePart]);
  424. vertex.UV = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  425. }
  426. if (hasUV2)
  427. {
  428. int[] indices = new int[3];
  429. unsafe
  430. {
  431. fixed (int* indicesPtr = indices)
  432. {
  433. mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  434. }
  435. }
  436. var texCoord = mesh.GetMapVertex(2, indices[facePart]);
  437. vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  438. }
  439. if (hasColor)
  440. {
  441. var vertexColorIndex = (int)face.Color[facePart];
  442. var vertexColor = mesh.GetColorVertex(vertexColorIndex);
  443. float alpha = 1;
  444. if (hasAlpha)
  445. {
  446. IPoint3 p = Loader.Global.Point3.Create();
  447. mesh.GetMapFaceIndex(-2, face.MeshFaceIndex, p.GetNativeHandle());
  448. alpha = p.X;
  449. }
  450. vertex.Color = new float[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha };
  451. }
  452. if (skin != null)
  453. {
  454. float weight0 = 0;
  455. float weight1 = 0;
  456. float weight2 = 0;
  457. int bone0 = bonesCount;
  458. int bone1 = bonesCount;
  459. int bone2 = bonesCount;
  460. int bone3 = bonesCount;
  461. int nbBones = skin.GetNumberOfBones(vertexIndex);
  462. if (nbBones > 0)
  463. {
  464. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 0).NodeID);
  465. weight0 = skin.GetWeight(vertexIndex, 0);
  466. }
  467. if (nbBones > 1)
  468. {
  469. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 1).NodeID);
  470. weight1 = skin.GetWeight(vertexIndex, 1);
  471. }
  472. if (nbBones > 2)
  473. {
  474. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 2).NodeID);
  475. weight2 = skin.GetWeight(vertexIndex, 2);
  476. }
  477. if (nbBones > 3)
  478. {
  479. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 3).NodeID);
  480. }
  481. if (nbBones == 0)
  482. {
  483. weight0 = 1.0f;
  484. bone0 = bonesCount;
  485. }
  486. if (nbBones > 4)
  487. {
  488. RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2);
  489. }
  490. vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2);
  491. vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  492. }
  493. if (verticesAlreadyExported != null)
  494. {
  495. if (verticesAlreadyExported[vertexIndex] != null)
  496. {
  497. var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
  498. if (index > -1)
  499. {
  500. return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
  501. }
  502. }
  503. else
  504. {
  505. verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
  506. }
  507. vertex.CurrentIndex = vertices.Count;
  508. verticesAlreadyExported[vertexIndex].Add(vertex);
  509. }
  510. vertices.Add(vertex);
  511. return vertices.Count - 1;
  512. }
  513. }
  514. }