BabylonExporter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using Autodesk.Max;
  10. using BabylonExport.Entities;
  11. using Newtonsoft.Json;
  12. using Color = System.Drawing.Color;
  13. using System.Runtime.InteropServices;
  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, 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. // Cameras
  135. BabylonCamera mainCamera = null;
  136. ICameraObject mainCameraNode = null;
  137. RaiseMessage("Exporting cameras");
  138. var camerasTab = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  139. for (int ix = 0; ix < camerasTab.Count; ++ix)
  140. {
  141. #if MAX2017
  142. var indexer = ix;
  143. #else
  144. var indexer = new IntPtr(ix);
  145. #endif
  146. var cameraNode = camerasTab[indexer];
  147. #if !MAX2017
  148. Marshal.FreeHGlobal(indexer);
  149. #endif
  150. ExportCamera(gameScene, cameraNode, babylonScene);
  151. if (mainCamera == null && babylonScene.CamerasList.Count > 0)
  152. {
  153. mainCameraNode = (cameraNode.MaxNode.ObjectRef as ICameraObject);
  154. mainCamera = babylonScene.CamerasList[0];
  155. babylonScene.activeCameraID = mainCamera.id;
  156. RaiseMessage("Active camera set to " + mainCamera.name, Color.Green, 1, true);
  157. }
  158. }
  159. if (mainCamera == null)
  160. {
  161. RaiseWarning("No camera defined", 1);
  162. }
  163. else
  164. {
  165. RaiseMessage(string.Format("Total: {0}", babylonScene.CamerasList.Count), Color.Gray, 1);
  166. }
  167. // Fog
  168. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  169. {
  170. var atmospheric = Loader.Core.GetAtmospheric(index);
  171. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  172. {
  173. var fog = atmospheric as IStdFog;
  174. RaiseMessage("Exporting fog");
  175. if (fog != null)
  176. {
  177. babylonScene.fogColor = fog.GetColor(0).ToArray();
  178. babylonScene.fogMode = 3;
  179. }
  180. #if !MAX2015 && !MAX2016 && !MAX2017
  181. else
  182. {
  183. var paramBlock = atmospheric.GetReference(0) as IIParamBlock;
  184. babylonScene.fogColor = Tools.GetParamBlockValueColor(paramBlock, "Fog Color");
  185. babylonScene.fogMode = 3;
  186. }
  187. #endif
  188. if (mainCamera != null)
  189. {
  190. babylonScene.fogStart = mainCameraNode.GetEnvRange(0, 0, Tools.Forever);
  191. babylonScene.fogEnd = mainCameraNode.GetEnvRange(0, 1, Tools.Forever);
  192. }
  193. }
  194. }
  195. // Meshes
  196. ReportProgressChanged(10);
  197. RaiseMessage("Exporting meshes");
  198. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  199. var progressionStep = 80.0f / meshes.Count;
  200. var progression = 10.0f;
  201. for (int ix = 0; ix < meshes.Count; ++ix)
  202. {
  203. #if MAX2017
  204. var indexer = ix;
  205. #else
  206. var indexer = new IntPtr(ix);
  207. #endif
  208. var meshNode = meshes[indexer];
  209. #if !MAX2017
  210. Marshal.FreeHGlobal(indexer);
  211. #endif
  212. ExportMesh(gameScene, meshNode, babylonScene);
  213. ReportProgressChanged((int)progression);
  214. progression += progressionStep;
  215. CheckCancelled();
  216. }
  217. // Materials
  218. RaiseMessage("Exporting materials");
  219. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  220. foreach (var mat in matsToExport)
  221. {
  222. ExportMaterial(mat, babylonScene);
  223. CheckCancelled();
  224. }
  225. RaiseMessage(string.Format("Total: {0}", babylonScene.MaterialsList.Count + babylonScene.MultiMaterialsList.Count), Color.Gray, 1);
  226. // Lights
  227. RaiseMessage("Exporting lights");
  228. var lightNodes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  229. for (var i = 0; i < lightNodes.Count; ++i)
  230. {
  231. #if MAX2017
  232. ExportLight(gameScene, lightNodes[i], babylonScene);
  233. #else
  234. ExportLight(gameScene, lightNodes[new IntPtr(i)], babylonScene);
  235. #endif
  236. CheckCancelled();
  237. }
  238. if (babylonScene.LightsList.Count == 0)
  239. {
  240. RaiseWarning("No light defined", 1);
  241. RaiseWarning("A default hemispheric light was added for your convenience", 1);
  242. ExportDefaultLight(babylonScene);
  243. }
  244. else
  245. {
  246. RaiseMessage(string.Format("Total: {0}", babylonScene.LightsList.Count), Color.Gray, 1);
  247. }
  248. // Skeletons
  249. if (skins.Count > 0)
  250. {
  251. RaiseMessage("Exporting skeletons");
  252. foreach (var skin in skins)
  253. {
  254. ExportSkin(skin, babylonScene);
  255. }
  256. }
  257. // Actions
  258. babylonScene.actions = ExportNodeAction(gameScene.GetIGameNode(rawScene));
  259. // Output
  260. RaiseMessage("Saving to output file");
  261. babylonScene.Prepare(false);
  262. var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
  263. var sb = new StringBuilder();
  264. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  265. await Task.Run(() =>
  266. {
  267. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  268. {
  269. jsonWriter.Formatting = Formatting.None;
  270. jsonSerializer.Serialize(jsonWriter, babylonScene);
  271. }
  272. File.WriteAllText(outputFile, sb.ToString());
  273. if (generateManifest)
  274. {
  275. File.WriteAllText(outputFile + ".manifest",
  276. "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
  277. }
  278. });
  279. // Binary
  280. if (generateBinary)
  281. {
  282. RaiseMessage("Generating binary files");
  283. BabylonFileConverter.BinaryConverter.Convert(outputFile, Path.GetDirectoryName(outputFile) + "\\Binary",
  284. message => RaiseMessage(message, 1),
  285. error => RaiseError(error, 1));
  286. }
  287. ReportProgressChanged(100);
  288. watch.Stop();
  289. RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
  290. }
  291. }
  292. }