BabylonExporter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Autodesk.Max;
  11. using BabylonExport.Entities;
  12. using Newtonsoft.Json;
  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. 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. private bool exportQuaternionsInsteadOfEulers;
  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, Form callerForm)
  69. {
  70. IsCancelled = false;
  71. RaiseMessage("Exportation started", Color.Blue);
  72. ReportProgressChanged(0);
  73. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  74. var maxScene = Loader.Core.RootNode;
  75. alreadyExportedTextures.Clear();
  76. if (!Directory.Exists(babylonScene.OutputPath))
  77. {
  78. RaiseError("Exportation stopped: Output folder does not exist");
  79. ReportProgressChanged(100);
  80. return;
  81. }
  82. var watch = new Stopwatch();
  83. watch.Start();
  84. // Save scene
  85. RaiseMessage("Saving 3ds max file");
  86. if (AutoSave3dsMaxFile)
  87. {
  88. var forceSave = Loader.Core.FileSave;
  89. if (callerForm != null)
  90. {
  91. callerForm.BringToFront();
  92. }
  93. }
  94. // Global
  95. babylonScene.autoClear = true;
  96. babylonScene.clearColor = Loader.Core.GetBackGround(0, Tools.Forever).ToArray();
  97. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Tools.Forever).ToArray();
  98. babylonScene.gravity = maxScene.GetVector3Property("babylonjs_gravity");
  99. exportQuaternionsInsteadOfEulers = maxScene.GetBoolProperty("babylonjs_exportquaternions", 1);
  100. // Cameras
  101. BabylonCamera mainCamera = null;
  102. RaiseMessage("Exporting cameras");
  103. foreach (var cameraNode in maxScene.NodesListBySuperClass(SClass_ID.Camera))
  104. {
  105. ExportCamera(cameraNode, babylonScene);
  106. if (mainCamera == null && babylonScene.CamerasList.Count > 0)
  107. {
  108. mainCamera = babylonScene.CamerasList[0];
  109. babylonScene.activeCameraID = mainCamera.id;
  110. RaiseMessage("Active camera set to " + mainCamera.name, Color.Green, 1, true);
  111. }
  112. }
  113. if (mainCamera == null)
  114. {
  115. RaiseWarning("No camera defined", 1);
  116. }
  117. else
  118. {
  119. RaiseMessage(string.Format("Total: {0}", babylonScene.CamerasList.Count), Color.Gray, 1);
  120. }
  121. // Fog
  122. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  123. {
  124. var atmospheric = Loader.Core.GetAtmospheric(index);
  125. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  126. {
  127. var fog = atmospheric as IStdFog;
  128. if (fog != null)
  129. {
  130. RaiseMessage("Exporting fog");
  131. babylonScene.fogColor = fog.GetColor(0).ToArray();
  132. babylonScene.fogDensity = fog.GetDensity(0);
  133. babylonScene.fogMode = fog.GetType_ == 0 ? 3 : 1;
  134. if (mainCamera != null)
  135. {
  136. babylonScene.fogStart = mainCamera.minZ*fog.GetNear(0);
  137. babylonScene.fogEnd = mainCamera.maxZ*fog.GetFar(0);
  138. }
  139. }
  140. }
  141. }
  142. // Meshes
  143. ReportProgressChanged(10);
  144. RaiseMessage("Exporting meshes");
  145. var meshes = maxScene.NodesListBySuperClasses(new[] { SClass_ID.Geomobject, SClass_ID.Helper });
  146. var progressionStep = 80.0f / meshes.Count();
  147. var progression = 10.0f;
  148. foreach (var meshNode in meshes)
  149. {
  150. Tools.PreparePipeline(meshNode, true);
  151. ExportMesh(meshNode, babylonScene);
  152. Tools.PreparePipeline(meshNode, false);
  153. progression += progressionStep;
  154. ReportProgressChanged((int)progression);
  155. CheckCancelled();
  156. }
  157. RaiseMessage(string.Format("Total: {0}", babylonScene.MeshesList.Count), Color.Gray, 1);
  158. // Materials
  159. RaiseMessage("Exporting materials");
  160. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  161. foreach (var mat in matsToExport)
  162. {
  163. ExportMaterial(mat, babylonScene);
  164. CheckCancelled();
  165. }
  166. RaiseMessage(string.Format("Total: {0}", babylonScene.MaterialsList.Count + babylonScene.MultiMaterialsList.Count), Color.Gray, 1);
  167. // Lights
  168. RaiseMessage("Exporting lights");
  169. foreach (var lightNode in maxScene.NodesListBySuperClass(SClass_ID.Light))
  170. {
  171. ExportLight(lightNode, babylonScene);
  172. CheckCancelled();
  173. }
  174. if (babylonScene.LightsList.Count == 0)
  175. {
  176. RaiseWarning("No light defined", 1);
  177. }
  178. else
  179. {
  180. RaiseMessage(string.Format("Total: {0}", babylonScene.LightsList.Count), Color.Gray, 1);
  181. }
  182. // Skeletons
  183. if (skins.Count > 0)
  184. {
  185. RaiseMessage("Exporting skeletons");
  186. foreach (var skin in skins)
  187. {
  188. ExportSkin(skin, babylonScene);
  189. skin.Dispose();
  190. }
  191. }
  192. // Output
  193. RaiseMessage("Saving to output file");
  194. babylonScene.Prepare(false);
  195. var jsonSerializer = JsonSerializer.Create();
  196. var sb = new StringBuilder();
  197. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  198. await Task.Run(() =>
  199. {
  200. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  201. {
  202. jsonWriter.Formatting = Formatting.None;
  203. jsonSerializer.Serialize(jsonWriter, babylonScene);
  204. }
  205. File.WriteAllText(outputFile, sb.ToString());
  206. if (generateManifest)
  207. {
  208. File.WriteAllText(outputFile + ".manifest",
  209. "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
  210. }
  211. });
  212. ReportProgressChanged(100);
  213. watch.Stop();
  214. RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
  215. }
  216. }
  217. }