BabylonExporter.Mesh.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Autodesk.Max;
  6. using BabylonExport.Entities;
  7. using System.Runtime.InteropServices;
  8. namespace Max2Babylon
  9. {
  10. partial class BabylonExporter
  11. {
  12. readonly Dictionary<IIGameSkin, List<int>> skinSortedBones = new Dictionary<IIGameSkin, List<int>>();
  13. List<int> SortBones(IIGameSkin skin)
  14. {
  15. var boneIds = new List<int>();
  16. var 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 string GetParentID(IIGameNode parentNode, BabylonScene babylonScene, IIGameScene scene)
  62. {
  63. var parentType = parentNode.IGameObject.IGameType;
  64. var parentId = parentNode.MaxNode.GetGuid().ToString();
  65. switch (parentType)
  66. {
  67. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  68. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  69. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  70. break;
  71. default:
  72. if (babylonScene.MeshesList.All(m => m.id != parentId))
  73. {
  74. ExportMesh(scene, parentNode, babylonScene);
  75. }
  76. break;
  77. }
  78. return parentId;
  79. }
  80. private void RotationToEulerAngles(BabylonAbstractMesh babylonMesh, IQuat rotation)
  81. {
  82. float rotx = 0, roty = 0, rotz = 0;
  83. unsafe
  84. {
  85. rotation.GetEuler(new IntPtr(&rotx), new IntPtr(&roty), new IntPtr(&rotz));
  86. }
  87. babylonMesh.rotation = new[] { rotx, roty, rotz };
  88. }
  89. private int bonesCount;
  90. private void ExportMesh(IIGameScene scene, IIGameNode meshNode, BabylonScene babylonScene)
  91. {
  92. if (meshNode.MaxNode.IsInstance())
  93. {
  94. return;
  95. }
  96. if (meshNode.MaxNode.GetBoolProperty("babylonjs_noexport"))
  97. {
  98. return;
  99. }
  100. if (!ExportHiddenObjects && meshNode.MaxNode.IsHidden(NodeHideFlags.None, false))
  101. {
  102. return;
  103. }
  104. var gameMesh = meshNode.IGameObject.AsGameMesh();
  105. bool initialized = gameMesh.InitializeData; //needed, the property is in fact a method initializing the exporter that has wrongly been auto
  106. // translated into a property because it has no parameters
  107. var babylonMesh = new BabylonMesh { name = meshNode.Name, id = meshNode.MaxNode.GetGuid().ToString() };
  108. if (meshNode.NodeParent != null)
  109. {
  110. babylonMesh.parentId = GetParentID(meshNode.NodeParent, babylonScene, scene);
  111. }
  112. // Sounds
  113. var soundName = meshNode.MaxNode.GetStringProperty("babylonjs_sound_filename", "");
  114. if (!string.IsNullOrEmpty(soundName))
  115. {
  116. var filename = Path.GetFileName(soundName);
  117. var meshSound = new BabylonSound
  118. {
  119. name = filename,
  120. autoplay = meshNode.MaxNode.GetBoolProperty("babylonjs_sound_autoplay", 1),
  121. loop = meshNode.MaxNode.GetBoolProperty("babylonjs_sound_loop", 1),
  122. volume = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_volume", 1.0f),
  123. playbackRate = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_playbackrate", 1.0f),
  124. connectedMeshId = babylonMesh.id,
  125. isDirectional = false,
  126. spatialSound = false,
  127. distanceModel = meshNode.MaxNode.GetStringProperty("babylonjs_sound_distancemodel", "linear"),
  128. maxDistance = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_maxdistance", 100f),
  129. rolloffFactor = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_rolloff", 1.0f),
  130. refDistance = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_refdistance", 1.0f),
  131. };
  132. var isDirectional = meshNode.MaxNode.GetBoolProperty("babylonjs_sound_directional", 0);
  133. if (isDirectional)
  134. {
  135. meshSound.isDirectional = true;
  136. meshSound.coneInnerAngle = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_coneinnerangle", 360f);
  137. meshSound.coneOuterAngle = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_coneouterangle", 360f);
  138. meshSound.coneOuterGain = meshNode.MaxNode.GetFloatProperty("babylonjs_sound_coneoutergain", 1.0f);
  139. }
  140. babylonScene.SoundsList.Add(meshSound);
  141. try
  142. {
  143. File.Copy(soundName, Path.Combine(babylonScene.OutputPath, filename), true);
  144. }
  145. catch
  146. {
  147. }
  148. }
  149. // Misc.
  150. babylonMesh.isVisible = meshNode.MaxNode.Renderable == 1;
  151. babylonMesh.pickable = meshNode.MaxNode.GetBoolProperty("babylonjs_checkpickable");
  152. babylonMesh.receiveShadows = meshNode.MaxNode.RcvShadows == 1;
  153. babylonMesh.showBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showboundingbox");
  154. babylonMesh.showSubMeshesBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showsubmeshesboundingbox");
  155. babylonMesh.applyFog = meshNode.MaxNode.ApplyAtmospherics == 1;
  156. babylonMesh.alphaIndex = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_alphaindex", 1000);
  157. // Actions
  158. babylonMesh.actions = ExportNodeAction(meshNode);
  159. // Collisions
  160. babylonMesh.checkCollisions = meshNode.MaxNode.GetBoolProperty("babylonjs_checkcollisions");
  161. var isSkinned = gameMesh.IsObjectSkinned;
  162. var skin = gameMesh.IGameSkin;
  163. var unskinnedMesh = gameMesh;
  164. IGMatrix skinInitPoseMatrix = Loader.Global.GMatrix.Create(Loader.Global.Matrix3.Create(true));
  165. List<int> boneIds = null;
  166. if (isSkinned)
  167. {
  168. bonesCount = skin.TotalSkinBoneCount;
  169. skins.Add(skin);
  170. skinnedNodes.Add(meshNode);
  171. babylonMesh.skeletonId = skins.IndexOf(skin);
  172. skin.GetInitSkinTM(skinInitPoseMatrix);
  173. boneIds = SortBones(skin);
  174. skinSortedBones[skin] = boneIds;
  175. }
  176. // Position / rotation / scaling
  177. var localTM = meshNode.GetObjectTM(0);
  178. if (meshNode.NodeParent != null)
  179. {
  180. var parentWorld = meshNode.NodeParent.GetObjectTM(0);
  181. localTM.MultiplyBy(parentWorld.Inverse);
  182. }
  183. var meshTrans = localTM.Translation;
  184. var meshRotation = localTM.Rotation;
  185. var meshScale = localTM.Scaling;
  186. var exportQuaternions = Loader.Core.RootNode.GetBoolProperty("babylonjs_exportquaternions");
  187. babylonMesh.position = new[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  188. if (exportQuaternions)
  189. {
  190. babylonMesh.rotationQuaternion = new[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
  191. }
  192. else
  193. {
  194. RotationToEulerAngles(babylonMesh, meshRotation);
  195. }
  196. babylonMesh.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };
  197. // Mesh
  198. RaiseMessage(meshNode.Name, 1);
  199. if (unskinnedMesh.IGameType == Autodesk.Max.IGameObject.ObjectTypes.Mesh && unskinnedMesh.MaxMesh != null)
  200. {
  201. if (unskinnedMesh.NumberOfFaces < 1)
  202. {
  203. RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name), 2);
  204. }
  205. if (unskinnedMesh.NumberOfVerts < 3)
  206. {
  207. RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name), 2);
  208. }
  209. if (unskinnedMesh.NumberOfVerts >= 65536)
  210. {
  211. 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);
  212. }
  213. // Physics
  214. var impostorText = meshNode.MaxNode.GetStringProperty("babylonjs_impostor", "None");
  215. if (impostorText != "None")
  216. {
  217. switch (impostorText)
  218. {
  219. case "Sphere":
  220. babylonMesh.physicsImpostor = 1;
  221. break;
  222. case "Box":
  223. babylonMesh.physicsImpostor = 2;
  224. break;
  225. case "Plane":
  226. babylonMesh.physicsImpostor = 3;
  227. break;
  228. default:
  229. babylonMesh.physicsImpostor = 0;
  230. break;
  231. }
  232. babylonMesh.physicsMass = meshNode.MaxNode.GetFloatProperty("babylonjs_mass");
  233. babylonMesh.physicsFriction = meshNode.MaxNode.GetFloatProperty("babylonjs_friction", 0.2f);
  234. babylonMesh.physicsRestitution = meshNode.MaxNode.GetFloatProperty("babylonjs_restitution", 0.2f);
  235. }
  236. // Material
  237. var mtl = meshNode.NodeMaterial;
  238. var multiMatsCount = 1;
  239. if (mtl != null)
  240. {
  241. babylonMesh.materialId = mtl.MaxMaterial.GetGuid().ToString();
  242. if (!referencedMaterials.Contains(mtl))
  243. {
  244. referencedMaterials.Add(mtl);
  245. }
  246. multiMatsCount = Math.Max(mtl.SubMaterialCount, 1);
  247. }
  248. babylonMesh.visibility = meshNode.MaxNode.GetVisibility(0, Tools.Forever);
  249. var vertices = new List<GlobalVertex>();
  250. var indices = new List<int>();
  251. var mappingChannels = unskinnedMesh.ActiveMapChannelNum;
  252. bool hasUV = false;
  253. bool hasUV2 = false;
  254. for (int i = 0; i < mappingChannels.Count; ++i)
  255. {
  256. var indexer = new IntPtr(i);
  257. var channelNum = mappingChannels[indexer];
  258. if (channelNum == 1)
  259. {
  260. hasUV = true;
  261. }
  262. else if (channelNum == 2)
  263. {
  264. hasUV2 = true;
  265. }
  266. }
  267. var hasColor = unskinnedMesh.NumberOfColorVerts > 0;
  268. var hasAlpha = unskinnedMesh.GetNumberOfMapVerts(-2) > 0;
  269. var optimizeVertices = meshNode.MaxNode.GetBoolProperty("babylonjs_optimizevertices");
  270. // Compute normals
  271. List<GlobalVertex>[] verticesAlreadyExported = null;
  272. if (optimizeVertices)
  273. {
  274. verticesAlreadyExported = new List<GlobalVertex>[unskinnedMesh.NumberOfVerts];
  275. }
  276. var subMeshes = new List<BabylonSubMesh>();
  277. var indexStart = 0;
  278. for (int i = 0; i < multiMatsCount; ++i)
  279. {
  280. int materialId = meshNode.NodeMaterial == null ? 0 : meshNode.NodeMaterial.GetMaterialID(i);
  281. var indexCount = 0;
  282. var minVertexIndex = int.MaxValue;
  283. var maxVertexIndex = int.MinValue;
  284. var subMesh = new BabylonSubMesh { indexStart = indexStart, materialIndex = i };
  285. if (multiMatsCount == 1)
  286. {
  287. for (int j = 0; j < unskinnedMesh.NumberOfFaces; ++j)
  288. {
  289. var face = unskinnedMesh.GetFace(j);
  290. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  291. }
  292. }
  293. else
  294. {
  295. ITab<IFaceEx> materialFaces = unskinnedMesh.GetFacesFromMatID(materialId);
  296. for (int j = 0; j < materialFaces.Count; ++j)
  297. {
  298. var faceIndexer = new IntPtr(j);
  299. var face = materialFaces[faceIndexer];
  300. Marshal.FreeHGlobal(faceIndexer);
  301. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  302. }
  303. }
  304. if (indexCount != 0)
  305. {
  306. subMesh.indexCount = indexCount;
  307. subMesh.verticesStart = minVertexIndex;
  308. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  309. indexStart += indexCount;
  310. subMeshes.Add(subMesh);
  311. }
  312. }
  313. if (vertices.Count >= 65536)
  314. {
  315. 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);
  316. if (!optimizeVertices)
  317. {
  318. RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
  319. }
  320. }
  321. RaiseMessage(string.Format("{0} vertices, {1} faces", vertices.Count, indices.Count / 3), 2);
  322. // Buffers
  323. babylonMesh.positions = vertices.SelectMany(v => new[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
  324. babylonMesh.normals = vertices.SelectMany(v => new[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
  325. if (hasUV)
  326. {
  327. babylonMesh.uvs = vertices.SelectMany(v => new[] { v.UV.X, 1 - v.UV.Y }).ToArray();
  328. }
  329. if (hasUV2)
  330. {
  331. babylonMesh.uvs2 = vertices.SelectMany(v => new[] { v.UV2.X, 1 - v.UV2.Y }).ToArray();
  332. }
  333. if (skin != null)
  334. {
  335. babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
  336. babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
  337. }
  338. if (hasColor)
  339. {
  340. babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
  341. babylonMesh.hasVertexAlpha = hasAlpha;
  342. }
  343. babylonMesh.subMeshes = subMeshes.ToArray();
  344. // Buffers - Indices
  345. babylonMesh.indices = indices.ToArray();
  346. }
  347. // Instances
  348. var tabs = Loader.Global.NodeTab.Create();
  349. Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs);
  350. var instances = new List<BabylonAbstractMesh>();
  351. for (var index = 0; index < tabs.Count; index++)
  352. {
  353. var indexer = new IntPtr(index);
  354. var tab = tabs[indexer];
  355. Marshal.FreeHGlobal(indexer);
  356. if (meshNode.MaxNode.GetGuid() == tab.GetGuid())
  357. {
  358. continue;
  359. }
  360. var instanceGameNode = scene.GetIGameNode(tab);
  361. if (instanceGameNode == null)
  362. {
  363. continue;
  364. }
  365. tab.MarkAsInstance();
  366. var instance = new BabylonAbstractMesh { name = tab.Name };
  367. {
  368. var instanceLocalTM = instanceGameNode.GetObjectTM(0);
  369. var instanceTrans = instanceLocalTM.Translation;
  370. var instanceRotation = instanceLocalTM.Rotation;
  371. var instanceScale = instanceLocalTM.Scaling;
  372. instance.position = new[] { instanceTrans.X, instanceTrans.Y, instanceTrans.Z };
  373. if (exportQuaternions)
  374. {
  375. instance.rotationQuaternion = new[] { instanceRotation.X, instanceRotation.Y, instanceRotation.Z, -instanceRotation.W };
  376. }
  377. else
  378. {
  379. RotationToEulerAngles(instance, instanceRotation);
  380. }
  381. instance.scaling = new[] { instanceScale.X, instanceScale.Y, instanceScale.Z };
  382. }
  383. var instanceAnimations = new List<BabylonAnimation>();
  384. GenerateCoordinatesAnimations(meshNode, instanceAnimations);
  385. instance.animations = instanceAnimations.ToArray();
  386. instances.Add(instance);
  387. }
  388. babylonMesh.instances = instances.ToArray();
  389. // Animations
  390. var animations = new List<BabylonAnimation>();
  391. GenerateCoordinatesAnimations(meshNode, animations);
  392. if (!ExportFloatController(meshNode.MaxNode.VisController, "visibility", animations))
  393. {
  394. ExportFloatAnimation("visibility", animations, key => new[] { meshNode.MaxNode.GetVisibility(key, Tools.Forever) });
  395. }
  396. babylonMesh.animations = animations.ToArray();
  397. if (meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimate", 1))
  398. {
  399. babylonMesh.autoAnimate = true;
  400. babylonMesh.autoAnimateFrom = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  401. babylonMesh.autoAnimateTo = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to", 100);
  402. babylonMesh.autoAnimateLoop = meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop", 1);
  403. }
  404. babylonScene.MeshesList.Add(babylonMesh);
  405. }
  406. 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)
  407. {
  408. var a = CreateGlobalVertex(unskinnedMesh, face, 0, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  409. var b = CreateGlobalVertex(unskinnedMesh, face, 2, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  410. var c = CreateGlobalVertex(unskinnedMesh, face, 1, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  411. indices.Add(a);
  412. indices.Add(b);
  413. indices.Add(c);
  414. if (a < minVertexIndex)
  415. {
  416. minVertexIndex = a;
  417. }
  418. if (b < minVertexIndex)
  419. {
  420. minVertexIndex = b;
  421. }
  422. if (c < minVertexIndex)
  423. {
  424. minVertexIndex = c;
  425. }
  426. if (a > maxVertexIndex)
  427. {
  428. maxVertexIndex = a;
  429. }
  430. if (b > maxVertexIndex)
  431. {
  432. maxVertexIndex = b;
  433. }
  434. if (c > maxVertexIndex)
  435. {
  436. maxVertexIndex = c;
  437. }
  438. indexCount += 3;
  439. CheckCancelled();
  440. }
  441. public static void GenerateCoordinatesAnimations(IIGameNode meshNode, List<BabylonAnimation> animations)
  442. {
  443. if (meshNode.IGameControl.IsAnimated(IGameControlType.Pos) ||
  444. meshNode.IGameControl.IsAnimated(IGameControlType.PosX) ||
  445. meshNode.IGameControl.IsAnimated(IGameControlType.PosY) ||
  446. meshNode.IGameControl.IsAnimated(IGameControlType.PosZ))
  447. {
  448. ExportVector3Animation("position", animations, key =>
  449. {
  450. var worldMatrix = meshNode.GetObjectTM(key);
  451. if (meshNode.NodeParent != null)
  452. {
  453. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  454. worldMatrix.MultiplyBy(parentWorld.Inverse);
  455. }
  456. var trans = worldMatrix.Translation;
  457. return new[] { trans.X, trans.Y, trans.Z };
  458. });
  459. }
  460. if (meshNode.IGameControl.IsAnimated(IGameControlType.Rot) ||
  461. meshNode.IGameControl.IsAnimated(IGameControlType.EulerX) ||
  462. meshNode.IGameControl.IsAnimated(IGameControlType.EulerY) ||
  463. meshNode.IGameControl.IsAnimated(IGameControlType.EulerZ))
  464. {
  465. ExportQuaternionAnimation("rotationQuaternion", animations, key =>
  466. {
  467. var worldMatrix = meshNode.GetObjectTM(key);
  468. if (meshNode.NodeParent != null)
  469. {
  470. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  471. worldMatrix.MultiplyBy(parentWorld.Inverse);
  472. }
  473. var rot = worldMatrix.Rotation;
  474. return new[] { rot.X, rot.Y, rot.Z, -rot.W };
  475. });
  476. }
  477. if (meshNode.IGameControl.IsAnimated(IGameControlType.Scale))
  478. {
  479. ExportVector3Animation("scaling", animations, key =>
  480. {
  481. var worldMatrix = meshNode.GetObjectTM(key);
  482. if (meshNode.NodeParent != null)
  483. {
  484. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  485. worldMatrix.MultiplyBy(parentWorld.Inverse);
  486. }
  487. var scale = worldMatrix.Scaling;
  488. return new[] { scale.X, scale.Y, scale.Z };
  489. });
  490. }
  491. }
  492. 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)
  493. {
  494. var vertexIndex = (int)face.Vert[facePart];
  495. var vertex = new GlobalVertex
  496. {
  497. BaseIndex = vertexIndex,
  498. Position = mesh.GetVertex(vertexIndex, true),
  499. Normal = mesh.GetNormal((int)face.Norm[facePart], true)
  500. };
  501. if (hasUV)
  502. {
  503. var indices = new int[3];
  504. unsafe
  505. {
  506. fixed (int* indicesPtr = indices)
  507. {
  508. mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
  509. }
  510. }
  511. var texCoord = mesh.GetMapVertex(1, indices[facePart]);
  512. vertex.UV = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  513. }
  514. if (hasUV2)
  515. {
  516. var indices = new int[3];
  517. unsafe
  518. {
  519. fixed (int* indicesPtr = indices)
  520. {
  521. mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  522. }
  523. }
  524. var texCoord = mesh.GetMapVertex(2, indices[facePart]);
  525. vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  526. }
  527. if (hasColor)
  528. {
  529. var vertexColorIndex = (int)face.Color[facePart];
  530. var vertexColor = mesh.GetColorVertex(vertexColorIndex);
  531. float alpha = 1;
  532. if (hasAlpha)
  533. {
  534. var indices = new int[3];
  535. unsafe
  536. {
  537. fixed (int* indicesPtr = indices)
  538. {
  539. mesh.GetMapFaceIndex(-2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  540. }
  541. }
  542. var color = mesh.GetMapVertex(-2, indices[facePart]);
  543. alpha = color.X;
  544. }
  545. vertex.Color = new[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha };
  546. }
  547. if (skin != null)
  548. {
  549. float weight0 = 0;
  550. float weight1 = 0;
  551. float weight2 = 0;
  552. int bone0 = bonesCount;
  553. int bone1 = bonesCount;
  554. int bone2 = bonesCount;
  555. int bone3 = bonesCount;
  556. int nbBones = skin.GetNumberOfBones(vertexIndex);
  557. if (nbBones > 0)
  558. {
  559. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 0).NodeID);
  560. weight0 = skin.GetWeight(vertexIndex, 0);
  561. }
  562. if (nbBones > 1)
  563. {
  564. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 1).NodeID);
  565. weight1 = skin.GetWeight(vertexIndex, 1);
  566. }
  567. if (nbBones > 2)
  568. {
  569. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 2).NodeID);
  570. weight2 = skin.GetWeight(vertexIndex, 2);
  571. }
  572. if (nbBones > 3)
  573. {
  574. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 3).NodeID);
  575. }
  576. if (nbBones == 0)
  577. {
  578. weight0 = 1.0f;
  579. bone0 = bonesCount;
  580. }
  581. if (nbBones > 4)
  582. {
  583. RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2);
  584. }
  585. vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2);
  586. vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  587. }
  588. if (verticesAlreadyExported != null)
  589. {
  590. if (verticesAlreadyExported[vertexIndex] != null)
  591. {
  592. var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
  593. if (index > -1)
  594. {
  595. return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
  596. }
  597. }
  598. else
  599. {
  600. verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
  601. }
  602. vertex.CurrentIndex = vertices.Count;
  603. verticesAlreadyExported[vertexIndex].Add(vertex);
  604. }
  605. vertices.Add(vertex);
  606. return vertices.Count - 1;
  607. }
  608. }
  609. }