Tools.cs 27 KB

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