BabylonExporter.Mesh.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. namespace Max2Babylon
  8. {
  9. partial class BabylonExporter
  10. {
  11. readonly Dictionary<IIGameSkin, List<int>> skinSortedBones = new Dictionary<IIGameSkin, List<int>>();
  12. List<int> SortBones(IIGameSkin skin)
  13. {
  14. var boneIds = new List<int>();
  15. var boneIndex = new Dictionary<int, IIGameNode>();
  16. for (var index = 0; index < skin.TotalSkinBoneCount; index++)
  17. {
  18. var bone = skin.GetIGameBone(index, false);
  19. if (bone == null)
  20. {
  21. // non bone in skeletton
  22. boneIds.Add(-2);
  23. }
  24. else
  25. {
  26. boneIds.Add(bone.NodeID);
  27. boneIndex[bone.NodeID] = bone;
  28. }
  29. }
  30. while (true)
  31. {
  32. bool foundMisMatch = false;
  33. for (int i = 0; i < boneIds.Count; ++i)
  34. {
  35. var id = boneIds[i];
  36. if (id == -2)
  37. {
  38. continue;
  39. }
  40. var parent = boneIndex[id].NodeParent;
  41. if (parent != null)
  42. {
  43. var parentId = parent.NodeID;
  44. if (boneIds.IndexOf(parentId) > i)
  45. {
  46. boneIds.RemoveAt(i);
  47. boneIds.Insert(boneIds.IndexOf(parentId) + 1, id);
  48. foundMisMatch = true;
  49. break;
  50. }
  51. }
  52. }
  53. if (!foundMisMatch)
  54. {
  55. break;
  56. }
  57. }
  58. return boneIds;
  59. }
  60. private string GetParentID(IIGameNode parentNode, BabylonScene babylonScene, IIGameScene scene)
  61. {
  62. var parentType = parentNode.IGameObject.IGameType;
  63. var parentId = parentNode.MaxNode.GetGuid().ToString();
  64. switch (parentType)
  65. {
  66. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  67. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  68. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  69. break;
  70. default:
  71. if (babylonScene.MeshesList.All(m => m.id != parentId))
  72. {
  73. ExportMesh(scene, parentNode, babylonScene);
  74. }
  75. break;
  76. }
  77. return parentId;
  78. }
  79. private int bonesCount;
  80. private void ExportMesh(IIGameScene scene, IIGameNode meshNode, BabylonScene babylonScene)
  81. {
  82. if (meshNode.MaxNode.IsInstance())
  83. {
  84. return;
  85. }
  86. if (meshNode.MaxNode.GetBoolProperty("babylonjs_noexport"))
  87. {
  88. return;
  89. }
  90. if (!ExportHiddenObjects && meshNode.MaxNode.IsHidden(NodeHideFlags.None, false))
  91. {
  92. return;
  93. }
  94. var gameMesh = meshNode.IGameObject.AsGameMesh();
  95. bool initialized = gameMesh.InitializeData; //needed, the property is in fact a method initializing the exporter that has wrongly been auto
  96. // translated into a property because it has no parameters
  97. var babylonMesh = new BabylonMesh { name = meshNode.Name, id = meshNode.MaxNode.GetGuid().ToString() };
  98. if (meshNode.NodeParent != null)
  99. {
  100. babylonMesh.parentId = GetParentID(meshNode.NodeParent, babylonScene, scene);
  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[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  139. babylonMesh.rotationQuaternion = new[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
  140. babylonMesh.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };
  141. }
  142. // Mesh
  143. RaiseMessage(meshNode.Name, 1);
  144. if (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. var 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. var indexCount = 0;
  208. var minVertexIndex = int.MaxValue;
  209. var maxVertexIndex = int.MinValue;
  210. var subMesh = new BabylonSubMesh { indexStart = indexStart, materialIndex = i };
  211. if (multiMatsCount == 1)
  212. {
  213. for (int j = 0; j < unskinnedMesh.NumberOfFaces; ++j)
  214. {
  215. var face = unskinnedMesh.GetFace(j);
  216. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  217. }
  218. }
  219. else
  220. {
  221. ITab<IFaceEx> materialFaces = unskinnedMesh.GetFacesFromMatID(materialId);
  222. for (int j = 0; j < materialFaces.Count; ++j)
  223. {
  224. var faceIndexer = new IntPtr(j);
  225. var face = materialFaces[faceIndexer];
  226. Marshal.FreeHGlobal(faceIndexer);
  227. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  228. }
  229. }
  230. if (indexCount != 0)
  231. {
  232. subMesh.indexCount = indexCount;
  233. subMesh.verticesStart = minVertexIndex;
  234. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  235. indexStart += indexCount;
  236. subMeshes.Add(subMesh);
  237. }
  238. }
  239. if (vertices.Count >= 65536)
  240. {
  241. 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);
  242. if (!optimizeVertices)
  243. {
  244. RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
  245. }
  246. }
  247. RaiseMessage(string.Format("{0} vertices, {1} faces", vertices.Count, indices.Count / 3), 2);
  248. // Buffers
  249. babylonMesh.positions = vertices.SelectMany(v => new[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
  250. babylonMesh.normals = vertices.SelectMany(v => new[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
  251. if (hasUV)
  252. {
  253. babylonMesh.uvs = vertices.SelectMany(v => new[] { v.UV.X, 1 - v.UV.Y }).ToArray();
  254. }
  255. if (hasUV2)
  256. {
  257. babylonMesh.uvs2 = vertices.SelectMany(v => new[] { v.UV2.X, 1 - v.UV2.Y }).ToArray();
  258. }
  259. if (skin != null)
  260. {
  261. babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
  262. babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
  263. }
  264. if (hasColor)
  265. {
  266. babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
  267. babylonMesh.hasVertexAlpha = hasAlpha;
  268. }
  269. babylonMesh.subMeshes = subMeshes.ToArray();
  270. // Buffers - Indices
  271. babylonMesh.indices = indices.ToArray();
  272. }
  273. // Instances
  274. var tabs = Loader.Global.NodeTab.Create();
  275. Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs);
  276. var instances = new List<BabylonAbstractMesh>();
  277. for (var index = 0; index < tabs.Count; index++)
  278. {
  279. var indexer = new IntPtr(index);
  280. var tab = tabs[indexer];
  281. Marshal.FreeHGlobal(indexer);
  282. if (meshNode.MaxNode.GetGuid() == tab.GetGuid())
  283. {
  284. continue;
  285. }
  286. var instanceGameNode = scene.GetIGameNode(tab);
  287. if (instanceGameNode == null)
  288. {
  289. continue;
  290. }
  291. tab.MarkAsInstance();
  292. var instance = new BabylonAbstractMesh { name = tab.Name };
  293. {
  294. var localTM = meshNode.GetObjectTM(0);
  295. var meshTrans = localTM.Translation;
  296. var meshRotation = localTM.Rotation;
  297. var meshScale = localTM.Scaling;
  298. instance.position = new[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  299. float rotx = 0, roty = 0, rotz = 0;
  300. unsafe
  301. {
  302. meshRotation.GetEuler(new IntPtr(&rotx), new IntPtr(&roty), new IntPtr(&rotz));
  303. }
  304. instance.rotation = new[] { rotx, roty, rotz };
  305. instance.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };
  306. }
  307. var instanceAnimations = new List<BabylonAnimation>();
  308. GenerateCoordinatesAnimations(meshNode, instanceAnimations);
  309. instance.animations = instanceAnimations.ToArray();
  310. instances.Add(instance);
  311. }
  312. babylonMesh.instances = instances.ToArray();
  313. // Animations
  314. var animations = new List<BabylonAnimation>();
  315. GenerateCoordinatesAnimations(meshNode, animations);
  316. if (!ExportFloatController(meshNode.MaxNode.VisController, "visibility", animations))
  317. {
  318. ExportFloatAnimation("visibility", animations, key => new[] { meshNode.MaxNode.GetVisibility(key, Tools.Forever) });
  319. }
  320. babylonMesh.animations = animations.ToArray();
  321. if (meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimate", 1))
  322. {
  323. babylonMesh.autoAnimate = true;
  324. babylonMesh.autoAnimateFrom = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  325. babylonMesh.autoAnimateTo = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to", 100);
  326. babylonMesh.autoAnimateLoop = meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop", 1);
  327. }
  328. babylonScene.MeshesList.Add(babylonMesh);
  329. }
  330. 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)
  331. {
  332. var a = CreateGlobalVertex(unskinnedMesh, face, 0, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  333. var b = CreateGlobalVertex(unskinnedMesh, face, 2, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  334. var c = CreateGlobalVertex(unskinnedMesh, face, 1, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  335. indices.Add(a);
  336. indices.Add(b);
  337. indices.Add(c);
  338. if (a < minVertexIndex)
  339. {
  340. minVertexIndex = a;
  341. }
  342. if (b < minVertexIndex)
  343. {
  344. minVertexIndex = b;
  345. }
  346. if (c < minVertexIndex)
  347. {
  348. minVertexIndex = c;
  349. }
  350. if (a > maxVertexIndex)
  351. {
  352. maxVertexIndex = a;
  353. }
  354. if (b > maxVertexIndex)
  355. {
  356. maxVertexIndex = b;
  357. }
  358. if (c > maxVertexIndex)
  359. {
  360. maxVertexIndex = c;
  361. }
  362. indexCount += 3;
  363. CheckCancelled();
  364. }
  365. public static void GenerateCoordinatesAnimations(IIGameNode meshNode, List<BabylonAnimation> animations)
  366. {
  367. if (meshNode.IGameControl.IsAnimated(IGameControlType.Pos) ||
  368. meshNode.IGameControl.IsAnimated(IGameControlType.PosX) ||
  369. meshNode.IGameControl.IsAnimated(IGameControlType.PosY) ||
  370. meshNode.IGameControl.IsAnimated(IGameControlType.PosZ))
  371. {
  372. ExportVector3Animation("position", animations, key =>
  373. {
  374. var worldMatrix = meshNode.GetObjectTM(key);
  375. if (meshNode.NodeParent != null)
  376. {
  377. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  378. worldMatrix.MultiplyBy(parentWorld.Inverse);
  379. }
  380. var trans = worldMatrix.Translation;
  381. return new[] {trans.X, trans.Y, trans.Z};
  382. });
  383. }
  384. if (meshNode.IGameControl.IsAnimated(IGameControlType.Rot) ||
  385. meshNode.IGameControl.IsAnimated(IGameControlType.EulerX) ||
  386. meshNode.IGameControl.IsAnimated(IGameControlType.EulerY) ||
  387. meshNode.IGameControl.IsAnimated(IGameControlType.EulerZ))
  388. {
  389. ExportQuaternionAnimation("rotationQuaternion", animations, key =>
  390. {
  391. var worldMatrix = meshNode.GetObjectTM(key);
  392. if (meshNode.NodeParent != null)
  393. {
  394. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  395. worldMatrix.MultiplyBy(parentWorld.Inverse);
  396. }
  397. var rot = worldMatrix.Rotation;
  398. return new[] {rot.X, rot.Y, rot.Z, -rot.W};
  399. });
  400. }
  401. if (meshNode.IGameControl.IsAnimated(IGameControlType.Scale))
  402. {
  403. ExportVector3Animation("scaling", animations, key =>
  404. {
  405. var worldMatrix = meshNode.GetObjectTM(key);
  406. if (meshNode.NodeParent != null)
  407. {
  408. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  409. worldMatrix.MultiplyBy(parentWorld.Inverse);
  410. }
  411. var scale = worldMatrix.Scaling;
  412. return new[] {scale.X, scale.Y, scale.Z};
  413. });
  414. }
  415. }
  416. 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)
  417. {
  418. var vertexIndex = (int)face.Vert[facePart];
  419. var vertex = new GlobalVertex
  420. {
  421. BaseIndex = vertexIndex,
  422. Position = mesh.GetVertex(vertexIndex, true),
  423. Normal = mesh.GetNormal((int)face.Norm[facePart], true)
  424. };
  425. if (hasUV)
  426. {
  427. var indices = new int[3];
  428. unsafe
  429. {
  430. fixed (int* indicesPtr = indices)
  431. {
  432. mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
  433. }
  434. }
  435. var texCoord = mesh.GetMapVertex(1, indices[facePart]);
  436. vertex.UV = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  437. }
  438. if (hasUV2)
  439. {
  440. var indices = new int[3];
  441. unsafe
  442. {
  443. fixed (int* indicesPtr = indices)
  444. {
  445. mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  446. }
  447. }
  448. var texCoord = mesh.GetMapVertex(2, indices[facePart]);
  449. vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  450. }
  451. if (hasColor)
  452. {
  453. var vertexColorIndex = (int)face.Color[facePart];
  454. var vertexColor = mesh.GetColorVertex(vertexColorIndex);
  455. float alpha = 1;
  456. if (hasAlpha)
  457. {
  458. IPoint3 p = Loader.Global.Point3.Create();
  459. mesh.GetMapFaceIndex(-2, face.MeshFaceIndex, p.GetNativeHandle());
  460. alpha = p.X;
  461. }
  462. vertex.Color = new[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha };
  463. }
  464. if (skin != null)
  465. {
  466. float weight0 = 0;
  467. float weight1 = 0;
  468. float weight2 = 0;
  469. int bone0 = bonesCount;
  470. int bone1 = bonesCount;
  471. int bone2 = bonesCount;
  472. int bone3 = bonesCount;
  473. int nbBones = skin.GetNumberOfBones(vertexIndex);
  474. if (nbBones > 0)
  475. {
  476. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 0).NodeID);
  477. weight0 = skin.GetWeight(vertexIndex, 0);
  478. }
  479. if (nbBones > 1)
  480. {
  481. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 1).NodeID);
  482. weight1 = skin.GetWeight(vertexIndex, 1);
  483. }
  484. if (nbBones > 2)
  485. {
  486. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 2).NodeID);
  487. weight2 = skin.GetWeight(vertexIndex, 2);
  488. }
  489. if (nbBones > 3)
  490. {
  491. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 3).NodeID);
  492. }
  493. if (nbBones == 0)
  494. {
  495. weight0 = 1.0f;
  496. bone0 = bonesCount;
  497. }
  498. if (nbBones > 4)
  499. {
  500. RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2);
  501. }
  502. vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2);
  503. vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  504. }
  505. if (verticesAlreadyExported != null)
  506. {
  507. if (verticesAlreadyExported[vertexIndex] != null)
  508. {
  509. var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
  510. if (index > -1)
  511. {
  512. return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
  513. }
  514. }
  515. else
  516. {
  517. verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
  518. }
  519. vertex.CurrentIndex = vertices.Count;
  520. verticesAlreadyExported[vertexIndex].Add(vertex);
  521. }
  522. vertices.Add(vertex);
  523. return vertices.Count - 1;
  524. }
  525. }
  526. }