Program.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace MakeIncremental
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. if (args.Length < 1)
  13. {
  14. DisplayUsage();
  15. return;
  16. }
  17. // Parsing arguments
  18. string input = "";
  19. bool extractTextures = false;
  20. foreach (var arg in args)
  21. {
  22. var order = arg.Substring(0, 3);
  23. switch (order)
  24. {
  25. case "/i:":
  26. input = arg.Substring(3);
  27. break;
  28. case "/textures":
  29. extractTextures = true;
  30. break;
  31. default:
  32. DisplayUsage();
  33. return;
  34. }
  35. }
  36. if (string.IsNullOrEmpty(input))
  37. {
  38. DisplayUsage();
  39. return;
  40. }
  41. ProcessSourceFile(input, extractTextures);
  42. }
  43. static string CreateDelayLoadingFile(dynamic mesh, string outputDir, string rootFilename)
  44. {
  45. var outputPath = Path.Combine(outputDir, rootFilename + "." + mesh.name + ".babylonmeshdata");
  46. var result = new JObject();
  47. result["positions"] = mesh.positions;
  48. result["indices"] = mesh.indices;
  49. result["normals"] = mesh.normals;
  50. if (mesh.uvs != null)
  51. {
  52. result["uvs"] = mesh.uvs;
  53. }
  54. if (mesh.uvs2 != null)
  55. {
  56. result["uvs2"] = mesh.uvs2;
  57. }
  58. if (mesh.colors != null)
  59. {
  60. result["colors"] = mesh.colors;
  61. }
  62. if (mesh.matricesIndices != null)
  63. {
  64. result["matricesIndices"] = mesh.matricesIndices;
  65. }
  66. if (mesh.matricesWeights != null)
  67. {
  68. result["matricesWeights"] = mesh.matricesWeights;
  69. }
  70. if (mesh.subMeshes != null)
  71. {
  72. result["subMeshes"] = mesh.subMeshes;
  73. }
  74. string json = result.ToString(Formatting.None);
  75. using (var writer = new StreamWriter(outputPath))
  76. {
  77. writer.Write(json);
  78. }
  79. return Path.GetFileName(outputPath);
  80. }
  81. static void ProcessSourceFile(string input, bool extractTextures)
  82. {
  83. try
  84. {
  85. dynamic scene;
  86. var outputDir = Path.GetDirectoryName(input);
  87. var rootFilename = Path.GetFileNameWithoutExtension(input);
  88. // Loading
  89. Console.ForegroundColor = ConsoleColor.Green;
  90. Console.WriteLine("Loading " + input);
  91. Console.WriteLine();
  92. Console.ResetColor();
  93. using (var streamReader = new StreamReader(input))
  94. {
  95. using (var reader = new JsonTextReader(streamReader))
  96. {
  97. scene = JObject.Load(reader);
  98. }
  99. }
  100. // Marking scene
  101. scene["autoClear"] = true;
  102. scene["useDelayedTextureLoading"] = true;
  103. // Parsing meshes
  104. var meshes = (JArray)scene.meshes;
  105. foreach (dynamic mesh in meshes)
  106. {
  107. if (mesh.checkCollisions.Value) // Do not delay load collisions object
  108. {
  109. continue;
  110. }
  111. Console.WriteLine("Extracting " + mesh.name);
  112. if (mesh.positions != null && mesh.normals != null && mesh.indices != null)
  113. {
  114. mesh.delayLoadingFile = CreateDelayLoadingFile(mesh, outputDir, rootFilename);
  115. // Compute bounding boxes
  116. var positions = ((JArray) mesh.positions).Select(v=>v.Value<float>()).ToArray();
  117. var minimum = new[] {float.MaxValue, float.MaxValue, float.MaxValue};
  118. var maximum = new[] {float.MinValue, float.MinValue, float.MinValue};
  119. for (var index = 0; index < positions.Length; index += 3)
  120. {
  121. var x = positions[index];
  122. var y = positions[index + 1];
  123. var z = positions[index + 2];
  124. if (x < minimum[0])
  125. {
  126. minimum[0] = x;
  127. }
  128. if (x > maximum[0])
  129. {
  130. maximum[0] = x;
  131. }
  132. if (y < minimum[1])
  133. {
  134. minimum[1] = y;
  135. }
  136. if (y > maximum[1])
  137. {
  138. maximum[1] = y;
  139. }
  140. if (z < minimum[2])
  141. {
  142. minimum[2] = z;
  143. }
  144. if (z > maximum[2])
  145. {
  146. maximum[2] = z;
  147. }
  148. }
  149. mesh["boundingBoxMinimum"] = new JArray(minimum);
  150. mesh["boundingBoxMaximum"] = new JArray(maximum);
  151. // Erasing infos
  152. mesh.positions = null;
  153. mesh.normals = null;
  154. mesh.indices = null;
  155. if (mesh.uvs != null)
  156. {
  157. mesh["hasUVs"] = true;
  158. mesh.uvs = null;
  159. }
  160. if (mesh.uvs2 != null)
  161. {
  162. mesh["hasUVs2"] = true;
  163. mesh.uvs2 = null;
  164. }
  165. if (mesh.colors != null)
  166. {
  167. mesh["hasColors"] = true;
  168. mesh.colors = null;
  169. }
  170. if (mesh.matricesIndices != null)
  171. {
  172. mesh["hasMatricesIndices"] = true;
  173. mesh.matricesIndices = null;
  174. }
  175. if (mesh.matricesWeights != null)
  176. {
  177. mesh["hasMatricesWeights"] = true;
  178. mesh.matricesWeights = null;
  179. }
  180. if (mesh.subMeshes != null)
  181. {
  182. mesh.subMeshes = null;
  183. }
  184. }
  185. }
  186. // Saving
  187. var outputPath = Path.Combine(outputDir, rootFilename + ".incremental.babylon");
  188. Console.ForegroundColor = ConsoleColor.Green;
  189. Console.WriteLine("Saving " + outputPath);
  190. string json = scene.ToString(Formatting.None);
  191. using (var writer = new StreamWriter(outputPath))
  192. {
  193. writer.Write(json);
  194. }
  195. Console.WriteLine();
  196. Console.ResetColor();
  197. }
  198. catch (Exception ex)
  199. {
  200. Console.ForegroundColor = ConsoleColor.Red;
  201. Console.WriteLine("Fatal error encountered:");
  202. Console.WriteLine(ex.Message);
  203. Console.ResetColor();
  204. }
  205. }
  206. static void DisplayUsage()
  207. {
  208. Console.WriteLine("MakeIncremental usage: MakeIncremental.exe /i:\"source file\" [/textures]");
  209. }
  210. }
  211. }