BabylonExporter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using Autodesk.Max;
  2. using BabylonExport.Entities;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using Color = System.Drawing.Color;
  14. namespace Max2Babylon
  15. {
  16. internal partial class BabylonExporter
  17. {
  18. public event Action<int> OnImportProgressChanged;
  19. public event Action<string, int> OnWarning;
  20. public event Action<string, Color, int, bool> OnMessage;
  21. public event Action<string, int> OnError;
  22. public bool AutoSave3dsMaxFile { get; set; }
  23. public bool ExportHiddenObjects { get; set; }
  24. public bool IsCancelled { get; set; }
  25. public bool CopyTexturesToOutput { get; set; }
  26. public string MaxSceneFileName { get; set; }
  27. public bool ExportQuaternionsInsteadOfEulers { get; set; }
  28. void ReportProgressChanged(int progress)
  29. {
  30. if (OnImportProgressChanged != null)
  31. {
  32. OnImportProgressChanged(progress);
  33. }
  34. }
  35. void RaiseError(string error, int rank = 0)
  36. {
  37. if (OnError != null)
  38. {
  39. OnError(error, rank);
  40. }
  41. }
  42. void RaiseWarning(string warning, int rank = 0)
  43. {
  44. if (OnWarning != null)
  45. {
  46. OnWarning(warning, rank);
  47. }
  48. }
  49. void RaiseMessage(string message, int rank = 0, bool emphasis = false)
  50. {
  51. RaiseMessage(message, Color.Black, rank, emphasis);
  52. }
  53. void RaiseMessage(string message, Color color, int rank = 0, bool emphasis = false)
  54. {
  55. if (OnMessage != null)
  56. {
  57. OnMessage(message, color, rank, emphasis);
  58. }
  59. }
  60. void CheckCancelled()
  61. {
  62. Application.DoEvents();
  63. if (IsCancelled)
  64. {
  65. throw new OperationCanceledException();
  66. }
  67. }
  68. public async Task ExportAsync(string outputFile, bool generateManifest, bool onlySelected, bool generateBinary, bool exportGltf, Form callerForm)
  69. {
  70. var gameConversionManger = Loader.Global.ConversionManager;
  71. gameConversionManger.CoordSystem = Autodesk.Max.IGameConversionManager.CoordSystem.D3d;
  72. var gameScene = Loader.Global.IGameInterface;
  73. gameScene.InitialiseIGame(onlySelected);
  74. gameScene.SetStaticFrame(0);
  75. MaxSceneFileName = gameScene.SceneFileName;
  76. IsCancelled = false;
  77. RaiseMessage("Exportation started", Color.Blue);
  78. ReportProgressChanged(0);
  79. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  80. var rawScene = Loader.Core.RootNode;
  81. if (!Directory.Exists(babylonScene.OutputPath))
  82. {
  83. RaiseError("Exportation stopped: Output folder does not exist");
  84. ReportProgressChanged(100);
  85. return;
  86. }
  87. var watch = new Stopwatch();
  88. watch.Start();
  89. // Save scene
  90. RaiseMessage("Saving 3ds max file");
  91. if (AutoSave3dsMaxFile)
  92. {
  93. var forceSave = Loader.Core.FileSave;
  94. callerForm?.BringToFront();
  95. }
  96. // Producer
  97. babylonScene.producer = new BabylonProducer
  98. {
  99. name = "3dsmax",
  100. #if MAX2017
  101. version = "2017",
  102. #else
  103. version = Loader.Core.ProductVersion.ToString(),
  104. #endif
  105. exporter_version = "0.4.5",
  106. file = Path.GetFileName(outputFile)
  107. };
  108. // Global
  109. babylonScene.autoClear = true;
  110. babylonScene.clearColor = Loader.Core.GetBackGround(0, Tools.Forever).ToArray();
  111. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Tools.Forever).ToArray();
  112. babylonScene.gravity = rawScene.GetVector3Property("babylonjs_gravity");
  113. ExportQuaternionsInsteadOfEulers = rawScene.GetBoolProperty("babylonjs_exportquaternions", 1);
  114. // Sounds
  115. var soundName = rawScene.GetStringProperty("babylonjs_sound_filename", "");
  116. if (!string.IsNullOrEmpty(soundName))
  117. {
  118. var filename = Path.GetFileName(soundName);
  119. var globalSound = new BabylonSound
  120. {
  121. autoplay = rawScene.GetBoolProperty("babylonjs_sound_autoplay", 1),
  122. loop = rawScene.GetBoolProperty("babylonjs_sound_loop", 1),
  123. name = filename
  124. };
  125. babylonScene.SoundsList.Add(globalSound);
  126. try
  127. {
  128. File.Copy(soundName, Path.Combine(babylonScene.OutputPath, filename), true);
  129. }
  130. catch
  131. {
  132. }
  133. }
  134. // Root nodes
  135. RaiseMessage("Exporting nodes");
  136. HashSet<IIGameNode> maxRootNodes = getRootNodes(gameScene);
  137. var progressionStep = 80.0f / maxRootNodes.Count;
  138. var progression = 10.0f;
  139. ReportProgressChanged((int)progression);
  140. referencedMaterials.Clear();
  141. foreach (var maxRootNode in maxRootNodes)
  142. {
  143. exportNodeRec(maxRootNode, babylonScene, gameScene);
  144. progression += progressionStep;
  145. ReportProgressChanged((int)progression);
  146. CheckCancelled();
  147. };
  148. RaiseMessage(string.Format("Total meshes: {0}", babylonScene.MeshesList.Count), Color.Gray, 1);
  149. // Main camera
  150. BabylonCamera babylonMainCamera = null;
  151. ICameraObject maxMainCameraObject = null;
  152. if (babylonMainCamera == null && babylonScene.CamerasList.Count > 0)
  153. {
  154. // Set first camera as main one
  155. babylonMainCamera = babylonScene.CamerasList[0];
  156. babylonScene.activeCameraID = babylonMainCamera.id;
  157. RaiseMessage("Active camera set to " + babylonMainCamera.name, Color.Green, 1, true);
  158. // Retreive camera node with same GUID
  159. var maxCameraNodesAsTab = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  160. var maxCameraNodes = TabToList(maxCameraNodesAsTab);
  161. var maxMainCameraNode = maxCameraNodes.Find(_camera => _camera.MaxNode.GetGuid().ToString() == babylonMainCamera.id);
  162. maxMainCameraObject = (maxMainCameraNode.MaxNode.ObjectRef as ICameraObject);
  163. }
  164. if (babylonMainCamera == null)
  165. {
  166. RaiseWarning("No camera defined", 1);
  167. }
  168. else
  169. {
  170. RaiseMessage(string.Format("Total cameras: {0}", babylonScene.CamerasList.Count), Color.Gray, 1);
  171. }
  172. // Default light
  173. if (babylonScene.LightsList.Count == 0)
  174. {
  175. RaiseWarning("No light defined", 1);
  176. RaiseWarning("A default hemispheric light was added for your convenience", 1);
  177. ExportDefaultLight(babylonScene);
  178. }
  179. else
  180. {
  181. RaiseMessage(string.Format("Total lights: {0}", babylonScene.LightsList.Count), Color.Gray, 1);
  182. }
  183. // Materials
  184. RaiseMessage("Exporting materials");
  185. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  186. foreach (var mat in matsToExport)
  187. {
  188. ExportMaterial(mat, babylonScene);
  189. CheckCancelled();
  190. }
  191. RaiseMessage(string.Format("Total: {0}", babylonScene.MaterialsList.Count + babylonScene.MultiMaterialsList.Count), Color.Gray, 1);
  192. // Fog
  193. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  194. {
  195. var atmospheric = Loader.Core.GetAtmospheric(index);
  196. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  197. {
  198. var fog = atmospheric as IStdFog;
  199. RaiseMessage("Exporting fog");
  200. if (fog != null)
  201. {
  202. babylonScene.fogColor = fog.GetColor(0).ToArray();
  203. babylonScene.fogMode = 3;
  204. }
  205. if (babylonMainCamera != null)
  206. {
  207. babylonScene.fogStart = maxMainCameraObject.GetEnvRange(0, 0, Tools.Forever);
  208. babylonScene.fogEnd = maxMainCameraObject.GetEnvRange(0, 1, Tools.Forever);
  209. }
  210. }
  211. }
  212. // Skeletons
  213. if (skins.Count > 0)
  214. {
  215. RaiseMessage("Exporting skeletons");
  216. foreach (var skin in skins)
  217. {
  218. ExportSkin(skin, babylonScene);
  219. }
  220. }
  221. // Actions
  222. babylonScene.actions = ExportNodeAction(gameScene.GetIGameNode(rawScene));
  223. // Output
  224. RaiseMessage("Saving to output file");
  225. babylonScene.Prepare(false, false);
  226. var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
  227. var sb = new StringBuilder();
  228. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  229. await Task.Run(() =>
  230. {
  231. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  232. {
  233. jsonWriter.Formatting = Formatting.None;
  234. jsonSerializer.Serialize(jsonWriter, babylonScene);
  235. }
  236. File.WriteAllText(outputFile, sb.ToString());
  237. if (generateManifest)
  238. {
  239. File.WriteAllText(outputFile + ".manifest",
  240. "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
  241. }
  242. });
  243. // Binary
  244. if (generateBinary)
  245. {
  246. RaiseMessage("Generating binary files");
  247. BabylonFileConverter.BinaryConverter.Convert(outputFile, Path.GetDirectoryName(outputFile) + "\\Binary",
  248. message => RaiseMessage(message, 1),
  249. error => RaiseError(error, 1));
  250. }
  251. ReportProgressChanged(100);
  252. // Export glTF
  253. if (exportGltf)
  254. {
  255. ExportGltf(babylonScene, outputFile, generateBinary);
  256. }
  257. watch.Stop();
  258. RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
  259. }
  260. private void exportNodeRec(IIGameNode maxGameNode, BabylonScene babylonScene, IIGameScene maxGameScene)
  261. {
  262. BabylonNode babylonNode = null;
  263. bool hasExporter = true;
  264. switch (maxGameNode.IGameObject.IGameType)
  265. {
  266. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  267. babylonNode = ExportMesh(maxGameScene, maxGameNode, babylonScene);
  268. break;
  269. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  270. babylonNode = ExportCamera(maxGameScene, maxGameNode, babylonScene);
  271. break;
  272. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  273. babylonNode = ExportLight(maxGameScene, maxGameNode, babylonScene);
  274. break;
  275. case Autodesk.Max.IGameObject.ObjectTypes.Unknown:
  276. // Create a dummy (empty mesh) when type is unknown
  277. // An example of unknown type object is the target of target light or camera
  278. babylonNode = ExportDummy(maxGameScene, maxGameNode, babylonScene);
  279. break;
  280. default:
  281. // The type of node is not exportable (helper, spline, xref...)
  282. hasExporter = false;
  283. break;
  284. }
  285. CheckCancelled();
  286. // If node is not exported successfully but is significant
  287. if (babylonNode == null &&
  288. isNodeRelevantToExport(maxGameNode))
  289. {
  290. if (!hasExporter)
  291. {
  292. RaiseWarning($"Type '{maxGameNode.IGameObject.IGameType}' of node '{maxGameNode.Name}' has no exporter, an empty node is exported instead", 1);
  293. }
  294. // Create a dummy (empty mesh)
  295. babylonNode = ExportDummy(maxGameScene, maxGameNode, babylonScene);
  296. };
  297. if (babylonNode != null)
  298. {
  299. // Export its children
  300. for (int i = 0; i < maxGameNode.ChildCount; i++)
  301. {
  302. var descendant = maxGameNode.GetNodeChild(i);
  303. exportNodeRec(descendant, babylonScene, maxGameScene);
  304. }
  305. }
  306. }
  307. /// <summary>
  308. /// Return true if node descendant hierarchy has any exportable Mesh, Camera or Light
  309. /// </summary>
  310. private bool isNodeRelevantToExport(IIGameNode maxGameNode)
  311. {
  312. bool isRelevantToExport;
  313. switch (maxGameNode.IGameObject.IGameType)
  314. {
  315. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  316. isRelevantToExport = IsMeshExportable(maxGameNode);
  317. break;
  318. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  319. isRelevantToExport = IsCameraExportable(maxGameNode);
  320. break;
  321. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  322. isRelevantToExport = IsLightExportable(maxGameNode);
  323. break;
  324. default:
  325. isRelevantToExport = false;
  326. break;
  327. }
  328. if (isRelevantToExport)
  329. {
  330. return true;
  331. }
  332. // Descandant recursivity
  333. List<IIGameNode> maxDescendants = getDescendants(maxGameNode);
  334. int indexDescendant = 0;
  335. while (indexDescendant < maxDescendants.Count) // while instead of for to stop as soon as a relevant node has been found
  336. {
  337. if (isNodeRelevantToExport(maxDescendants[indexDescendant]))
  338. {
  339. return true;
  340. }
  341. indexDescendant++;
  342. }
  343. // No relevant node found in hierarchy
  344. return false;
  345. }
  346. private List<IIGameNode> getDescendants(IIGameNode maxGameNode)
  347. {
  348. var maxDescendants = new List<IIGameNode>();
  349. for (int i = 0; i < maxGameNode.ChildCount; i++)
  350. {
  351. maxDescendants.Add(maxGameNode.GetNodeChild(i));
  352. }
  353. return maxDescendants;
  354. }
  355. private HashSet<IIGameNode> getRootNodes(IIGameScene maxGameScene)
  356. {
  357. HashSet<IIGameNode> maxGameNodes = new HashSet<IIGameNode>();
  358. Func<IIGameNode, IIGameNode> getMaxRootNode = delegate (IIGameNode maxGameNode)
  359. {
  360. while (maxGameNode.NodeParent != null)
  361. {
  362. maxGameNode = maxGameNode.NodeParent;
  363. }
  364. return maxGameNode;
  365. };
  366. Action<Autodesk.Max.IGameObject.ObjectTypes> addMaxRootNodes = delegate (Autodesk.Max.IGameObject.ObjectTypes type)
  367. {
  368. ITab<IIGameNode> maxGameNodesOfType = maxGameScene.GetIGameNodeByType(type);
  369. if (maxGameNodesOfType != null)
  370. {
  371. TabToList(maxGameNodesOfType).ForEach(maxGameNode =>
  372. {
  373. var maxRootNode = getMaxRootNode(maxGameNode);
  374. maxGameNodes.Add(maxRootNode);
  375. });
  376. }
  377. };
  378. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  379. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Light);
  380. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  381. return maxGameNodes;
  382. }
  383. private static List<T> TabToList<T>(ITab<T> tab)
  384. {
  385. if (tab == null)
  386. {
  387. return null;
  388. }
  389. else
  390. {
  391. List<T> list = new List<T>();
  392. for (int i = 0; i < tab.Count; i++)
  393. {
  394. #if MAX2017
  395. var indexer = i;
  396. #else
  397. var indexer = new IntPtr(i);
  398. Marshal.FreeHGlobal(indexer);
  399. #endif
  400. list.Add(tab[indexer]);
  401. }
  402. return list;
  403. }
  404. }
  405. }
  406. }