BabylonExporter.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Threading;
  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, bool> OnWarning;
  20. public event Action<string, bool, bool, bool, Color> OnMessage;
  21. public event Action<string, bool> OnError;
  22. readonly List<string> alreadyExportedTextures = new List<string>();
  23. void ReportProgressChanged(int progress)
  24. {
  25. if (OnImportProgressChanged != null)
  26. {
  27. OnImportProgressChanged(progress);
  28. }
  29. }
  30. void RaiseError(string error, bool asChild = true)
  31. {
  32. if (OnError != null)
  33. {
  34. OnError(error, asChild);
  35. }
  36. }
  37. void RaiseWarning(string warning, bool asChild = false)
  38. {
  39. if (OnWarning != null)
  40. {
  41. OnWarning(warning, asChild);
  42. }
  43. }
  44. void RaiseMessage(string message, bool asChild = false, bool emphasis = false, bool embed = false)
  45. {
  46. RaiseMessage(message, Color.Black, asChild, emphasis, embed);
  47. }
  48. void RaiseMessage(string message, Color color, bool asChild = false, bool emphasis = false, bool embed = false)
  49. {
  50. if (OnMessage != null)
  51. {
  52. OnMessage(message, asChild, emphasis, embed, color);
  53. }
  54. }
  55. public void Export(string outputFile, CancellationToken token)
  56. {
  57. RaiseMessage("Exportation started");
  58. ReportProgressChanged(0);
  59. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  60. var maxScene = Kernel.Scene;
  61. alreadyExportedTextures.Clear();
  62. if (!Directory.Exists(babylonScene.OutputPath))
  63. {
  64. RaiseError("Exportation stopped: Output folder does not exist");
  65. ReportProgressChanged(100);
  66. return;
  67. }
  68. // Global
  69. babylonScene.autoClear = true;
  70. babylonScene.clearColor = Loader.Core.GetBackGround(0, Interval.Forever._IInterval).ToArray();
  71. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Interval.Forever._IInterval).ToArray();
  72. babylonScene.gravity = maxScene.RootNode._Node.GetVector3Property("babylonjs_gravity");
  73. // Cameras
  74. BabylonCamera mainCamera = null;
  75. RaiseMessage("Exporting cameras");
  76. foreach (var cameraNode in maxScene.NodesListBySuperClass(SuperClassID.Camera))
  77. {
  78. ExportCamera(cameraNode, babylonScene);
  79. if (mainCamera == null && babylonScene.CamerasList.Count > 0)
  80. {
  81. mainCamera = babylonScene.CamerasList[0];
  82. babylonScene.activeCameraID = mainCamera.id;
  83. RaiseMessage("Active camera set to " + mainCamera.name, true, true);
  84. }
  85. }
  86. if (mainCamera == null)
  87. {
  88. RaiseWarning("No camera defined", true);
  89. }
  90. // Fog
  91. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  92. {
  93. var atmospheric = Loader.Core.GetAtmospheric(index);
  94. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  95. {
  96. RaiseMessage("Exporting fog");
  97. var reference = atmospheric.GetReference(0);
  98. var parameters = Animatable.CreateWrapper<ParameterBlock1>(reference);
  99. babylonScene.fogColor = (parameters["fog color"].Value as IColor).ToArray();
  100. babylonScene.fogDensity = (float)parameters["density"].Value;
  101. babylonScene.fogMode = ((int)parameters["fog type"].Value) == 0 ? 3 : 1;
  102. if (mainCamera != null)
  103. {
  104. babylonScene.fogStart = mainCamera.minZ * (float)parameters["near %"].Value;
  105. babylonScene.fogEnd = mainCamera.maxZ * (float)parameters["far %"].Value;
  106. }
  107. }
  108. }
  109. // Meshes
  110. ReportProgressChanged(10);
  111. RaiseMessage("Exporting meshes");
  112. var meshes = maxScene.NodesListBySuperClasses(new[] {SuperClassID.GeometricObject, SuperClassID.Helper});
  113. var progressionStep = 80.0f / meshes.Count();
  114. var progression = 10.0f;
  115. foreach (var meshNode in meshes)
  116. {
  117. Tools.PreparePipeline(meshNode._Node, true);
  118. ExportMesh(meshNode, babylonScene, token);
  119. Tools.PreparePipeline(meshNode._Node, false);
  120. progression += progressionStep;
  121. ReportProgressChanged((int)progression);
  122. if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
  123. }
  124. // Materials
  125. RaiseMessage("Exporting materials");
  126. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  127. foreach (var mat in matsToExport)
  128. {
  129. ExportMaterial(mat, babylonScene);
  130. if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
  131. }
  132. // Lights
  133. RaiseMessage("Exporting lights");
  134. foreach (var lightNode in maxScene.NodesListBySuperClass(SuperClassID.Light))
  135. {
  136. ExportLight(lightNode, babylonScene);
  137. if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
  138. }
  139. if (babylonScene.LightsList.Count == 0)
  140. {
  141. RaiseWarning("No light defined", true);
  142. }
  143. // Output
  144. babylonScene.Prepare(false);
  145. var jsonSerializer = JsonSerializer.Create();
  146. var sb = new StringBuilder();
  147. var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
  148. using (var jsonWriter = new JsonTextWriterOptimized(sw))
  149. {
  150. jsonWriter.Formatting = Formatting.None;
  151. jsonSerializer.Serialize(jsonWriter, babylonScene);
  152. }
  153. File.WriteAllText(outputFile, sb.ToString());
  154. ReportProgressChanged(100);
  155. RaiseMessage("Exportation done");
  156. }
  157. }
  158. }