Tools.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using Autodesk.Max;
  8. using BabylonExport.Entities;
  9. using SharpDX;
  10. using System.Reflection;
  11. namespace Max2Babylon
  12. {
  13. public static class Tools
  14. {
  15. public static IntPtr GetNativeHandle(this INativeObject obj)
  16. {
  17. #if MAX2015
  18. return obj.NativePointer;
  19. #else
  20. return obj.Handle;
  21. #endif
  22. }
  23. static Assembly GetWrappersAssembly()
  24. {
  25. return Assembly.Load("Autodesk.Max.Wrappers, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
  26. }
  27. public static IIGameCamera AsGameCamera(this IIGameObject obj)
  28. {
  29. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameCamera");
  30. var constructor = type.GetConstructors()[0];
  31. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  32. unsafe
  33. {
  34. var voidPtr = obj.GetNativeHandle().ToPointer();
  35. return (IIGameCamera)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  36. }
  37. }
  38. public static IIGameMesh AsGameMesh(this IIGameObject obj)
  39. {
  40. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameMesh");
  41. var constructor = type.GetConstructors()[0];
  42. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  43. unsafe
  44. {
  45. var voidPtr = obj.GetNativeHandle().ToPointer();
  46. return (IIGameMesh)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  47. }
  48. }
  49. public static IIGameLight AsGameLight(this IIGameObject obj)
  50. {
  51. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameLight");
  52. var constructor = type.GetConstructors()[0];
  53. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  54. unsafe
  55. {
  56. var voidPtr = obj.GetNativeHandle().ToPointer();
  57. return (IIGameLight)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  58. }
  59. }
  60. public const float Epsilon = 0.001f;
  61. public static IPoint3 XAxis { get { return Loader.Global.Point3.Create(1, 0, 0); } }
  62. public static IPoint3 YAxis { get { return Loader.Global.Point3.Create(0, 1, 0); } }
  63. public static IPoint3 ZAxis { get { return Loader.Global.Point3.Create(0, 0, 1); } }
  64. public static IPoint3 Origin { get { return Loader.Global.Point3.Create(0, 0, 0); } }
  65. public static IInterval Forever
  66. {
  67. get { return Loader.Global.Interval.Create(int.MinValue, int.MaxValue); }
  68. }
  69. public static IMatrix3 Identity { get { return Loader.Global.Matrix3.Create(XAxis, YAxis, ZAxis, Origin); } }
  70. public static Vector3 ToEulerAngles(this IQuat q)
  71. {
  72. // Store the Euler angles in radians
  73. var pitchYawRoll = new Vector3();
  74. double sqw = q.W * q.W;
  75. double sqx = q.X * q.X;
  76. double sqy = q.Y * q.Y;
  77. double sqz = q.Z * q.Z;
  78. // If quaternion is normalised the unit is one, otherwise it is the correction factor
  79. double unit = sqx + sqy + sqz + sqw;
  80. double test = q.X * q.Y + q.Z * q.W;
  81. if (test > 0.4999f * unit) // 0.4999f OR 0.5f - EPSILON
  82. {
  83. // Singularity at north pole
  84. pitchYawRoll.Y = 2f * (float)Math.Atan2(q.X, q.W); // Yaw
  85. pitchYawRoll.X = (float)Math.PI * 0.5f; // Pitch
  86. pitchYawRoll.Z = 0f; // Roll
  87. return pitchYawRoll;
  88. }
  89. if (test < -0.4999f * unit) // -0.4999f OR -0.5f + EPSILON
  90. {
  91. // Singularity at south pole
  92. pitchYawRoll.Y = -2f * (float)Math.Atan2(q.X, q.W); // Yaw
  93. pitchYawRoll.X = -(float)Math.PI * 0.5f; // Pitch
  94. pitchYawRoll.Z = 0f; // Roll
  95. return pitchYawRoll;
  96. }
  97. pitchYawRoll.Y = (float)Math.Atan2(2f * q.Y * q.W - 2f * q.X * q.Z, sqx - sqy - sqz + sqw); // Yaw
  98. pitchYawRoll.X = (float)Math.Asin(2f * test / unit); // Pitch
  99. pitchYawRoll.Z = (float)Math.Atan2(2f * q.X * q.W - 2f * q.Y * q.Z, -sqx + sqy - sqz + sqw); // Roll
  100. return pitchYawRoll;
  101. }
  102. public static float[] ToArray(this IGMatrix gmat)
  103. {
  104. //float eulX =0, eulY=0, eulZ=0;
  105. //unsafe
  106. //{
  107. // gmat.Rotation.GetEuler( new IntPtr(&eulX), new IntPtr(&eulY), new IntPtr(&eulZ));
  108. //}
  109. //return (Matrix.Scaling(gmat.Scaling.X, gmat.Scaling.Y, gmat.Scaling.Z) * Matrix.RotationYawPitchRoll(eulY, eulX, eulZ) * Matrix.Translation(gmat.Translation.X, gmat.Translation.Y, gmat.Translation.Z)).ToArray();
  110. var r0 = gmat.GetRow(0);
  111. var r1 = gmat.GetRow(1);
  112. var r2 = gmat.GetRow(2);
  113. var r3 = gmat.GetRow(3);
  114. return new float[] {r0.X, r0.Y, r0.Z, r0.W,
  115. r1.X, r1.Y,r1.Z, r1.W,
  116. r2.X, r2.Y,r2.Z, r2.W,
  117. r3.X, r3.Y,r3.Z, r3.W,};
  118. }
  119. public static void PreparePipeline(IINode node, bool deactivate)
  120. {
  121. var obj = node.ObjectRef;
  122. if (obj == null || obj.SuperClassID != SClass_ID.GenDerivob)
  123. {
  124. return;
  125. }
  126. var derivedObject = obj as IIDerivedObject;
  127. if (derivedObject == null)
  128. {
  129. return;
  130. }
  131. for (var index = 0; index < derivedObject.NumModifiers; index++)
  132. {
  133. var modifier = derivedObject.GetModifier(index);
  134. //if (modifier.ClassID.PartA == 9815843 && modifier.ClassID.PartB == 87654) // Skin
  135. //{
  136. // if (deactivate)
  137. // {
  138. // modifier.DisableMod();
  139. // }
  140. // else
  141. // {
  142. // modifier.EnableMod();
  143. // }
  144. //}
  145. }
  146. }
  147. public static VNormal[] ComputeNormals(IMesh mesh, bool optimize)
  148. {
  149. var vnorms = new VNormal[mesh.NumVerts];
  150. var fnorms = new Vector3[mesh.NumFaces];
  151. for (var index = 0; index < mesh.NumVerts; index++)
  152. {
  153. vnorms[index] = new VNormal();
  154. }
  155. for (var index = 0; index < mesh.NumFaces; index++)
  156. {
  157. var face = mesh.Faces[index];
  158. Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3();
  159. Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3();
  160. Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3();
  161. fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1));
  162. for (var j = 0; j < 3; j++)
  163. {
  164. vnorms[(int)face.V[j]].AddNormal(fnorms[index], optimize ? 1 : face.SmGroup);
  165. }
  166. fnorms[index].Normalize();
  167. }
  168. for (var index = 0; index < mesh.NumVerts; index++)
  169. {
  170. vnorms[index].Normalize();
  171. }
  172. return vnorms;
  173. }
  174. public static bool IsEqualTo(this float[] value, float[] other)
  175. {
  176. if (value.Length != other.Length)
  177. {
  178. return false;
  179. }
  180. return !value.Where((t, i) => Math.Abs(t - other[i]) > Epsilon).Any();
  181. }
  182. public static float[] ToArray(this IMatrix3 value)
  183. {
  184. var row0 = value.GetRow(0).ToArraySwitched();
  185. var row1 = value.GetRow(1).ToArraySwitched();
  186. var row2 = value.GetRow(2).ToArraySwitched();
  187. var row3 = value.GetRow(3).ToArraySwitched();
  188. return new[]
  189. {
  190. row0[0], row0[1], row0[2], 0,
  191. row2[0], row2[1], row2[2], 0,
  192. row1[0], row1[1], row1[2], 0,
  193. row3[0], row3[1], row3[2], 1
  194. };
  195. }
  196. public static IPoint3 ToPoint3(this Vector3 value)
  197. {
  198. return Loader.Global.Point3.Create(value.X, value.Y, value.Z);
  199. }
  200. public static Vector3 ToVector3(this IPoint3 value)
  201. {
  202. return new Vector3(value.X, value.Y, value.Z);
  203. }
  204. public static Quaternion ToQuat(this IQuat value)
  205. {
  206. return new Quaternion(value.X, value.Z, value.Y, value.W);
  207. }
  208. public static float[] ToArray(this IQuat value)
  209. {
  210. return new[] { value.X, value.Z, value.Y, value.W };
  211. }
  212. public static float[] Scale(this IColor value, float scale)
  213. {
  214. return new[] { value.R * scale, value.G * scale, value.B * scale };
  215. }
  216. public static float[] ToArray(this IPoint4 value)
  217. {
  218. return new[] { value.X, value.Y, value.Z, value.W };
  219. }
  220. public static float[] ToArray(this IPoint3 value)
  221. {
  222. return new[] { value.X, value.Y, value.Z };
  223. }
  224. public static float[] ToArray(this IPoint2 value)
  225. {
  226. return new[] { value.X, value.Y };
  227. }
  228. public static float[] ToArraySwitched(this IPoint2 value)
  229. {
  230. return new[] { value.X, 1.0f - value.Y };
  231. }
  232. public static float[] ToArraySwitched(this IPoint3 value)
  233. {
  234. return new[] { value.X, value.Z, value.Y };
  235. }
  236. public static float[] ToArray(this IColor value)
  237. {
  238. return new[] { value.R, value.G, value.B };
  239. }
  240. public static IEnumerable<IINode> Nodes(this IINode node)
  241. {
  242. for (int i = 0; i < node.NumberOfChildren; ++i)
  243. if (node.GetChildNode(i) != null)
  244. yield return node.GetChildNode(i);
  245. }
  246. public static IEnumerable<IINode> NodeTree(this IINode node)
  247. {
  248. foreach (var x in node.Nodes())
  249. {
  250. yield return x;
  251. foreach (var y in x.NodeTree())
  252. yield return y;
  253. }
  254. }
  255. public static IEnumerable<IINode> NodesListBySuperClass(this IINode rootNode, SClass_ID sid)
  256. {
  257. return from n in rootNode.NodeTree() where n.ObjectRef != null && n.EvalWorldState(0, false).Obj.SuperClassID == sid select n;
  258. }
  259. public static IEnumerable<IINode> NodesListBySuperClasses(this IINode rootNode, SClass_ID[] sids)
  260. {
  261. return from n in rootNode.NodeTree() where n.ObjectRef != null && sids.Any(sid => n.EvalWorldState(0, false).Obj.SuperClassID == sid) select n;
  262. }
  263. public static float ConvertFov(float fov)
  264. {
  265. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  266. }
  267. public static bool HasParent(this IINode node)
  268. {
  269. return node.ParentNode != null && node.ParentNode.ObjectRef != null;
  270. }
  271. public static bool IsInstance(this IAnimatable node)
  272. {
  273. var data = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  274. if (data != null)
  275. {
  276. return data.Data[0] != 0;
  277. }
  278. return false;
  279. }
  280. public static void MarkAsInstance(this IAnimatable node)
  281. {
  282. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1, new byte[] { 1 });
  283. }
  284. public static Guid GetGuid(this IAnimatable node)
  285. {
  286. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 0);
  287. Guid uid;
  288. if (uidData != null)
  289. {
  290. uid = new Guid(uidData.Data);
  291. }
  292. else
  293. {
  294. uid = Guid.NewGuid();
  295. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 0, uid.ToByteArray());
  296. }
  297. return uid;
  298. }
  299. public static string GetLocalData(this IAnimatable node)
  300. {
  301. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  302. if (uidData != null)
  303. {
  304. return System.Text.Encoding.UTF8.GetString(uidData.Data);
  305. }
  306. return "";
  307. }
  308. public static void SetLocalData(this IAnimatable node, string value)
  309. {
  310. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  311. if (uidData != null)
  312. {
  313. node.RemoveAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  314. }
  315. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1, System.Text.Encoding.UTF8.GetBytes(value));
  316. }
  317. public static IMatrix3 GetWorldMatrix(this IINode node, int t, bool parent)
  318. {
  319. var tm = node.GetNodeTM(t, Forever);
  320. var ptm = node.ParentNode.GetNodeTM(t, Forever);
  321. if (!parent)
  322. return tm;
  323. if (node.ParentNode.SuperClassID == SClass_ID.Camera)
  324. {
  325. var r = ptm.GetRow(3);
  326. ptm.IdentityMatrix();
  327. ptm.SetRow(3, r);
  328. }
  329. ptm.Invert();
  330. return tm.Multiply(ptm);
  331. }
  332. public static IMatrix3 GetWorldMatrixComplete(this IINode node, int t, bool parent)
  333. {
  334. var tm = node.GetObjTMAfterWSM(t, Forever);
  335. var ptm = node.ParentNode.GetObjTMAfterWSM(t, Forever);
  336. if (!parent)
  337. return tm;
  338. if (node.ParentNode.SuperClassID == SClass_ID.Camera)
  339. {
  340. var r = ptm.GetRow(3);
  341. ptm.IdentityMatrix();
  342. ptm.SetRow(3, r);
  343. }
  344. ptm.Invert();
  345. return tm.Multiply(ptm);
  346. }
  347. public static ITriObject GetMesh(this IObject obj)
  348. {
  349. var triObjectClassId = Loader.Global.Class_ID.Create(0x0009, 0);
  350. if (obj.CanConvertToType(triObjectClassId) == 0)
  351. return null;
  352. return obj.ConvertToType(0, triObjectClassId) as ITriObject;
  353. }
  354. public static bool IsAlmostEqualTo(this float[] current, float[] other, float epsilon)
  355. {
  356. if (current == null && other == null)
  357. {
  358. return true;
  359. }
  360. if (current == null || other == null)
  361. {
  362. return false;
  363. }
  364. if (current.Length != other.Length)
  365. {
  366. return false;
  367. }
  368. for (var i = 0; i < current.Length; ++i)
  369. {
  370. if (Math.Abs(current[i] - other[i]) > epsilon)
  371. {
  372. return false;
  373. }
  374. }
  375. return true;
  376. }
  377. public static bool IsAlmostEqualTo(this IPoint4 current, IPoint4 other, float epsilon)
  378. {
  379. if (Math.Abs(current.X - other.X) > epsilon)
  380. {
  381. return false;
  382. }
  383. if (Math.Abs(current.Y - other.Y) > epsilon)
  384. {
  385. return false;
  386. }
  387. if (Math.Abs(current.Z - other.Z) > epsilon)
  388. {
  389. return false;
  390. }
  391. if (Math.Abs(current.W - other.W) > epsilon)
  392. {
  393. return false;
  394. }
  395. return true;
  396. }
  397. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  398. {
  399. if (Math.Abs(current.X - other.X) > epsilon)
  400. {
  401. return false;
  402. }
  403. if (Math.Abs(current.Y - other.Y) > epsilon)
  404. {
  405. return false;
  406. }
  407. if (Math.Abs(current.Z - other.Z) > epsilon)
  408. {
  409. return false;
  410. }
  411. return true;
  412. }
  413. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  414. {
  415. if (Math.Abs(current.X - other.X) > epsilon)
  416. {
  417. return false;
  418. }
  419. if (Math.Abs(current.Y - other.Y) > epsilon)
  420. {
  421. return false;
  422. }
  423. return true;
  424. }
  425. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  426. {
  427. int state = defaultState;
  428. #if MAX2015
  429. node.GetUserPropBool(propertyName, ref state);
  430. #else
  431. node.GetUserPropBool(ref propertyName, ref state);
  432. #endif
  433. return state == 1;
  434. }
  435. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  436. {
  437. float state = defaultState;
  438. #if MAX2015
  439. node.GetUserPropFloat(propertyName, ref state);
  440. #else
  441. node.GetUserPropFloat(ref propertyName, ref state);
  442. #endif
  443. return state;
  444. }
  445. public static float[] GetVector3Property(this IINode node, string propertyName)
  446. {
  447. float state0 = 0;
  448. string name = propertyName + "_x";
  449. #if MAX2015
  450. node.GetUserPropFloat(name, ref state0);
  451. #else
  452. node.GetUserPropFloat(ref name, ref state0);
  453. #endif
  454. float state1 = 0;
  455. name = propertyName + "_y";
  456. #if MAX2015
  457. node.GetUserPropFloat(name, ref state1);
  458. #else
  459. node.GetUserPropFloat(ref name, ref state1);
  460. #endif
  461. float state2 = 0;
  462. name = propertyName + "_z";
  463. #if MAX2015
  464. node.GetUserPropFloat(name, ref state2);
  465. #else
  466. node.GetUserPropFloat(ref name, ref state2);
  467. #endif
  468. return new[] { state0, state1, state2 };
  469. }
  470. public static bool PrepareCheckBox(CheckBox checkBox, IINode node, string propertyName, int defaultState = 0)
  471. {
  472. var state = node.GetBoolProperty(propertyName, defaultState);
  473. if (checkBox.CheckState == CheckState.Indeterminate)
  474. {
  475. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  476. }
  477. else
  478. {
  479. if (checkBox.ThreeState)
  480. {
  481. if (!state && checkBox.CheckState == CheckState.Checked ||
  482. state && checkBox.CheckState == CheckState.Unchecked)
  483. {
  484. checkBox.CheckState = CheckState.Indeterminate;
  485. return true;
  486. }
  487. }
  488. else
  489. {
  490. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  491. return true;
  492. }
  493. }
  494. return false;
  495. }
  496. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  497. {
  498. checkBox.CheckState = CheckState.Indeterminate;
  499. foreach (var node in nodes)
  500. {
  501. if (PrepareCheckBox(checkBox, node, propertyName, defaultState))
  502. {
  503. break;
  504. }
  505. }
  506. }
  507. public static void UpdateCheckBox(CheckBox checkBox, IINode node, string propertyName)
  508. {
  509. if (checkBox.CheckState != CheckState.Indeterminate)
  510. {
  511. #if MAX2015
  512. node.SetUserPropBool(propertyName, checkBox.CheckState == CheckState.Checked);
  513. #else
  514. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  515. #endif
  516. }
  517. }
  518. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  519. {
  520. foreach (var node in nodes)
  521. {
  522. UpdateCheckBox(checkBox, node, propertyName);
  523. }
  524. }
  525. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  526. {
  527. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  528. }
  529. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  530. {
  531. foreach (var node in nodes)
  532. {
  533. #if MAX2015
  534. node.SetUserPropFloat(propertyName, (float)nup.Value);
  535. #else
  536. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  537. #endif
  538. }
  539. }
  540. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  541. {
  542. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  543. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  544. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  545. }
  546. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  547. {
  548. string name = propertyName + "_x";
  549. #if MAX2015
  550. node.SetUserPropFloat(name, vector3Control.X);
  551. #else
  552. node.SetUserPropFloat(ref name, vector3Control.X);
  553. #endif
  554. name = propertyName + "_y";
  555. #if MAX2015
  556. node.SetUserPropFloat(name, vector3Control.Y);
  557. #else
  558. node.SetUserPropFloat(ref name, vector3Control.Y);
  559. #endif
  560. name = propertyName + "_z";
  561. #if MAX2015
  562. node.SetUserPropFloat(name, vector3Control.Z);
  563. #else
  564. node.SetUserPropFloat(ref name, vector3Control.Z);
  565. #endif
  566. }
  567. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  568. {
  569. foreach (var node in nodes)
  570. {
  571. UpdateVector3Control(vector3Control, node, propertyName);
  572. }
  573. }
  574. public static IMatrix3 ExtractCoordinates(IINode meshNode, BabylonAbstractMesh babylonMesh, bool exportQuaternionsInsteadOfEulers)
  575. {
  576. var wm = meshNode.GetWorldMatrix(0, meshNode.HasParent());
  577. babylonMesh.position = wm.Trans.ToArraySwitched();
  578. var parts = Loader.Global.AffineParts.Create();
  579. Loader.Global.DecompAffine(wm, parts);
  580. if (exportQuaternionsInsteadOfEulers)
  581. {
  582. babylonMesh.rotationQuaternion = parts.Q.ToArray();
  583. }
  584. else
  585. {
  586. var rotate = new float[3];
  587. IntPtr xPtr = Marshal.AllocHGlobal(sizeof(float));
  588. IntPtr yPtr = Marshal.AllocHGlobal(sizeof(float));
  589. IntPtr zPtr = Marshal.AllocHGlobal(sizeof(float));
  590. parts.Q.GetEuler(xPtr, yPtr, zPtr);
  591. Marshal.Copy(xPtr, rotate, 0, 1);
  592. Marshal.Copy(yPtr, rotate, 1, 1);
  593. Marshal.Copy(zPtr, rotate, 2, 1);
  594. var temp = rotate[1];
  595. rotate[0] = -rotate[0] * parts.F;
  596. rotate[1] = -rotate[2] * parts.F;
  597. rotate[2] = -temp * parts.F;
  598. babylonMesh.rotation = rotate;
  599. }
  600. babylonMesh.scaling = parts.K.ToArraySwitched();
  601. return wm;
  602. }
  603. }
  604. }