Tools.cs 20 KB

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