BabylonExporter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. readonly List<string> alreadyExportedTextures = new List<string>();
  23. public bool AutoSave3dsMaxFile { get; set; }
  24. public bool ExportHiddenObjects { get; set; }
  25. public bool IsCancelled { get; set; }
  26. public bool CopyTexturesToOutput { get; set; }
  27. public string MaxSceneFileName { get; set; }
  28. public bool ExportQuaternionsInsteadOfEulers { get; set; }
  29. void ReportProgressChanged(int progress)
  30. {
  31. if (OnImportProgressChanged != null)
  32. {
  33. OnImportProgressChanged(progress);
  34. }
  35. }
  36. void RaiseError(string error, int rank = 0)
  37. {
  38. if (OnError != null)
  39. {
  40. OnError(error, rank);
  41. }
  42. }
  43. void RaiseWarning(string warning, int rank = 0)
  44. {
  45. if (OnWarning != null)
  46. {
  47. OnWarning(warning, rank);
  48. }
  49. }
  50. void RaiseMessage(string message, int rank = 0, bool emphasis = false)
  51. {
  52. RaiseMessage(message, Color.Black, rank, emphasis);
  53. }
  54. void RaiseMessage(string message, Color color, int rank = 0, bool emphasis = false)
  55. {
  56. if (OnMessage != null)
  57. {
  58. OnMessage(message, color, rank, emphasis);
  59. }
  60. }
  61. void CheckCancelled()
  62. {
  63. Application.DoEvents();
  64. if (IsCancelled)
  65. {
  66. throw new OperationCanceledException();
  67. }
  68. }
  69. public async Task ExportAsync(string outputFile, bool generateManifest, bool onlySelected, bool generateBinary, Form callerForm)
  70. {
  71. var gameConversionManger = Loader.Global.ConversionManager;
  72. gameConversionManger.CoordSystem = Autodesk.Max.IGameConversionManager.CoordSystem.D3d;
  73. var gameScene = Loader.Global.IGameInterface;
  74. gameScene.InitialiseIGame(onlySelected);
  75. gameScene.SetStaticFrame(0);
  76. MaxSceneFileName = gameScene.SceneFileName;
  77. IsCancelled = false;
  78. RaiseMessage("Exportation started", Color.Blue);
  79. ReportProgressChanged(0);
  80. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  81. var rawScene = Loader.Core.RootNode;
  82. alreadyExportedTextures.Clear();
  83. if (!Directory.Exists(babylonScene.OutputPath))
  84. {
  85. RaiseError("Exportation stopped: Output folder does not exist");
  86. ReportProgressChanged(100);
  87. return;
  88. }
  89. var watch = new Stopwatch();
  90. watch.Start();
  91. // Save scene
  92. RaiseMessage("Saving 3ds max file");
  93. if (AutoSave3dsMaxFile)
  94. {
  95. var forceSave = Loader.Core.FileSave;
  96. if (callerForm != null)
  97. {
  98. callerForm.BringToFront();
  99. }
  100. }
  101. // Global
  102. babylonScene.autoClear = true;
  103. babylonScene.clearColor = Loader.Core.GetBackGround(0, Tools.Forever).ToArray();
  104. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Tools.Forever).ToArray();
  105. babylonScene.gravity = rawScene.GetVector3Property("babylonjs_gravity");
  106. ExportQuaternionsInsteadOfEulers = rawScene.GetBoolProperty("babylonjs_exportquaternions", 1);
  107. // Cameras
  108. BabylonCamera mainCamera = null;
  109. ICameraObject mainCameraNode = null;
  110. RaiseMessage("Exporting cameras");
  111. var camerasTab = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  112. for (int ix = 0; ix < camerasTab.Count; ++ix)
  113. {
  114. var indexer = new IntPtr(ix);
  115. var cameraNode = camerasTab[indexer];
  116. Marshal.FreeHGlobal(indexer);
  117. ExportCamera(gameScene, cameraNode, babylonScene);
  118. if (mainCamera == null && babylonScene.CamerasList.Count > 0)
  119. {
  120. mainCameraNode = (cameraNode.MaxNode.ObjectRef as ICameraObject);
  121. mainCamera = babylonScene.CamerasList[0];
  122. babylonScene.activeCameraID = mainCamera.id;
  123. RaiseMessage("Active camera set to " + mainCamera.name, Color.Green, 1, true);
  124. }
  125. }
  126. if (mainCamera == null)
  127. {
  128. RaiseWarning("No camera defined", 1);
  129. }
  130. else
  131. {
  132. RaiseMessage(string.Format("Total: {0}", babylonScene.CamerasList.Count), Color.Gray, 1);
  133. }
  134. // Fog
  135. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  136. {
  137. var atmospheric = Loader.Core.GetAtmospheric(index);
  138. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  139. {
  140. var fog = atmospheric as IStdFog;
  141. RaiseMessage("Exporting fog");
  142. if (fog != null)
  143. {
  144. babylonScene.fogColor = fog.GetColor(0).ToArray();
  145. babylonScene.fogMode = 3;
  146. }
  147. #if !MAX2015
  148. else
  149. {
  150. var paramBlock = atmospheric.GetReference(0) as IIParamBlock;
  151. babylonScene.fogColor = Tools.GetParamBlockValueColor(paramBlock, "Fog Color");
  152. babylonScene.fogMode = 3;
  153. }
  154. #endif
  155. if (mainCamera != null)
  156. {
  157. babylonScene.fogStart = mainCameraNode.GetEnvRange(0, 0, Tools.Forever);
  158. babylonScene.fogEnd = mainCameraNode.GetEnvRange(0, 1, Tools.Forever);
  159. }
  160. }
  161. }
  162. // Meshes
  163. ReportProgressChanged(10);
  164. RaiseMessage("Exporting meshes");
  165. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  166. var progressionStep = 80.0f / meshes.Count;
  167. var progression = 10.0f;
  168. for (int ix = 0; ix < meshes.Count; ++ix)
  169. {
  170. var indexer = new IntPtr(ix);
  171. var meshNode = meshes[indexer];
  172. Marshal.FreeHGlobal(indexer);
  173. ExportMesh(gameScene, meshNode, babylonScene);
  174. ReportProgressChanged((int)progression);
  175. progression += progressionStep;
  176. CheckCancelled();
  177. }
  178. // Materials
  179. RaiseMessage("Exporting materials");
  180. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  181. foreach (var mat in matsToExport)
  182. {
  183. ExportMaterial(mat, babylonScene);
  184. CheckCancelled();
  185. }
  186. RaiseMessage(string.Format("Total: {0}", babylonScene.MaterialsList.Count + babylonScene.MultiMaterialsList.Count), Color.Gray, 1);
  187. // Lights
  188. RaiseMessage("Exporting lights");
  189. var lightNodes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  190. for (var i = 0; i < lightNodes.Count; ++i)
  191. {
  192. ExportLight(gameScene, lightNodes[new IntPtr(i)], babylonScene);
  193. CheckCancelled();
  194. }
  195. if (babylonScene.LightsList.Count == 0)
  196. {
  197. RaiseWarning("No light defined", 1);
  198. RaiseWarning("A default hemispheric light was added for your convenience", 1);
  199. ExportDefaultLight(babylonScene);
  200. }
  201. else
  202. {
  203. RaiseMessage(string.Format("Total: {0}", babylonScene.LightsList.Count), Color.Gray, 1);
  204. }
  205. // Skeletons
  206. if (skins.Count > 0)
  207. {
  208. RaiseMessage("Exporting skeletons");
  209. foreach (var skin in skins)
  210. {
  211. ExportSkin(skin, babylonScene);
  212. }
  213. }
  214. // Output
  215. RaiseMessage("Saving to output file");
  216. babylonScene.Prepare(false);
  217. var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
  218. var sb = new StringBuilder();
  219. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  220. await Task.Run(() =>
  221. {
  222. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  223. {
  224. jsonWriter.Formatting = Formatting.None;
  225. jsonSerializer.Serialize(jsonWriter, babylonScene);
  226. }
  227. File.WriteAllText(outputFile, sb.ToString());
  228. if (generateManifest)
  229. {
  230. File.WriteAllText(outputFile + ".manifest",
  231. "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
  232. }
  233. });
  234. // Binary
  235. if (generateBinary)
  236. {
  237. RaiseMessage("Generating binary files");
  238. BabylonFileConverter.BinaryConverter.Convert(outputFile, Path.GetDirectoryName(outputFile) + "\\Binary",
  239. message => RaiseMessage(message, 1),
  240. error => RaiseError(error, 1));
  241. }
  242. ReportProgressChanged(100);
  243. watch.Stop();
  244. RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
  245. }
  246. }
  247. }