Tools.cs 16 KB

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