BabylonExporter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using Autodesk.Max;
  2. using BabylonExport.Entities;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Color = System.Drawing.Color;
  12. namespace Max2Babylon
  13. {
  14. internal partial class BabylonExporter
  15. {
  16. public event Action<int> OnImportProgressChanged;
  17. public event Action<string, int> OnWarning;
  18. public event Action<string, Color, int, bool> OnMessage;
  19. public event Action<string, int> OnError;
  20. public bool AutoSave3dsMaxFile { get; set; }
  21. public bool ExportHiddenObjects { get; set; }
  22. public bool IsCancelled { get; set; }
  23. public bool CopyTexturesToOutput { get; set; }
  24. public string MaxSceneFileName { get; set; }
  25. public bool ExportQuaternionsInsteadOfEulers { get; set; }
  26. void ReportProgressChanged(int progress)
  27. {
  28. if (OnImportProgressChanged != null)
  29. {
  30. OnImportProgressChanged(progress);
  31. }
  32. }
  33. void RaiseError(string error, int rank = 0)
  34. {
  35. if (OnError != null)
  36. {
  37. OnError(error, rank);
  38. }
  39. }
  40. void RaiseWarning(string warning, int rank = 0)
  41. {
  42. if (OnWarning != null)
  43. {
  44. OnWarning(warning, rank);
  45. }
  46. }
  47. void RaiseMessage(string message, int rank = 0, bool emphasis = false)
  48. {
  49. RaiseMessage(message, Color.Black, rank, emphasis);
  50. }
  51. void RaiseMessage(string message, Color color, int rank = 0, bool emphasis = false)
  52. {
  53. if (OnMessage != null)
  54. {
  55. OnMessage(message, color, rank, emphasis);
  56. }
  57. }
  58. void CheckCancelled()
  59. {
  60. Application.DoEvents();
  61. if (IsCancelled)
  62. {
  63. throw new OperationCanceledException();
  64. }
  65. }
  66. public async Task ExportAsync(string outputFile, bool generateManifest, bool onlySelected, bool generateBinary, bool exportGltf, Form callerForm)
  67. {
  68. var gameConversionManger = Loader.Global.ConversionManager;
  69. gameConversionManger.CoordSystem = Autodesk.Max.IGameConversionManager.CoordSystem.D3d;
  70. var gameScene = Loader.Global.IGameInterface;
  71. gameScene.InitialiseIGame(onlySelected);
  72. gameScene.SetStaticFrame(0);
  73. MaxSceneFileName = gameScene.SceneFileName;
  74. IsCancelled = false;
  75. RaiseMessage("Exportation started", Color.Blue);
  76. ReportProgressChanged(0);
  77. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  78. var rawScene = Loader.Core.RootNode;
  79. if (!Directory.Exists(babylonScene.OutputPath))
  80. {
  81. RaiseError("Exportation stopped: Output folder does not exist");
  82. ReportProgressChanged(100);
  83. return;
  84. }
  85. var watch = new Stopwatch();
  86. watch.Start();
  87. // Save scene
  88. RaiseMessage("Saving 3ds max file");
  89. if (AutoSave3dsMaxFile)
  90. {
  91. var forceSave = Loader.Core.FileSave;
  92. callerForm?.BringToFront();
  93. }
  94. // Producer
  95. babylonScene.producer = new BabylonProducer
  96. {
  97. name = "3dsmax",
  98. #if MAX2017
  99. version = "2017",
  100. #else
  101. version = Loader.Core.ProductVersion.ToString(),
  102. #endif
  103. exporter_version = "0.4.5",
  104. file = Path.GetFileName(outputFile)
  105. };
  106. // Global
  107. babylonScene.autoClear = true;
  108. babylonScene.clearColor = Loader.Core.GetBackGround(0, Tools.Forever).ToArray();
  109. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Tools.Forever).ToArray();
  110. babylonScene.gravity = rawScene.GetVector3Property("babylonjs_gravity");
  111. ExportQuaternionsInsteadOfEulers = rawScene.GetBoolProperty("babylonjs_exportquaternions", 1);
  112. // Sounds
  113. var soundName = rawScene.GetStringProperty("babylonjs_sound_filename", "");
  114. if (!string.IsNullOrEmpty(soundName))
  115. {
  116. var filename = Path.GetFileName(soundName);
  117. var globalSound = new BabylonSound
  118. {
  119. autoplay = rawScene.GetBoolProperty("babylonjs_sound_autoplay", 1),
  120. loop = rawScene.GetBoolProperty("babylonjs_sound_loop", 1),
  121. name = filename
  122. };
  123. babylonScene.SoundsList.Add(globalSound);
  124. try
  125. {
  126. File.Copy(soundName, Path.Combine(babylonScene.OutputPath, filename), true);
  127. }
  128. catch
  129. {
  130. }
  131. }
  132. // Cameras
  133. BabylonCamera mainCamera = null;
  134. ICameraObject mainCameraNode = null;
  135. RaiseMessage("Exporting cameras");
  136. var camerasTab = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  137. for (int ix = 0; ix < camerasTab.Count; ++ix)
  138. {
  139. #if MAX2017
  140. var indexer = ix;
  141. #else
  142. var indexer = new IntPtr(ix);
  143. #endif
  144. var cameraNode = camerasTab[indexer];
  145. #if !MAX2017
  146. Marshal.FreeHGlobal(indexer);
  147. #endif
  148. ExportCamera(gameScene, cameraNode, babylonScene);
  149. if (mainCamera == null && babylonScene.CamerasList.Count > 0)
  150. {
  151. mainCameraNode = (cameraNode.MaxNode.ObjectRef as ICameraObject);
  152. mainCamera = babylonScene.CamerasList[0];
  153. babylonScene.activeCameraID = mainCamera.id;
  154. RaiseMessage("Active camera set to " + mainCamera.name, Color.Green, 1, true);
  155. }
  156. }
  157. if (mainCamera == null)
  158. {
  159. RaiseWarning("No camera defined", 1);
  160. }
  161. else
  162. {
  163. RaiseMessage(string.Format("Total: {0}", babylonScene.CamerasList.Count), Color.Gray, 1);
  164. }
  165. // Fog
  166. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  167. {
  168. var atmospheric = Loader.Core.GetAtmospheric(index);
  169. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  170. {
  171. var fog = atmospheric as IStdFog;
  172. RaiseMessage("Exporting fog");
  173. if (fog != null)
  174. {
  175. babylonScene.fogColor = fog.GetColor(0).ToArray();
  176. babylonScene.fogMode = 3;
  177. }
  178. #if !MAX2015 && !MAX2016 && !MAX2017
  179. else
  180. {
  181. var paramBlock = atmospheric.GetReference(0) as IIParamBlock;
  182. babylonScene.fogColor = Tools.GetParamBlockValueColor(paramBlock, "Fog Color");
  183. babylonScene.fogMode = 3;
  184. }
  185. #endif
  186. if (mainCamera != null)
  187. {
  188. babylonScene.fogStart = mainCameraNode.GetEnvRange(0, 0, Tools.Forever);
  189. babylonScene.fogEnd = mainCameraNode.GetEnvRange(0, 1, Tools.Forever);
  190. }
  191. }
  192. }
  193. // Meshes
  194. ReportProgressChanged(10);
  195. RaiseMessage("Exporting meshes");
  196. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  197. var progressionStep = 80.0f / meshes.Count;
  198. var progression = 10.0f;
  199. for (int ix = 0; ix < meshes.Count; ++ix)
  200. {
  201. #if MAX2017
  202. var indexer = ix;
  203. #else
  204. var indexer = new IntPtr(ix);
  205. #endif
  206. var meshNode = meshes[indexer];
  207. #if !MAX2017
  208. Marshal.FreeHGlobal(indexer);
  209. #endif
  210. ExportMesh(gameScene, meshNode, babylonScene);
  211. ReportProgressChanged((int)progression);
  212. progression += progressionStep;
  213. CheckCancelled();
  214. }
  215. // Materials
  216. RaiseMessage("Exporting materials");
  217. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  218. foreach (var mat in matsToExport)
  219. {
  220. ExportMaterial(mat, babylonScene);
  221. CheckCancelled();
  222. }
  223. RaiseMessage(string.Format("Total: {0}", babylonScene.MaterialsList.Count + babylonScene.MultiMaterialsList.Count), Color.Gray, 1);
  224. // Lights
  225. RaiseMessage("Exporting lights");
  226. var lightNodes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  227. for (var i = 0; i < lightNodes.Count; ++i)
  228. {
  229. #if MAX2017
  230. ExportLight(gameScene, lightNodes[i], babylonScene);
  231. #else
  232. ExportLight(gameScene, lightNodes[new IntPtr(i)], babylonScene);
  233. #endif
  234. CheckCancelled();
  235. }
  236. if (babylonScene.LightsList.Count == 0)
  237. {
  238. RaiseWarning("No light defined", 1);
  239. RaiseWarning("A default hemispheric light was added for your convenience", 1);
  240. ExportDefaultLight(babylonScene);
  241. }
  242. else
  243. {
  244. RaiseMessage(string.Format("Total: {0}", babylonScene.LightsList.Count), Color.Gray, 1);
  245. }
  246. // Skeletons
  247. if (skins.Count > 0)
  248. {
  249. RaiseMessage("Exporting skeletons");
  250. foreach (var skin in skins)
  251. {
  252. ExportSkin(skin, babylonScene);
  253. }
  254. }
  255. // Actions
  256. babylonScene.actions = ExportNodeAction(gameScene.GetIGameNode(rawScene));
  257. // Output
  258. RaiseMessage("Saving to output file");
  259. babylonScene.Prepare(false, false);
  260. var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
  261. var sb = new StringBuilder();
  262. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  263. await Task.Run(() =>
  264. {
  265. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  266. {
  267. jsonWriter.Formatting = Formatting.None;
  268. jsonSerializer.Serialize(jsonWriter, babylonScene);
  269. }
  270. File.WriteAllText(outputFile, sb.ToString());
  271. if (generateManifest)
  272. {
  273. File.WriteAllText(outputFile + ".manifest",
  274. "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
  275. }
  276. });
  277. // Binary
  278. if (generateBinary)
  279. {
  280. RaiseMessage("Generating binary files");
  281. BabylonFileConverter.BinaryConverter.Convert(outputFile, Path.GetDirectoryName(outputFile) + "\\Binary",
  282. message => RaiseMessage(message, 1),
  283. error => RaiseError(error, 1));
  284. }
  285. ReportProgressChanged(100);
  286. // Export glTF
  287. if (exportGltf)
  288. {
  289. ExportGltf(babylonScene, outputFile, generateBinary);
  290. }
  291. watch.Stop();
  292. RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
  293. }
  294. }
  295. }