Tools.cs 18 KB

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