Tools.cs 27 KB

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