Tools.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using Autodesk.Max;
  6. using MaxSharp;
  7. using SharpDX;
  8. using Color = MaxSharp.Color;
  9. namespace Max2Babylon
  10. {
  11. public static class Tools
  12. {
  13. public const float Epsilon = 0.001f;
  14. public static void PreparePipeline(IINode node, bool deactivate)
  15. {
  16. var obj = node.ObjectRef;
  17. if (obj.SuperClassID != SClass_ID.GenDerivob)
  18. {
  19. return;
  20. }
  21. var derivedObject = obj as IIDerivedObject;
  22. if (derivedObject == null)
  23. {
  24. return;
  25. }
  26. for (var index = 0; index < derivedObject.NumModifiers; index++)
  27. {
  28. var modifier = derivedObject.GetModifier(index);
  29. if (modifier.ClassID.PartA == 9815843 && modifier.ClassID.PartB == 87654) // Skin
  30. {
  31. if (deactivate)
  32. {
  33. modifier.DisableMod();
  34. }
  35. else
  36. {
  37. modifier.EnableMod();
  38. }
  39. }
  40. }
  41. }
  42. public static VNormal[] ComputeNormals(IMesh mesh)
  43. {
  44. var vnorms = new VNormal[mesh.NumVerts];
  45. var fnorms = new Vector3[mesh.NumFaces];
  46. for (var index = 0; index < mesh.NumVerts; index++)
  47. {
  48. vnorms[index] = new VNormal();
  49. }
  50. for (var index = 0; index < mesh.NumFaces; index++)
  51. {
  52. var face = mesh.Faces[index];
  53. Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3();
  54. Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3();
  55. Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3();
  56. fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1));
  57. for (var j = 0; j < 3; j++)
  58. {
  59. vnorms[(int)face.V[j]].AddNormal(fnorms[index], face.SmGroup);
  60. }
  61. fnorms[index].Normalize();
  62. }
  63. for (var index = 0; index < mesh.NumVerts; index++)
  64. {
  65. vnorms[index].Normalize();
  66. }
  67. return vnorms;
  68. }
  69. public static bool IsEqualTo(this float[] value, float[] other)
  70. {
  71. if (value.Length != other.Length)
  72. {
  73. return false;
  74. }
  75. return !value.Where((t, i) => Math.Abs(t - other[i]) > Epsilon).Any();
  76. }
  77. public static float[] ToArray(this IMatrix3 value)
  78. {
  79. var row0 = value.GetRow(0).ToArraySwitched();
  80. var row1 = value.GetRow(1).ToArraySwitched();
  81. var row2 = value.GetRow(2).ToArraySwitched();
  82. var row3 = value.GetRow(3).ToArraySwitched();
  83. return new[]
  84. {
  85. row0[0], row0[1], row0[2], 0,
  86. row2[0], row2[1], row2[2], 0,
  87. row1[0], row1[1], row1[2], 0,
  88. row3[0], row3[1], row3[2], 1
  89. };
  90. }
  91. public static IPoint3 ToPoint3(this Vector3 value)
  92. {
  93. return Loader.Global.Point3.Create(value.X, value.Y, value.Z);
  94. }
  95. public static Vector3 ToVector3(this IPoint3 value)
  96. {
  97. return new Vector3(value.X, value.Y, value.Z);
  98. }
  99. public static Quaternion ToQuat(this IQuat value)
  100. {
  101. return new Quaternion(value.X, value.Z, value.Y, value.W );
  102. }
  103. public static float[] ToArray(this IQuat value)
  104. {
  105. return new[] { value.X, value.Z, value.Y, value.W };
  106. }
  107. public static float[] Scale(this Color value, float scale)
  108. {
  109. return new[] { value.r * scale, value.g * scale, value.b * scale };
  110. }
  111. public static float[] ToArray(this Color value)
  112. {
  113. return new[] { value.r, value.g, value.b };
  114. }
  115. public static float[] ToArray(this IPoint3 value)
  116. {
  117. return new[] { value.X, value.Y, value.Z };
  118. }
  119. public static float[] ToArray(this IPoint2 value)
  120. {
  121. return new[] { value.X, value.Y };
  122. }
  123. public static float[] ToArraySwitched(this IPoint2 value)
  124. {
  125. return new[] { value.X, 1.0f - value.Y };
  126. }
  127. public static float[] ToArraySwitched(this IPoint3 value)
  128. {
  129. return new[] { value.X, value.Z, value.Y };
  130. }
  131. public static float[] ToArray(this IColor value)
  132. {
  133. return new[] { value.R, value.G, value.B };
  134. }
  135. public static IEnumerable<Node> NodesListBySuperClass(this Scene scene, SuperClassID sid)
  136. {
  137. return from n in scene.NodeTree where n.Object != null && n._Node.EvalWorldState(0, false).Obj.SuperClassID == sid select n;
  138. }
  139. public static IEnumerable<Node> NodesListBySuperClasses(this Scene scene, SuperClassID[] sids)
  140. {
  141. return from n in scene.NodeTree where n.Object != null && sids.Any(sid => n._Node.EvalWorldState(0, false).Obj.SuperClassID == sid) select n;
  142. }
  143. public static float ConvertFov(float fov)
  144. {
  145. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  146. }
  147. public static bool HasParent(this Node node)
  148. {
  149. return node.Parent != null && node.Parent.Object != null;
  150. }
  151. public static Guid GetGuid(this Animatable node)
  152. {
  153. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  154. var uidData = appData.GetChunk(0);
  155. Guid uid;
  156. if (uidData != null)
  157. {
  158. uid = new Guid(uidData);
  159. }
  160. else
  161. {
  162. uid = Guid.NewGuid();
  163. appData.AddChunk(0, uid.ToByteArray());
  164. }
  165. return uid;
  166. }
  167. public static Guid GetGuid(this IINode node)
  168. {
  169. return GetGuid(Animatable.CreateWrapper<Node>(node));
  170. }
  171. public static string GetLocalData(this Node node)
  172. {
  173. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  174. var uidData = appData.GetChunk(1);
  175. if (uidData != null)
  176. {
  177. return System.Text.Encoding.UTF8.GetString(uidData);
  178. }
  179. return "";
  180. }
  181. public static void SetLocalData(this Node node, string value)
  182. {
  183. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  184. var uidData = appData.GetChunk(1);
  185. if (uidData != null)
  186. {
  187. appData.RemoveChunk(1);
  188. }
  189. appData.AddChunk(1, System.Text.Encoding.UTF8.GetBytes(value));
  190. }
  191. public static IMatrix3 GetWorldMatrix(this Node node, TimeValue t, bool parent)
  192. {
  193. var innerNode = node._Node;
  194. var tm = innerNode.GetNodeTM(t, Interval.Forever._IInterval);
  195. var ptm = innerNode.ParentNode.GetNodeTM(t, Interval.Forever._IInterval);
  196. if (!parent)
  197. return tm;
  198. if (innerNode.ParentNode.SuperClassID == SuperClassID.Camera)
  199. {
  200. var r = ptm.GetRow(3);
  201. ptm.IdentityMatrix();
  202. ptm.SetRow(3, r);
  203. }
  204. ptm.Invert();
  205. return tm.Multiply(ptm);
  206. }
  207. public static ITriObject GetMesh(this IObject obj, out bool mustBeDeleted)
  208. {
  209. mustBeDeleted = false;
  210. if (obj.CanConvertToType(ClassID.TriObject._IClass_ID) == 0)
  211. return null;
  212. var tri = obj.ConvertToType(0, ClassID.TriObject._IClass_ID) as ITriObject;
  213. mustBeDeleted = (tri != obj);
  214. return tri;
  215. }
  216. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  217. {
  218. if (Math.Abs(current.X - other.X) > epsilon)
  219. {
  220. return false;
  221. }
  222. if (Math.Abs(current.Y - other.Y) > epsilon)
  223. {
  224. return false;
  225. }
  226. if (Math.Abs(current.Z - other.Z) > epsilon)
  227. {
  228. return false;
  229. }
  230. return true;
  231. }
  232. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  233. {
  234. if (Math.Abs(current.X - other.X) > epsilon)
  235. {
  236. return false;
  237. }
  238. if (Math.Abs(current.Y - other.Y) > epsilon)
  239. {
  240. return false;
  241. }
  242. return true;
  243. }
  244. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  245. {
  246. int state = defaultState;
  247. node.GetUserPropBool(ref propertyName, ref state);
  248. return state == 1;
  249. }
  250. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  251. {
  252. float state = defaultState;
  253. node.GetUserPropFloat(ref propertyName, ref state);
  254. return state;
  255. }
  256. public static float[] GetVector3Property(this IINode node, string propertyName)
  257. {
  258. float state0 = 0;
  259. string name = propertyName + "_x";
  260. node.GetUserPropFloat(ref name, ref state0);
  261. float state1 = 0;
  262. name = propertyName + "_y";
  263. node.GetUserPropFloat(ref name, ref state1);
  264. float state2 = 0;
  265. name = propertyName + "_z";
  266. node.GetUserPropFloat(ref name, ref state2);
  267. return new[] { state0, state1, state2 };
  268. }
  269. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  270. {
  271. checkBox.CheckState = CheckState.Indeterminate;
  272. foreach (var node in nodes)
  273. {
  274. var state = node.GetBoolProperty(propertyName, defaultState);
  275. if (checkBox.CheckState == CheckState.Indeterminate)
  276. {
  277. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  278. }
  279. else
  280. {
  281. if (!state && checkBox.CheckState == CheckState.Checked ||
  282. state && checkBox.CheckState == CheckState.Unchecked)
  283. {
  284. checkBox.CheckState = CheckState.Indeterminate;
  285. break;
  286. }
  287. }
  288. }
  289. }
  290. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  291. {
  292. foreach (var node in nodes)
  293. {
  294. if (checkBox.CheckState != CheckState.Indeterminate)
  295. {
  296. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  297. }
  298. }
  299. }
  300. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  301. {
  302. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  303. }
  304. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  305. {
  306. foreach (var node in nodes)
  307. {
  308. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  309. }
  310. }
  311. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  312. {
  313. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  314. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  315. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  316. }
  317. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  318. {
  319. string name = propertyName + "_x";
  320. node.SetUserPropFloat(ref name, vector3Control.X);
  321. name = propertyName + "_y";
  322. node.SetUserPropFloat(ref name, vector3Control.Y);
  323. name = propertyName + "_z";
  324. node.SetUserPropFloat(ref name, vector3Control.Z);
  325. }
  326. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  327. {
  328. foreach (var node in nodes)
  329. {
  330. UpdateVector3Control(vector3Control, node, propertyName);
  331. }
  332. }
  333. }
  334. }