BabylonExporter.cs 8.1 KB

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