MeshSaver.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Text;
  13. using System.Threading;
  14. using UnityEngine;
  15. namespace NRKernal.NRExamples
  16. {
  17. /// <summary> A mesh info processor to save unity mesh to obj file. </summary>
  18. public class MeshSaver : MonoBehaviour, IMeshInfoProcessor
  19. {
  20. protected string SavePath
  21. {
  22. get
  23. {
  24. string folder;
  25. #if UNITY_EDITOR
  26. folder = Directory.GetCurrentDirectory();
  27. #else
  28. folder = Application.persistentDataPath;
  29. #endif
  30. return Path.Combine(folder, "MeshSave");
  31. }
  32. }
  33. Dictionary<ulong, Mesh> m_MeshDict = new Dictionary<ulong, Mesh>();
  34. int m_SubFolderIndex = 0;
  35. Thread m_SaveThread;
  36. void Awake()
  37. {
  38. DirectoryInfo directoryInfo = new DirectoryInfo(SavePath);
  39. if (!directoryInfo.Exists)
  40. {
  41. directoryInfo.Create();
  42. }
  43. else
  44. {
  45. while (m_SubFolderIndex < int.MaxValue)
  46. {
  47. string subFolder = Path.Combine(SavePath, m_SubFolderIndex.ToString());
  48. directoryInfo = new DirectoryInfo(subFolder);
  49. if (directoryInfo.Exists)
  50. m_SubFolderIndex++;
  51. else
  52. break;
  53. }
  54. }
  55. }
  56. public void Save()
  57. {
  58. if (m_SaveThread == null)
  59. {
  60. m_SaveThread = new Thread(SaveMeshThread);
  61. m_SaveThread.Start();
  62. }
  63. }
  64. void IMeshInfoProcessor.UpdateMeshInfo(ulong identifier, NRMeshingBlockState meshingBlockState, Mesh mesh)
  65. {
  66. NRDebugger.Debug("[MeshSaver] meshingBlockState: {0} identifier: {1}", meshingBlockState, identifier);
  67. lock (m_MeshDict)
  68. {
  69. m_MeshDict[identifier] = mesh;
  70. }
  71. }
  72. void SaveMeshThread()
  73. {
  74. Dictionary<ulong, Mesh> meshDictCopy;
  75. lock (m_MeshDict)
  76. {
  77. meshDictCopy = new Dictionary<ulong, Mesh>(m_MeshDict);
  78. }
  79. DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(SavePath, m_SubFolderIndex.ToString()));
  80. if (!directoryInfo.Exists)
  81. {
  82. directoryInfo.Create();
  83. }
  84. foreach (var item in meshDictCopy)
  85. {
  86. FileInfo fileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, item.Key.ToString() + ".obj"));
  87. StreamWriter sw = new StreamWriter(fileInfo.FullName);
  88. string str = MeshToString(item.Value);
  89. sw.Write(str);
  90. sw.Flush();
  91. sw.Close();
  92. }
  93. m_SaveThread = null;
  94. m_SubFolderIndex++;
  95. }
  96. void IMeshInfoProcessor.ClearMeshInfo()
  97. {
  98. NRDebugger.Debug("[MeshSaver] ClearMeshInfo.");
  99. DirectoryInfo directoryInfo = new DirectoryInfo(SavePath);
  100. if (directoryInfo.Exists)
  101. {
  102. foreach (var file in directoryInfo.EnumerateFiles("*.obj"))
  103. {
  104. file.Delete();
  105. }
  106. }
  107. }
  108. private static string MeshToString(Mesh mesh)
  109. {
  110. StringBuilder sb = new StringBuilder();
  111. sb.Append("# \n");
  112. Vector3[] vertices = mesh.vertices;
  113. foreach (Vector3 v in vertices)
  114. {
  115. sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, v.z));
  116. }
  117. sb.Append("\n");
  118. Vector3[] normals = mesh.normals;
  119. foreach (Vector3 vn in normals)
  120. {
  121. sb.Append(string.Format("vn {0} {1} {2}\n", vn.x, vn.y, vn.z));
  122. }
  123. sb.Append("\n");
  124. int[] triangles = mesh.triangles;
  125. for (int i = 0; i < triangles.Length; i += 3)
  126. {
  127. sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
  128. triangles[i] + 1, triangles[i + 1] + 1, triangles[i + 2] + 1));
  129. }
  130. sb.Append("\n");
  131. return sb.ToString();
  132. }
  133. public static Mesh StringToMesh(string data)
  134. {
  135. var lines = data.Split('\n');
  136. List<Vector3> verticeList = new List<Vector3>();
  137. List<Vector3> normalList = new List<Vector3>();
  138. List<int> triangleList = new List<int>();
  139. foreach (var line in lines)
  140. {
  141. var nums = line.Split(' ', '/');
  142. switch (nums[0])
  143. {
  144. case "v":
  145. verticeList.Add(new Vector3(float.Parse(nums[1]), float.Parse(nums[2]), float.Parse(nums[3])));
  146. break;
  147. case "vn":
  148. normalList.Add(new Vector3(float.Parse(nums[1]), float.Parse(nums[2]), float.Parse(nums[3])));
  149. break;
  150. case "f":
  151. triangleList.Add(int.Parse(nums[1]) - 1);
  152. triangleList.Add(int.Parse(nums[4]) - 1);
  153. triangleList.Add(int.Parse(nums[7]) - 1);
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. return new Mesh
  160. {
  161. vertices = verticeList.ToArray(),
  162. normals = normalList.ToArray(),
  163. triangles = triangleList.ToArray()
  164. };
  165. }
  166. }
  167. }