Tools.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using Autodesk.Max;
  6. using Autodesk.Max.IMXSDebugger;
  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 float[] ToArray(this IMatrix3 value)
  16. {
  17. var row0 = value.GetRow(0).ToArraySwitched();
  18. var row1 = value.GetRow(1).ToArraySwitched();
  19. var row2 = value.GetRow(2).ToArraySwitched();
  20. var row3 = value.GetRow(3).ToArraySwitched();
  21. return new[]
  22. {
  23. row0[0], row0[1], row0[2], 0,
  24. row2[0], row2[1], row2[2], 0,
  25. row1[0], row1[1], row1[2], 0,
  26. row3[0], row3[1], row3[2], 1
  27. };
  28. }
  29. public static Quaternion ToQuat(this IQuat value)
  30. {
  31. return new Quaternion(value.X, value.Z, value.Y, value.W );
  32. }
  33. public static float[] ToArray(this IQuat value)
  34. {
  35. return new[] { value.X, value.Z, value.Y, value.W };
  36. }
  37. public static float[] Scale(this Color value, float scale)
  38. {
  39. return new[] { value.r * scale, value.g * scale, value.b * scale };
  40. }
  41. public static float[] ToArray(this Color value)
  42. {
  43. return new[] { value.r, value.g, value.b };
  44. }
  45. public static float[] ToArray(this IPoint3 value)
  46. {
  47. return new[] { value.X, value.Y, value.Z };
  48. }
  49. public static float[] ToArray(this IPoint2 value)
  50. {
  51. return new[] { value.X, value.Y };
  52. }
  53. public static float[] ToArraySwitched(this IPoint2 value)
  54. {
  55. return new[] { value.X, 1.0f - value.Y };
  56. }
  57. public static float[] ToArraySwitched(this IPoint3 value)
  58. {
  59. return new[] { value.X, value.Z, value.Y };
  60. }
  61. public static float[] ToArray(this IColor value)
  62. {
  63. return new[] { value.R, value.G, value.B };
  64. }
  65. public static IEnumerable<Node> NodesListBySuperClass(this Scene scene, SuperClassID sid)
  66. {
  67. return from n in scene.NodeTree where n.Object != null && n.Object.SuperClassID == sid select n;
  68. }
  69. public static float ConvertFov(float fov)
  70. {
  71. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  72. }
  73. public static bool HasParent(this Node node)
  74. {
  75. return node.Parent != null && node.Parent.Object != null;
  76. }
  77. public static Guid GetGuid(this Animatable node)
  78. {
  79. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  80. var uidData = appData.GetChunk(0);
  81. Guid uid;
  82. if (uidData != null)
  83. {
  84. uid = new Guid(uidData);
  85. }
  86. else
  87. {
  88. uid = Guid.NewGuid();
  89. appData.AddChunk(0, uid.ToByteArray());
  90. }
  91. return uid;
  92. }
  93. public static Guid GetGuid(this IINode node)
  94. {
  95. return GetGuid(Animatable.CreateWrapper<Node>(node));
  96. }
  97. public static string GetLocalData(this Node node)
  98. {
  99. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  100. var uidData = appData.GetChunk(1);
  101. if (uidData != null)
  102. {
  103. return System.Text.Encoding.UTF8.GetString(uidData);
  104. }
  105. return "";
  106. }
  107. public static void SetLocalData(this Node node, string value)
  108. {
  109. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  110. var uidData = appData.GetChunk(1);
  111. if (uidData != null)
  112. {
  113. appData.RemoveChunk(1);
  114. }
  115. appData.AddChunk(1, System.Text.Encoding.UTF8.GetBytes(value));
  116. }
  117. public static IMatrix3 GetWorldMatrix(this Node node, TimeValue t, bool parent)
  118. {
  119. var innerNode = node._Node;
  120. var tm = innerNode.GetNodeTM(t, Interval.Forever._IInterval);
  121. var ptm = innerNode.ParentNode.GetNodeTM(t, Interval.Forever._IInterval);
  122. if (!parent)
  123. return tm;
  124. if (innerNode.ParentNode.SuperClassID == SuperClassID.Camera)
  125. {
  126. var r = ptm.GetRow(3);
  127. ptm.IdentityMatrix();
  128. ptm.SetRow(3, r);
  129. }
  130. ptm.Invert();
  131. return tm.Multiply(ptm);
  132. }
  133. public static IMesh GetMesh(this IObject obj)
  134. {
  135. if (obj.CanConvertToType(ClassID.TriObject._IClass_ID) == 0)
  136. return null;
  137. var tri = obj.ConvertToType(0, ClassID.TriObject._IClass_ID) as ITriObject;
  138. return tri == null ? null : tri.Mesh;
  139. }
  140. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  141. {
  142. if (Math.Abs(current.X - other.X) > epsilon)
  143. {
  144. return false;
  145. }
  146. if (Math.Abs(current.Y - other.Y) > epsilon)
  147. {
  148. return false;
  149. }
  150. if (Math.Abs(current.Z - other.Z) > epsilon)
  151. {
  152. return false;
  153. }
  154. return true;
  155. }
  156. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  157. {
  158. if (Math.Abs(current.X - other.X) > epsilon)
  159. {
  160. return false;
  161. }
  162. if (Math.Abs(current.Y - other.Y) > epsilon)
  163. {
  164. return false;
  165. }
  166. return true;
  167. }
  168. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  169. {
  170. int state = defaultState;
  171. node.GetUserPropBool(ref propertyName, ref state);
  172. return state == 1;
  173. }
  174. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  175. {
  176. float state = defaultState;
  177. node.GetUserPropFloat(ref propertyName, ref state);
  178. return state;
  179. }
  180. public static float[] GetVector3Property(this IINode node, string propertyName)
  181. {
  182. float state0 = 0;
  183. string name = propertyName + "_x";
  184. node.GetUserPropFloat(ref name, ref state0);
  185. float state1 = 0;
  186. name = propertyName + "_y";
  187. node.GetUserPropFloat(ref name, ref state1);
  188. float state2 = 0;
  189. name = propertyName + "_z";
  190. node.GetUserPropFloat(ref name, ref state2);
  191. return new[] { state0, state1, state2 };
  192. }
  193. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  194. {
  195. checkBox.CheckState = CheckState.Indeterminate;
  196. foreach (var node in nodes)
  197. {
  198. var state = node.GetBoolProperty(propertyName, defaultState);
  199. if (checkBox.CheckState == CheckState.Indeterminate)
  200. {
  201. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  202. }
  203. else
  204. {
  205. if (!state && checkBox.CheckState == CheckState.Checked ||
  206. state && checkBox.CheckState == CheckState.Unchecked)
  207. {
  208. checkBox.CheckState = CheckState.Indeterminate;
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  215. {
  216. foreach (var node in nodes)
  217. {
  218. if (checkBox.CheckState != CheckState.Indeterminate)
  219. {
  220. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  221. }
  222. }
  223. }
  224. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  225. {
  226. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  227. }
  228. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  229. {
  230. foreach (var node in nodes)
  231. {
  232. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  233. }
  234. }
  235. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  236. {
  237. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  238. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  239. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  240. }
  241. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  242. {
  243. string name = propertyName + "_x";
  244. node.SetUserPropFloat(ref name, vector3Control.X);
  245. name = propertyName + "_y";
  246. node.SetUserPropFloat(ref name, vector3Control.Y);
  247. name = propertyName + "_z";
  248. node.SetUserPropFloat(ref name, vector3Control.Z);
  249. }
  250. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  251. {
  252. foreach (var node in nodes)
  253. {
  254. UpdateVector3Control(vector3Control, node, propertyName);
  255. }
  256. }
  257. }
  258. }