Tools.cs 16 KB

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