BabylonExporter.cs 17 KB

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