Tools.cs 29 KB

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