BabylonExporter.Mesh.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. var 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. var localTM = meshNode.GetObjectTM(0);
  129. if (meshNode.NodeParent != null)
  130. {
  131. var parentWorld = meshNode.NodeParent.GetObjectTM(0);
  132. localTM.MultiplyBy(parentWorld.Inverse);
  133. }
  134. var meshTrans = localTM.Translation;
  135. var meshRotation = localTM.Rotation;
  136. var meshScale = localTM.Scaling;
  137. babylonMesh.position = new[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  138. babylonMesh.rotationQuaternion = new[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
  139. babylonMesh.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };
  140. // Mesh
  141. RaiseMessage(meshNode.Name, 1);
  142. if (unskinnedMesh.IGameType == Autodesk.Max.IGameObject.ObjectTypes.Mesh && unskinnedMesh.MaxMesh != null)
  143. {
  144. if (unskinnedMesh.NumberOfFaces < 1)
  145. {
  146. RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name), 2);
  147. }
  148. if (unskinnedMesh.NumberOfVerts < 3)
  149. {
  150. RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name), 2);
  151. }
  152. if (unskinnedMesh.NumberOfVerts >= 65536)
  153. {
  154. 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);
  155. }
  156. // Physics
  157. var impostorText = meshNode.MaxNode.GetStringProperty("babylonjs_impostor", "None");
  158. if (impostorText != "None")
  159. {
  160. switch (impostorText)
  161. {
  162. case "Sphere":
  163. babylonMesh.physicsImpostor = 1;
  164. break;
  165. case "Box":
  166. babylonMesh.physicsImpostor = 2;
  167. break;
  168. case "Plane":
  169. babylonMesh.physicsImpostor = 3;
  170. break;
  171. default:
  172. babylonMesh.physicsImpostor = 0;
  173. break;
  174. }
  175. babylonMesh.physicsMass = meshNode.MaxNode.GetFloatProperty("babylonjs_mass");
  176. babylonMesh.physicsFriction = meshNode.MaxNode.GetFloatProperty("babylonjs_friction", 0.2f);
  177. babylonMesh.physicsRestitution = meshNode.MaxNode.GetFloatProperty("babylonjs_restitution", 0.2f);
  178. }
  179. // Material
  180. var mtl = meshNode.NodeMaterial;
  181. var multiMatsCount = 1;
  182. if (mtl != null)
  183. {
  184. babylonMesh.materialId = mtl.MaxMaterial.GetGuid().ToString();
  185. if (!referencedMaterials.Contains(mtl))
  186. {
  187. referencedMaterials.Add(mtl);
  188. }
  189. multiMatsCount = Math.Max(mtl.SubMaterialCount, 1);
  190. }
  191. babylonMesh.visibility = meshNode.MaxNode.GetVisibility(0, Tools.Forever);
  192. var vertices = new List<GlobalVertex>();
  193. var indices = new List<int>();
  194. var mappingChannels = unskinnedMesh.ActiveMapChannelNum;
  195. bool hasUV = false;
  196. bool hasUV2 = false;
  197. for (int i = 0; i < mappingChannels.Count; ++i)
  198. {
  199. var indexer = new IntPtr(i);
  200. var channelNum = mappingChannels[indexer];
  201. if (channelNum == 1)
  202. {
  203. hasUV = true;
  204. }
  205. else if (channelNum == 2)
  206. {
  207. hasUV2 = true;
  208. }
  209. }
  210. var hasColor = unskinnedMesh.NumberOfColorVerts > 0;
  211. var hasAlpha = unskinnedMesh.GetNumberOfMapVerts(-2) > 0;
  212. var optimizeVertices = meshNode.MaxNode.GetBoolProperty("babylonjs_optimizevertices");
  213. // Compute normals
  214. List<GlobalVertex>[] verticesAlreadyExported = null;
  215. if (optimizeVertices)
  216. {
  217. verticesAlreadyExported = new List<GlobalVertex>[unskinnedMesh.NumberOfVerts];
  218. }
  219. var subMeshes = new List<BabylonSubMesh>();
  220. var indexStart = 0;
  221. for (int i = 0; i < multiMatsCount; ++i)
  222. {
  223. int materialId = meshNode.NodeMaterial == null ? 0 : meshNode.NodeMaterial.GetMaterialID(i);
  224. var indexCount = 0;
  225. var minVertexIndex = int.MaxValue;
  226. var maxVertexIndex = int.MinValue;
  227. var subMesh = new BabylonSubMesh { indexStart = indexStart, materialIndex = i };
  228. if (multiMatsCount == 1)
  229. {
  230. for (int j = 0; j < unskinnedMesh.NumberOfFaces; ++j)
  231. {
  232. var face = unskinnedMesh.GetFace(j);
  233. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  234. }
  235. }
  236. else
  237. {
  238. ITab<IFaceEx> materialFaces = unskinnedMesh.GetFacesFromMatID(materialId);
  239. for (int j = 0; j < materialFaces.Count; ++j)
  240. {
  241. var faceIndexer = new IntPtr(j);
  242. var face = materialFaces[faceIndexer];
  243. Marshal.FreeHGlobal(faceIndexer);
  244. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  245. }
  246. }
  247. if (indexCount != 0)
  248. {
  249. subMesh.indexCount = indexCount;
  250. subMesh.verticesStart = minVertexIndex;
  251. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  252. indexStart += indexCount;
  253. subMeshes.Add(subMesh);
  254. }
  255. }
  256. if (vertices.Count >= 65536)
  257. {
  258. 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);
  259. if (!optimizeVertices)
  260. {
  261. RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
  262. }
  263. }
  264. RaiseMessage(string.Format("{0} vertices, {1} faces", vertices.Count, indices.Count / 3), 2);
  265. // Buffers
  266. babylonMesh.positions = vertices.SelectMany(v => new[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
  267. babylonMesh.normals = vertices.SelectMany(v => new[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
  268. if (hasUV)
  269. {
  270. babylonMesh.uvs = vertices.SelectMany(v => new[] { v.UV.X, 1 - v.UV.Y }).ToArray();
  271. }
  272. if (hasUV2)
  273. {
  274. babylonMesh.uvs2 = vertices.SelectMany(v => new[] { v.UV2.X, 1 - v.UV2.Y }).ToArray();
  275. }
  276. if (skin != null)
  277. {
  278. babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
  279. babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
  280. }
  281. if (hasColor)
  282. {
  283. babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
  284. babylonMesh.hasVertexAlpha = hasAlpha;
  285. }
  286. babylonMesh.subMeshes = subMeshes.ToArray();
  287. // Buffers - Indices
  288. babylonMesh.indices = indices.ToArray();
  289. }
  290. // Instances
  291. var tabs = Loader.Global.NodeTab.Create();
  292. Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs);
  293. var instances = new List<BabylonAbstractMesh>();
  294. for (var index = 0; index < tabs.Count; index++)
  295. {
  296. var indexer = new IntPtr(index);
  297. var tab = tabs[indexer];
  298. Marshal.FreeHGlobal(indexer);
  299. if (meshNode.MaxNode.GetGuid() == tab.GetGuid())
  300. {
  301. continue;
  302. }
  303. var instanceGameNode = scene.GetIGameNode(tab);
  304. if (instanceGameNode == null)
  305. {
  306. continue;
  307. }
  308. tab.MarkAsInstance();
  309. var instance = new BabylonAbstractMesh { name = tab.Name };
  310. {
  311. var instanceLocalTM = meshNode.GetObjectTM(0);
  312. var instanceTrans = instanceLocalTM.Translation;
  313. var instanceRotation = instanceLocalTM.Rotation;
  314. var instanceScale = instanceLocalTM.Scaling;
  315. instance.position = new[] { instanceTrans.X, instanceTrans.Y, instanceTrans.Z };
  316. float rotx = 0, roty = 0, rotz = 0;
  317. unsafe
  318. {
  319. instanceRotation.GetEuler(new IntPtr(&rotx), new IntPtr(&roty), new IntPtr(&rotz));
  320. }
  321. instance.rotation = new[] { rotx, roty, rotz };
  322. instance.scaling = new[] { instanceScale.X, instanceScale.Y, instanceScale.Z };
  323. }
  324. var instanceAnimations = new List<BabylonAnimation>();
  325. GenerateCoordinatesAnimations(meshNode, instanceAnimations);
  326. instance.animations = instanceAnimations.ToArray();
  327. instances.Add(instance);
  328. }
  329. babylonMesh.instances = instances.ToArray();
  330. // Animations
  331. var animations = new List<BabylonAnimation>();
  332. GenerateCoordinatesAnimations(meshNode, animations);
  333. if (!ExportFloatController(meshNode.MaxNode.VisController, "visibility", animations))
  334. {
  335. ExportFloatAnimation("visibility", animations, key => new[] { meshNode.MaxNode.GetVisibility(key, Tools.Forever) });
  336. }
  337. babylonMesh.animations = animations.ToArray();
  338. if (meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimate", 1))
  339. {
  340. babylonMesh.autoAnimate = true;
  341. babylonMesh.autoAnimateFrom = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  342. babylonMesh.autoAnimateTo = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to", 100);
  343. babylonMesh.autoAnimateLoop = meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop", 1);
  344. }
  345. babylonScene.MeshesList.Add(babylonMesh);
  346. }
  347. 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)
  348. {
  349. var a = CreateGlobalVertex(unskinnedMesh, face, 0, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  350. var b = CreateGlobalVertex(unskinnedMesh, face, 2, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  351. var c = CreateGlobalVertex(unskinnedMesh, face, 1, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  352. indices.Add(a);
  353. indices.Add(b);
  354. indices.Add(c);
  355. if (a < minVertexIndex)
  356. {
  357. minVertexIndex = a;
  358. }
  359. if (b < minVertexIndex)
  360. {
  361. minVertexIndex = b;
  362. }
  363. if (c < minVertexIndex)
  364. {
  365. minVertexIndex = c;
  366. }
  367. if (a > maxVertexIndex)
  368. {
  369. maxVertexIndex = a;
  370. }
  371. if (b > maxVertexIndex)
  372. {
  373. maxVertexIndex = b;
  374. }
  375. if (c > maxVertexIndex)
  376. {
  377. maxVertexIndex = c;
  378. }
  379. indexCount += 3;
  380. CheckCancelled();
  381. }
  382. public static void GenerateCoordinatesAnimations(IIGameNode meshNode, List<BabylonAnimation> animations)
  383. {
  384. if (meshNode.IGameControl.IsAnimated(IGameControlType.Pos) ||
  385. meshNode.IGameControl.IsAnimated(IGameControlType.PosX) ||
  386. meshNode.IGameControl.IsAnimated(IGameControlType.PosY) ||
  387. meshNode.IGameControl.IsAnimated(IGameControlType.PosZ))
  388. {
  389. ExportVector3Animation("position", 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 trans = worldMatrix.Translation;
  398. return new[] { trans.X, trans.Y, trans.Z };
  399. });
  400. }
  401. if (meshNode.IGameControl.IsAnimated(IGameControlType.Rot) ||
  402. meshNode.IGameControl.IsAnimated(IGameControlType.EulerX) ||
  403. meshNode.IGameControl.IsAnimated(IGameControlType.EulerY) ||
  404. meshNode.IGameControl.IsAnimated(IGameControlType.EulerZ))
  405. {
  406. ExportQuaternionAnimation("rotationQuaternion", animations, key =>
  407. {
  408. var worldMatrix = meshNode.GetObjectTM(key);
  409. if (meshNode.NodeParent != null)
  410. {
  411. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  412. worldMatrix.MultiplyBy(parentWorld.Inverse);
  413. }
  414. var rot = worldMatrix.Rotation;
  415. return new[] { rot.X, rot.Y, rot.Z, -rot.W };
  416. });
  417. }
  418. if (meshNode.IGameControl.IsAnimated(IGameControlType.Scale))
  419. {
  420. ExportVector3Animation("scaling", animations, key =>
  421. {
  422. var worldMatrix = meshNode.GetObjectTM(key);
  423. if (meshNode.NodeParent != null)
  424. {
  425. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  426. worldMatrix.MultiplyBy(parentWorld.Inverse);
  427. }
  428. var scale = worldMatrix.Scaling;
  429. return new[] { scale.X, scale.Y, scale.Z };
  430. });
  431. }
  432. }
  433. 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)
  434. {
  435. var vertexIndex = (int)face.Vert[facePart];
  436. var vertex = new GlobalVertex
  437. {
  438. BaseIndex = vertexIndex,
  439. Position = mesh.GetVertex(vertexIndex, true),
  440. Normal = mesh.GetNormal((int)face.Norm[facePart], true)
  441. };
  442. if (hasUV)
  443. {
  444. var indices = new int[3];
  445. unsafe
  446. {
  447. fixed (int* indicesPtr = indices)
  448. {
  449. mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
  450. }
  451. }
  452. var texCoord = mesh.GetMapVertex(1, indices[facePart]);
  453. vertex.UV = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  454. }
  455. if (hasUV2)
  456. {
  457. var indices = new int[3];
  458. unsafe
  459. {
  460. fixed (int* indicesPtr = indices)
  461. {
  462. mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  463. }
  464. }
  465. var texCoord = mesh.GetMapVertex(2, indices[facePart]);
  466. vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  467. }
  468. if (hasColor)
  469. {
  470. var vertexColorIndex = (int)face.Color[facePart];
  471. var vertexColor = mesh.GetColorVertex(vertexColorIndex);
  472. float alpha = 1;
  473. if (hasAlpha)
  474. {
  475. IPoint3 p = Loader.Global.Point3.Create();
  476. mesh.GetMapFaceIndex(-2, face.MeshFaceIndex, p.GetNativeHandle());
  477. alpha = p.X;
  478. }
  479. vertex.Color = new[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha };
  480. }
  481. if (skin != null)
  482. {
  483. float weight0 = 0;
  484. float weight1 = 0;
  485. float weight2 = 0;
  486. int bone0 = bonesCount;
  487. int bone1 = bonesCount;
  488. int bone2 = bonesCount;
  489. int bone3 = bonesCount;
  490. int nbBones = skin.GetNumberOfBones(vertexIndex);
  491. if (nbBones > 0)
  492. {
  493. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 0).NodeID);
  494. weight0 = skin.GetWeight(vertexIndex, 0);
  495. }
  496. if (nbBones > 1)
  497. {
  498. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 1).NodeID);
  499. weight1 = skin.GetWeight(vertexIndex, 1);
  500. }
  501. if (nbBones > 2)
  502. {
  503. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 2).NodeID);
  504. weight2 = skin.GetWeight(vertexIndex, 2);
  505. }
  506. if (nbBones > 3)
  507. {
  508. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 3).NodeID);
  509. }
  510. if (nbBones == 0)
  511. {
  512. weight0 = 1.0f;
  513. bone0 = bonesCount;
  514. }
  515. if (nbBones > 4)
  516. {
  517. RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2);
  518. }
  519. vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2);
  520. vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  521. }
  522. if (verticesAlreadyExported != null)
  523. {
  524. if (verticesAlreadyExported[vertexIndex] != null)
  525. {
  526. var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
  527. if (index > -1)
  528. {
  529. return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
  530. }
  531. }
  532. else
  533. {
  534. verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
  535. }
  536. vertex.CurrentIndex = vertices.Count;
  537. verticesAlreadyExported[vertexIndex].Add(vertex);
  538. }
  539. vertices.Add(vertex);
  540. return vertices.Count - 1;
  541. }
  542. }
  543. }