Tools.cs 27 KB

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