BabylonExporter.Mesh.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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");
  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. #if MAX2017
  151. babylonMesh.isVisible = meshNode.MaxNode.Renderable;
  152. babylonMesh.receiveShadows = meshNode.MaxNode.RcvShadows;
  153. babylonMesh.applyFog = meshNode.MaxNode.ApplyAtmospherics;
  154. #else
  155. babylonMesh.isVisible = meshNode.MaxNode.Renderable == 1;
  156. babylonMesh.receiveShadows = meshNode.MaxNode.RcvShadows == 1;
  157. babylonMesh.applyFog = meshNode.MaxNode.ApplyAtmospherics == 1;
  158. #endif
  159. babylonMesh.pickable = meshNode.MaxNode.GetBoolProperty("babylonjs_checkpickable");
  160. babylonMesh.showBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showboundingbox");
  161. babylonMesh.showSubMeshesBoundingBox = meshNode.MaxNode.GetBoolProperty("babylonjs_showsubmeshesboundingbox");
  162. babylonMesh.alphaIndex = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_alphaindex", 1000);
  163. // Actions
  164. babylonMesh.actions = ExportNodeAction(meshNode);
  165. // Collisions
  166. babylonMesh.checkCollisions = meshNode.MaxNode.GetBoolProperty("babylonjs_checkcollisions");
  167. var isSkinned = gameMesh.IsObjectSkinned;
  168. var skin = gameMesh.IGameSkin;
  169. var unskinnedMesh = gameMesh;
  170. IGMatrix skinInitPoseMatrix = Loader.Global.GMatrix.Create(Loader.Global.Matrix3.Create(true));
  171. List<int> boneIds = null;
  172. int maxNbBones = 0;
  173. if (isSkinned)
  174. {
  175. bonesCount = skin.TotalSkinBoneCount;
  176. skins.Add(skin);
  177. skinnedNodes.Add(meshNode);
  178. babylonMesh.skeletonId = skins.IndexOf(skin);
  179. skin.GetInitSkinTM(skinInitPoseMatrix);
  180. boneIds = SortBones(skin);
  181. skinSortedBones[skin] = boneIds;
  182. }
  183. // Position / rotation / scaling
  184. var localTM = meshNode.GetObjectTM(0);
  185. if (meshNode.NodeParent != null)
  186. {
  187. var parentWorld = meshNode.NodeParent.GetObjectTM(0);
  188. localTM.MultiplyBy(parentWorld.Inverse);
  189. }
  190. var meshTrans = localTM.Translation;
  191. var meshRotation = localTM.Rotation;
  192. var meshScale = localTM.Scaling;
  193. var exportQuaternions = Loader.Core.RootNode.GetBoolProperty("babylonjs_exportquaternions");
  194. babylonMesh.position = new[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
  195. if (exportQuaternions)
  196. {
  197. babylonMesh.rotationQuaternion = new[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
  198. }
  199. else
  200. {
  201. RotationToEulerAngles(babylonMesh, meshRotation);
  202. }
  203. babylonMesh.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };
  204. // Mesh
  205. RaiseMessage(meshNode.Name, 1);
  206. if (unskinnedMesh.IGameType == Autodesk.Max.IGameObject.ObjectTypes.Mesh && unskinnedMesh.MaxMesh != null)
  207. {
  208. if (unskinnedMesh.NumberOfFaces < 1)
  209. {
  210. RaiseError($"Mesh {babylonMesh.name} has no face", 2);
  211. }
  212. if (unskinnedMesh.NumberOfVerts < 3)
  213. {
  214. RaiseError($"Mesh {babylonMesh.name} has not enough vertices", 2);
  215. }
  216. if (unskinnedMesh.NumberOfVerts >= 65536)
  217. {
  218. RaiseWarning($"Mesh {babylonMesh.name} 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.", 2);
  219. }
  220. if (skin != null)
  221. {
  222. for (var vertexIndex = 0; vertexIndex < unskinnedMesh.NumberOfVerts; vertexIndex++)
  223. {
  224. maxNbBones = Math.Max(maxNbBones, skin.GetNumberOfBones(vertexIndex));
  225. }
  226. }
  227. // Physics
  228. var impostorText = meshNode.MaxNode.GetStringProperty("babylonjs_impostor", "None");
  229. if (impostorText != "None")
  230. {
  231. switch (impostorText)
  232. {
  233. case "Sphere":
  234. babylonMesh.physicsImpostor = 1;
  235. break;
  236. case "Box":
  237. babylonMesh.physicsImpostor = 2;
  238. break;
  239. case "Plane":
  240. babylonMesh.physicsImpostor = 3;
  241. break;
  242. default:
  243. babylonMesh.physicsImpostor = 0;
  244. break;
  245. }
  246. babylonMesh.physicsMass = meshNode.MaxNode.GetFloatProperty("babylonjs_mass");
  247. babylonMesh.physicsFriction = meshNode.MaxNode.GetFloatProperty("babylonjs_friction", 0.2f);
  248. babylonMesh.physicsRestitution = meshNode.MaxNode.GetFloatProperty("babylonjs_restitution", 0.2f);
  249. }
  250. // Material
  251. var mtl = meshNode.NodeMaterial;
  252. var multiMatsCount = 1;
  253. if (mtl != null)
  254. {
  255. babylonMesh.materialId = mtl.MaxMaterial.GetGuid().ToString();
  256. if (!referencedMaterials.Contains(mtl))
  257. {
  258. referencedMaterials.Add(mtl);
  259. }
  260. multiMatsCount = Math.Max(mtl.SubMaterialCount, 1);
  261. }
  262. babylonMesh.visibility = meshNode.MaxNode.GetVisibility(0, Tools.Forever);
  263. var vertices = new List<GlobalVertex>();
  264. var indices = new List<int>();
  265. var mappingChannels = unskinnedMesh.ActiveMapChannelNum;
  266. bool hasUV = false;
  267. bool hasUV2 = false;
  268. for (int i = 0; i < mappingChannels.Count; ++i)
  269. {
  270. #if MAX2017
  271. var indexer = i;
  272. #else
  273. var indexer = new IntPtr(i);
  274. #endif
  275. var channelNum = mappingChannels[indexer];
  276. if (channelNum == 1)
  277. {
  278. hasUV = true;
  279. }
  280. else if (channelNum == 2)
  281. {
  282. hasUV2 = true;
  283. }
  284. }
  285. var hasColor = unskinnedMesh.NumberOfColorVerts > 0;
  286. var hasAlpha = unskinnedMesh.GetNumberOfMapVerts(-2) > 0;
  287. var optimizeVertices = meshNode.MaxNode.GetBoolProperty("babylonjs_optimizevertices");
  288. // Compute normals
  289. List<GlobalVertex>[] verticesAlreadyExported = null;
  290. if (optimizeVertices)
  291. {
  292. verticesAlreadyExported = new List<GlobalVertex>[unskinnedMesh.NumberOfVerts];
  293. }
  294. var subMeshes = new List<BabylonSubMesh>();
  295. var indexStart = 0;
  296. for (int i = 0; i < multiMatsCount; ++i)
  297. {
  298. int materialId = meshNode.NodeMaterial?.GetMaterialID(i) ?? 0;
  299. var indexCount = 0;
  300. var minVertexIndex = int.MaxValue;
  301. var maxVertexIndex = int.MinValue;
  302. var subMesh = new BabylonSubMesh { indexStart = indexStart, materialIndex = i };
  303. if (multiMatsCount == 1)
  304. {
  305. for (int j = 0; j < unskinnedMesh.NumberOfFaces; ++j)
  306. {
  307. var face = unskinnedMesh.GetFace(j);
  308. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  309. }
  310. }
  311. else
  312. {
  313. ITab<IFaceEx> materialFaces = unskinnedMesh.GetFacesFromMatID(materialId);
  314. for (int j = 0; j < materialFaces.Count; ++j)
  315. {
  316. #if MAX2017
  317. var faceIndexer = j;
  318. #else
  319. var faceIndexer = new IntPtr(j);
  320. #endif
  321. var face = materialFaces[faceIndexer];
  322. #if !MAX2017
  323. Marshal.FreeHGlobal(faceIndexer);
  324. #endif
  325. ExtractFace(skin, unskinnedMesh, vertices, indices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, ref indexCount, ref minVertexIndex, ref maxVertexIndex, face, boneIds);
  326. }
  327. }
  328. if (indexCount != 0)
  329. {
  330. subMesh.indexCount = indexCount;
  331. subMesh.verticesStart = minVertexIndex;
  332. subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
  333. indexStart += indexCount;
  334. subMeshes.Add(subMesh);
  335. }
  336. }
  337. if (vertices.Count >= 65536)
  338. {
  339. RaiseWarning($"Mesh {babylonMesh.name} has {vertices.Count} vertices. This may prevent your scene to work on low end devices where 32 bits indice are not supported", 2);
  340. if (!optimizeVertices)
  341. {
  342. RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
  343. }
  344. }
  345. RaiseMessage($"{vertices.Count} vertices, {indices.Count/3} faces", 2);
  346. // Buffers
  347. babylonMesh.positions = vertices.SelectMany(v => new[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
  348. babylonMesh.normals = vertices.SelectMany(v => new[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
  349. if (hasUV)
  350. {
  351. babylonMesh.uvs = vertices.SelectMany(v => new[] { v.UV.X, 1 - v.UV.Y }).ToArray();
  352. }
  353. if (hasUV2)
  354. {
  355. babylonMesh.uvs2 = vertices.SelectMany(v => new[] { v.UV2.X, 1 - v.UV2.Y }).ToArray();
  356. }
  357. if (skin != null)
  358. {
  359. babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
  360. babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
  361. babylonMesh.numBoneInfluencers = maxNbBones;
  362. if (maxNbBones > 4)
  363. {
  364. babylonMesh.matricesWeightsExtra = vertices.SelectMany(v => v.WeightsExtra != null ? v.WeightsExtra.ToArray() : new[] {0.0f, 0.0f, 0.0f, 0.0f }).ToArray();
  365. babylonMesh.matricesIndicesExtra = vertices.Select(v => v.BonesIndicesExtra).ToArray();
  366. }
  367. }
  368. if (hasColor)
  369. {
  370. babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
  371. babylonMesh.hasVertexAlpha = hasAlpha;
  372. }
  373. babylonMesh.subMeshes = subMeshes.ToArray();
  374. // Buffers - Indices
  375. babylonMesh.indices = indices.ToArray();
  376. }
  377. // Instances
  378. var tabs = Loader.Global.NodeTab.Create();
  379. Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs);
  380. var instances = new List<BabylonAbstractMesh>();
  381. for (var index = 0; index < tabs.Count; index++)
  382. {
  383. #if MAX2017
  384. var indexer = index;
  385. #else
  386. var indexer = new IntPtr(index);
  387. #endif
  388. var tab = tabs[indexer];
  389. #if !MAX2017
  390. Marshal.FreeHGlobal(indexer);
  391. #endif
  392. if (meshNode.MaxNode.GetGuid() == tab.GetGuid())
  393. {
  394. continue;
  395. }
  396. var instanceGameNode = scene.GetIGameNode(tab);
  397. if (instanceGameNode == null)
  398. {
  399. continue;
  400. }
  401. tab.MarkAsInstance();
  402. var instance = new BabylonAbstractMesh { name = tab.Name };
  403. {
  404. var instanceLocalTM = instanceGameNode.GetObjectTM(0);
  405. var instanceTrans = instanceLocalTM.Translation;
  406. var instanceRotation = instanceLocalTM.Rotation;
  407. var instanceScale = instanceLocalTM.Scaling;
  408. instance.id = instanceGameNode.MaxNode.GetGuid().ToString();
  409. instance.position = new[] { instanceTrans.X, instanceTrans.Y, instanceTrans.Z };
  410. if (exportQuaternions)
  411. {
  412. instance.rotationQuaternion = new[] { instanceRotation.X, instanceRotation.Y, instanceRotation.Z, -instanceRotation.W };
  413. }
  414. else
  415. {
  416. RotationToEulerAngles(instance, instanceRotation);
  417. }
  418. instance.scaling = new[] { instanceScale.X, instanceScale.Y, instanceScale.Z };
  419. if (instanceGameNode.NodeParent != null)
  420. {
  421. instance.parentId = GetParentID(instanceGameNode.NodeParent, babylonScene, scene);
  422. }
  423. }
  424. var instanceAnimations = new List<BabylonAnimation>();
  425. GenerateCoordinatesAnimations(meshNode, instanceAnimations);
  426. instance.animations = instanceAnimations.ToArray();
  427. instances.Add(instance);
  428. }
  429. babylonMesh.instances = instances.ToArray();
  430. // Animations
  431. var animations = new List<BabylonAnimation>();
  432. GenerateCoordinatesAnimations(meshNode, animations);
  433. if (!ExportFloatController(meshNode.MaxNode.VisController, "visibility", animations))
  434. {
  435. ExportFloatAnimation("visibility", animations, key => new[] { meshNode.MaxNode.GetVisibility(key, Tools.Forever) });
  436. }
  437. babylonMesh.animations = animations.ToArray();
  438. if (meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimate", 1))
  439. {
  440. babylonMesh.autoAnimate = true;
  441. babylonMesh.autoAnimateFrom = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  442. babylonMesh.autoAnimateTo = (int)meshNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to", 100);
  443. babylonMesh.autoAnimateLoop = meshNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop", 1);
  444. }
  445. babylonScene.MeshesList.Add(babylonMesh);
  446. }
  447. 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)
  448. {
  449. var a = CreateGlobalVertex(unskinnedMesh, face, 0, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  450. var b = CreateGlobalVertex(unskinnedMesh, face, 2, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  451. var c = CreateGlobalVertex(unskinnedMesh, face, 1, vertices, hasUV, hasUV2, hasColor, hasAlpha, verticesAlreadyExported, skin, boneIds);
  452. indices.Add(a);
  453. indices.Add(b);
  454. indices.Add(c);
  455. if (a < minVertexIndex)
  456. {
  457. minVertexIndex = a;
  458. }
  459. if (b < minVertexIndex)
  460. {
  461. minVertexIndex = b;
  462. }
  463. if (c < minVertexIndex)
  464. {
  465. minVertexIndex = c;
  466. }
  467. if (a > maxVertexIndex)
  468. {
  469. maxVertexIndex = a;
  470. }
  471. if (b > maxVertexIndex)
  472. {
  473. maxVertexIndex = b;
  474. }
  475. if (c > maxVertexIndex)
  476. {
  477. maxVertexIndex = c;
  478. }
  479. indexCount += 3;
  480. CheckCancelled();
  481. }
  482. public static void GenerateCoordinatesAnimations(IIGameNode meshNode, List<BabylonAnimation> animations)
  483. {
  484. if (meshNode.IGameControl.IsAnimated(IGameControlType.Pos) ||
  485. meshNode.IGameControl.IsAnimated(IGameControlType.PosX) ||
  486. meshNode.IGameControl.IsAnimated(IGameControlType.PosY) ||
  487. meshNode.IGameControl.IsAnimated(IGameControlType.PosZ))
  488. {
  489. ExportVector3Animation("position", animations, key =>
  490. {
  491. var worldMatrix = meshNode.GetObjectTM(key);
  492. if (meshNode.NodeParent != null)
  493. {
  494. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  495. worldMatrix.MultiplyBy(parentWorld.Inverse);
  496. }
  497. var trans = worldMatrix.Translation;
  498. return new[] { trans.X, trans.Y, trans.Z };
  499. });
  500. }
  501. if (meshNode.IGameControl.IsAnimated(IGameControlType.Rot) ||
  502. meshNode.IGameControl.IsAnimated(IGameControlType.EulerX) ||
  503. meshNode.IGameControl.IsAnimated(IGameControlType.EulerY) ||
  504. meshNode.IGameControl.IsAnimated(IGameControlType.EulerZ))
  505. {
  506. ExportQuaternionAnimation("rotationQuaternion", animations, key =>
  507. {
  508. var worldMatrix = meshNode.GetObjectTM(key);
  509. if (meshNode.NodeParent != null)
  510. {
  511. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  512. worldMatrix.MultiplyBy(parentWorld.Inverse);
  513. }
  514. var rot = worldMatrix.Rotation;
  515. return new[] { rot.X, rot.Y, rot.Z, -rot.W };
  516. });
  517. }
  518. if (meshNode.IGameControl.IsAnimated(IGameControlType.Scale))
  519. {
  520. ExportVector3Animation("scaling", animations, key =>
  521. {
  522. var worldMatrix = meshNode.GetObjectTM(key);
  523. if (meshNode.NodeParent != null)
  524. {
  525. var parentWorld = meshNode.NodeParent.GetObjectTM(key);
  526. worldMatrix.MultiplyBy(parentWorld.Inverse);
  527. }
  528. var scale = worldMatrix.Scaling;
  529. return new[] { scale.X, scale.Y, scale.Z };
  530. });
  531. }
  532. }
  533. 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)
  534. {
  535. var vertexIndex = (int)face.Vert[facePart];
  536. var vertex = new GlobalVertex
  537. {
  538. BaseIndex = vertexIndex,
  539. Position = mesh.GetVertex(vertexIndex, true),
  540. Normal = mesh.GetNormal((int)face.Norm[facePart], true)
  541. };
  542. if (hasUV)
  543. {
  544. var indices = new int[3];
  545. unsafe
  546. {
  547. fixed (int* indicesPtr = indices)
  548. {
  549. mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
  550. }
  551. }
  552. var texCoord = mesh.GetMapVertex(1, indices[facePart]);
  553. vertex.UV = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  554. }
  555. if (hasUV2)
  556. {
  557. var indices = new int[3];
  558. unsafe
  559. {
  560. fixed (int* indicesPtr = indices)
  561. {
  562. mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  563. }
  564. }
  565. var texCoord = mesh.GetMapVertex(2, indices[facePart]);
  566. vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
  567. }
  568. if (hasColor)
  569. {
  570. var vertexColorIndex = (int)face.Color[facePart];
  571. var vertexColor = mesh.GetColorVertex(vertexColorIndex);
  572. float alpha = 1;
  573. if (hasAlpha)
  574. {
  575. var indices = new int[3];
  576. unsafe
  577. {
  578. fixed (int* indicesPtr = indices)
  579. {
  580. mesh.GetMapFaceIndex(-2, face.MeshFaceIndex, new IntPtr(indicesPtr));
  581. }
  582. }
  583. var color = mesh.GetMapVertex(-2, indices[facePart]);
  584. alpha = color.X;
  585. }
  586. vertex.Color = new[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha };
  587. }
  588. if (skin != null)
  589. {
  590. float weight0 = 0;
  591. float weight1 = 0;
  592. float weight2 = 0;
  593. float weight3 = 0;
  594. int bone0 = bonesCount;
  595. int bone1 = bonesCount;
  596. int bone2 = bonesCount;
  597. int bone3 = bonesCount;
  598. var nbBones = skin.GetNumberOfBones(vertexIndex);
  599. if (nbBones > 0)
  600. {
  601. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 0).NodeID);
  602. weight0 = skin.GetWeight(vertexIndex, 0);
  603. }
  604. if (nbBones > 1)
  605. {
  606. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 1).NodeID);
  607. weight1 = skin.GetWeight(vertexIndex, 1);
  608. }
  609. if (nbBones > 2)
  610. {
  611. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 2).NodeID);
  612. weight2 = skin.GetWeight(vertexIndex, 2);
  613. }
  614. if (nbBones > 3)
  615. {
  616. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 3).NodeID);
  617. weight3 = skin.GetWeight(vertexIndex, 3);
  618. }
  619. if (nbBones == 0)
  620. {
  621. weight0 = 1.0f;
  622. bone0 = bonesCount;
  623. }
  624. vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, weight3);
  625. vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  626. if (nbBones > 4)
  627. {
  628. bone0 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 4).NodeID);
  629. weight0 = skin.GetWeight(vertexIndex, 4);
  630. weight1 = 0;
  631. weight2 = 0;
  632. weight3 = 0;
  633. if (nbBones > 5)
  634. {
  635. bone1 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 5).NodeID);
  636. weight1 = skin.GetWeight(vertexIndex, 5);
  637. }
  638. if (nbBones > 6)
  639. {
  640. bone2 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 6).NodeID);
  641. weight2 = skin.GetWeight(vertexIndex, 6);
  642. }
  643. if (nbBones > 7)
  644. {
  645. bone3 = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, 7).NodeID);
  646. weight3 = skin.GetWeight(vertexIndex, 7);
  647. }
  648. vertex.WeightsExtra = Loader.Global.Point4.Create(weight0, weight1, weight2, weight3);
  649. vertex.BonesIndicesExtra = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
  650. if (nbBones > 8)
  651. {
  652. RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 8 bones influences per vertex.", 2);
  653. }
  654. }
  655. }
  656. if (verticesAlreadyExported != null)
  657. {
  658. if (verticesAlreadyExported[vertexIndex] != null)
  659. {
  660. var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
  661. if (index > -1)
  662. {
  663. return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
  664. }
  665. }
  666. else
  667. {
  668. verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
  669. }
  670. vertex.CurrentIndex = vertices.Count;
  671. verticesAlreadyExported[vertexIndex].Add(vertex);
  672. }
  673. vertices.Add(vertex);
  674. return vertices.Count - 1;
  675. }
  676. }
  677. }