BabylonExporter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Serialization.Json;
  6. using Autodesk.Max;
  7. using BabylonExport.Entities;
  8. using MaxSharp;
  9. using Animatable = MaxSharp.Animatable;
  10. namespace Max2Babylon
  11. {
  12. internal partial class BabylonExporter
  13. {
  14. public event Action<int> OnImportProgressChanged;
  15. public event Action<string, bool> OnWarning;
  16. public event Action<string, bool, bool, bool> OnMessage;
  17. public event Action<string, bool> OnError;
  18. readonly List<string> alreadyExportedTextures = new List<string>();
  19. void ReportProgressChanged(int progress)
  20. {
  21. if (OnImportProgressChanged != null)
  22. {
  23. OnImportProgressChanged(progress);
  24. }
  25. }
  26. void RaiseError(string error, bool asChild = false)
  27. {
  28. if (OnError != null)
  29. {
  30. OnError(error, asChild);
  31. }
  32. }
  33. void RaiseWarning(string warning, bool asChild = false)
  34. {
  35. if (OnWarning != null)
  36. {
  37. OnWarning(warning, asChild);
  38. }
  39. }
  40. void RaiseMessage(string message, bool asChild = false, bool emphasis = false, bool embed = false)
  41. {
  42. if (OnMessage != null)
  43. {
  44. OnMessage(message, asChild, emphasis, embed);
  45. }
  46. }
  47. public void Export(string outputFile)
  48. {
  49. RaiseMessage("Exportation started");
  50. ReportProgressChanged(25);
  51. var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
  52. var maxScene = Kernel.Scene;
  53. alreadyExportedTextures.Clear();
  54. if (!Directory.Exists(babylonScene.OutputPath))
  55. {
  56. RaiseError("Exportation stopped: Output folder does not exist");
  57. ReportProgressChanged(100);
  58. return;
  59. }
  60. // Global
  61. babylonScene.autoClear = true;
  62. babylonScene.clearColor = Loader.Core.GetBackGround(0, Interval.Forever._IInterval).ToArray();
  63. babylonScene.ambientColor = Loader.Core.GetAmbient(0, Interval.Forever._IInterval).ToArray();
  64. babylonScene.gravity = maxScene.RootNode._Node.GetVector3Property("babylonjs_gravity");
  65. // Cameras
  66. BabylonCamera mainCamera = null;
  67. RaiseMessage("Exporting cameras");
  68. foreach (var cameraNode in maxScene.NodesListBySuperClass(SuperClassID.Camera))
  69. {
  70. var babylonCamera = ExportCamera(cameraNode, babylonScene);
  71. if (mainCamera == null)
  72. {
  73. mainCamera = babylonCamera;
  74. babylonScene.activeCameraID = mainCamera.id;
  75. RaiseMessage("Active camera set to " + mainCamera.name, true, true);
  76. }
  77. }
  78. if (mainCamera == null)
  79. {
  80. RaiseWarning("No camera defined", true);
  81. }
  82. // Fog
  83. for (var index = 0; index < Loader.Core.NumAtmospheric; index++)
  84. {
  85. var atmospheric = Loader.Core.GetAtmospheric(index);
  86. if (atmospheric.Active(0) && atmospheric.ClassName == "Fog")
  87. {
  88. RaiseMessage("Exporting fog");
  89. var reference = atmospheric.GetReference(0);
  90. var parameters = Animatable.CreateWrapper<ParameterBlock1>(reference);
  91. babylonScene.fogColor = (parameters["fog color"].Value as IColor).ToArray();
  92. babylonScene.fogDensity = (float)parameters["density"].Value;
  93. babylonScene.fogMode = ((int)parameters["fog type"].Value) == 0 ? 3 : 1;
  94. if (mainCamera != null)
  95. {
  96. babylonScene.fogStart = mainCamera.minZ * (float)parameters["near %"].Value;
  97. babylonScene.fogEnd = mainCamera.maxZ * (float)parameters["far %"].Value;
  98. }
  99. }
  100. }
  101. // Meshes
  102. RaiseMessage("Exporting meshes");
  103. foreach (var meshNode in maxScene.NodesListBySuperClass(SuperClassID.GeometricObject))
  104. {
  105. ExportMesh(meshNode, babylonScene);
  106. }
  107. // Materials
  108. RaiseMessage("Exporting materials");
  109. var matsToExport = referencedMaterials.ToArray(); // Snapshot because multimaterials can export new materials
  110. foreach (var mat in matsToExport)
  111. {
  112. ExportMaterial(mat, babylonScene);
  113. }
  114. // Lights
  115. RaiseMessage("Exporting lights");
  116. foreach (var lightNode in maxScene.NodesListBySuperClass(SuperClassID.Light))
  117. {
  118. ExportLight(lightNode, babylonScene);
  119. }
  120. // Output
  121. babylonScene.Prepare(false);
  122. using (var outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
  123. {
  124. var ser = new DataContractJsonSerializer(typeof(BabylonScene));
  125. ser.WriteObject(outputStream, babylonScene);
  126. }
  127. ReportProgressChanged(100);
  128. RaiseMessage("Exportation done");
  129. }
  130. }
  131. }