Program.cs 8.0 KB

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