Program.cs 9.2 KB

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