BabylonExporter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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, bool exportGltfImagesAsBinary, 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, exportGltfImagesAsBinary);
  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. default:
  276. // The type of node is not exportable (helper, spline, xref...)
  277. hasExporter = false;
  278. break;
  279. }
  280. CheckCancelled();
  281. // If node is not exported successfully but is significant
  282. if (babylonNode == null &&
  283. isNodeRelevantToExport(maxGameNode))
  284. {
  285. if (!hasExporter)
  286. {
  287. RaiseWarning($"Type '{maxGameNode.IGameObject.IGameType}' of node '{maxGameNode.Name}' has no exporter, an empty node is exported instead", 1);
  288. }
  289. // Create a dummy (empty mesh)
  290. babylonNode = ExportDummy(maxGameScene, maxGameNode, babylonScene);
  291. };
  292. if (babylonNode != null)
  293. {
  294. // Export its children
  295. for (int i = 0; i < maxGameNode.ChildCount; i++)
  296. {
  297. var descendant = maxGameNode.GetNodeChild(i);
  298. exportNodeRec(descendant, babylonScene, maxGameScene);
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// Return true if node descendant hierarchy has any exportable Mesh, Camera or Light
  304. /// </summary>
  305. private bool isNodeRelevantToExport(IIGameNode maxGameNode)
  306. {
  307. bool isRelevantToExport;
  308. switch (maxGameNode.IGameObject.IGameType)
  309. {
  310. case Autodesk.Max.IGameObject.ObjectTypes.Mesh:
  311. isRelevantToExport = IsMeshExportable(maxGameNode);
  312. break;
  313. case Autodesk.Max.IGameObject.ObjectTypes.Camera:
  314. isRelevantToExport = IsCameraExportable(maxGameNode);
  315. break;
  316. case Autodesk.Max.IGameObject.ObjectTypes.Light:
  317. isRelevantToExport = IsLightExportable(maxGameNode);
  318. break;
  319. default:
  320. isRelevantToExport = false;
  321. break;
  322. }
  323. if (isRelevantToExport)
  324. {
  325. return true;
  326. }
  327. // Descandant recursivity
  328. List<IIGameNode> maxDescendants = getDescendants(maxGameNode);
  329. int indexDescendant = 0;
  330. while (indexDescendant < maxDescendants.Count) // while instead of for to stop as soon as a relevant node has been found
  331. {
  332. if (isNodeRelevantToExport(maxDescendants[indexDescendant]))
  333. {
  334. return true;
  335. }
  336. indexDescendant++;
  337. }
  338. // No relevant node found in hierarchy
  339. return false;
  340. }
  341. private List<IIGameNode> getDescendants(IIGameNode maxGameNode)
  342. {
  343. var maxDescendants = new List<IIGameNode>();
  344. for (int i = 0; i < maxGameNode.ChildCount; i++)
  345. {
  346. maxDescendants.Add(maxGameNode.GetNodeChild(i));
  347. }
  348. return maxDescendants;
  349. }
  350. private HashSet<IIGameNode> getRootNodes(IIGameScene maxGameScene)
  351. {
  352. HashSet<IIGameNode> maxGameNodes = new HashSet<IIGameNode>();
  353. Func<IIGameNode, IIGameNode> getMaxRootNode = delegate (IIGameNode maxGameNode)
  354. {
  355. while (maxGameNode.NodeParent != null)
  356. {
  357. maxGameNode = maxGameNode.NodeParent;
  358. }
  359. return maxGameNode;
  360. };
  361. Action<Autodesk.Max.IGameObject.ObjectTypes> addMaxRootNodes = delegate (Autodesk.Max.IGameObject.ObjectTypes type)
  362. {
  363. ITab<IIGameNode> maxGameNodesOfType = maxGameScene.GetIGameNodeByType(type);
  364. if (maxGameNodesOfType != null)
  365. {
  366. TabToList(maxGameNodesOfType).ForEach(maxGameNode =>
  367. {
  368. var maxRootNode = getMaxRootNode(maxGameNode);
  369. maxGameNodes.Add(maxRootNode);
  370. });
  371. }
  372. };
  373. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  374. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Light);
  375. addMaxRootNodes(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  376. return maxGameNodes;
  377. }
  378. private static List<T> TabToList<T>(ITab<T> tab)
  379. {
  380. if (tab == null)
  381. {
  382. return null;
  383. }
  384. else
  385. {
  386. List<T> list = new List<T>();
  387. for (int i = 0; i < tab.Count; i++)
  388. {
  389. #if MAX2017
  390. var indexer = i;
  391. #else
  392. var indexer = new IntPtr(i);
  393. Marshal.FreeHGlobal(indexer);
  394. #endif
  395. list.Add(tab[indexer]);
  396. }
  397. return list;
  398. }
  399. }
  400. }
  401. }