CustomMeshCreator.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. public class CustomMeshCreator : MonoBehaviour
  8. {
  9. string filePath = "C:\\Users\\36299\\model\\";
  10. // Input your vertex data here (example data)
  11. public Vector3[] vertexPositions;
  12. public Vector3[] vertexNormals;
  13. public Vector2[] vertexTextureCoords;
  14. public bool q;
  15. public bool w;
  16. public bool e;
  17. public bool r;
  18. public bool y;
  19. public bool u;
  20. public bool a;
  21. public bool c;
  22. public List<GameObject> list;
  23. public IEnumerator Download()
  24. {
  25. GameObject objp = new GameObject();
  26. // while(true)
  27. // {
  28. Debug.Log("刷新");
  29. if(list!=null)
  30. {
  31. for (int l = 0; l < list.Count; l++)
  32. {
  33. Destroy(list[l]);
  34. }
  35. }
  36. list = new List<GameObject>();
  37. for (int k = 0; k < 72; k++)
  38. {
  39. try
  40. {
  41. int qq = q == true ? 1 : -1;
  42. int ww = w == true ? 1 : -1;
  43. int ee = e == true ? 1 : -1;
  44. int rr = r == true ? 1 : -1;
  45. int yy = y == true ? 1 : -1;
  46. int uu = u == true ? 1 : -1;
  47. int aa = a == true ? 1 : -1;
  48. int cc = c == true ? 1 : -1;
  49. List<Vector3> p = new List<Vector3>();
  50. List<Vector3> n = new List<Vector3>();
  51. List<Vector2> t = new List<Vector2>();
  52. List<int> tt = new List<int>();
  53. // Check if the file exists
  54. if (File.Exists(filePath + "obj" + k + ".bin"))
  55. {
  56. // Read all bytes from the binary file
  57. string binaryData = File.ReadAllText(filePath + "obj" + k + ".bin");
  58. JsonData data = JsonMapper.ToObject(binaryData);
  59. for (int i = 2; i < data["vertexPositions"].Count; i += 3)
  60. {
  61. p.Add(new Vector3(float.Parse(data["vertexPositions"][i - 2].ToString()) * qq, -float.Parse(data["vertexPositions"][i - 1].ToString()) * ww, ee * float.Parse(data["vertexPositions"][i].ToString())));
  62. }
  63. for (int i = 2; i < data["vertexNormals"].Count; i += 3)
  64. {
  65. n.Add(new Vector3(rr * float.Parse(data["vertexNormals"][i - 2].ToString()), -yy * float.Parse(data["vertexNormals"][i - 1].ToString()), uu * float.Parse(data["vertexNormals"][i].ToString())));
  66. }
  67. for (int i = 1; i < data["vertexTextureCoords"].Count; i += 2)
  68. {
  69. t.Add(new Vector2(aa * (float.Parse(data["vertexTextureCoords"][i-1 ].ToString())), cc * (float.Parse(data["vertexTextureCoords"][i].ToString()))));
  70. }
  71. for (int i = 0; i < data["indices"].Count; i++)
  72. {
  73. tt.Add(int.Parse(data["indices"][i].ToString()));
  74. }
  75. }
  76. else
  77. {
  78. Debug.LogError("File not found: " + filePath);
  79. }
  80. vertexPositions = p.ToArray();
  81. vertexNormals = n.ToArray();
  82. vertexTextureCoords = t.ToArray();
  83. // Create a new mesh
  84. Mesh mesh = new Mesh();
  85. GameObject nobj = new GameObject();
  86. nobj.transform.parent = objp.transform;
  87. nobj.name = "obj" + k;
  88. list.Add(nobj);
  89. // Assign vertex data to the mesh
  90. mesh.vertices = vertexPositions;
  91. mesh.normals = vertexNormals;
  92. mesh.uv = vertexTextureCoords;
  93. // Create triangles (assuming a simple structure without submeshes)
  94. int[] triangles = new int[vertexPositions.Length];
  95. mesh.triangles = tt.ToArray();
  96. // Assign the mesh to the MeshFilter component
  97. MeshFilter meshFilter = nobj.GetComponent<MeshFilter>();
  98. if (meshFilter == null)
  99. {
  100. meshFilter = nobj.AddComponent<MeshFilter>();
  101. }
  102. meshFilter.mesh = mesh;
  103. if (!Directory.Exists("Assets/SavedMeshes"))
  104. AssetDatabase.CreateFolder("Assets", "SavedMeshes");
  105. // 设置保存路径(可以根据需要修改路径)
  106. string savePath = "Assets/SavedMeshes/" + nobj.name + ".asset";
  107. AssetDatabase.CreateAsset(mesh, savePath);
  108. AssetDatabase.SaveAssets();
  109. // Add MeshRenderer component for visualization (optional)
  110. MeshRenderer meshRenderer = nobj.GetComponent<MeshRenderer>();
  111. if (meshRenderer == null)
  112. {
  113. meshRenderer = nobj.AddComponent<MeshRenderer>();
  114. meshRenderer.material = mat;
  115. }
  116. }
  117. catch
  118. {
  119. }
  120. }
  121. CreatePrefab(objp, "objp");
  122. yield return new WaitForSeconds(3f);
  123. // }
  124. }
  125. public MeshFilter meshFilter; // 引用你的MeshFilter组件
  126. public Renderer renderer; // 引用你的Renderer组件
  127. void SaveMesh(GameObject go)
  128. {
  129. // 获取MeshFilter组件
  130. MeshFilter meshFilter = go.GetComponent<MeshFilter>();
  131. // 检查MeshFilter是否存在
  132. if (meshFilter != null && meshFilter.sharedMesh != null)
  133. {
  134. // 获取Mesh
  135. Mesh mesh = meshFilter.sharedMesh;
  136. // 创建一个新的Mesh实例,防止修改原始Mesh
  137. Mesh clonedMesh = Instantiate(mesh);
  138. // 设置保存路径(可以根据需要修改路径)
  139. string savePath = "Assets/SavedMeshes/"+ go.name+ ".asset";
  140. // 保存Mesh为Asset文件
  141. AssetDatabase.CreateAsset(clonedMesh, savePath);
  142. AssetDatabase.SaveAssets();
  143. Debug.Log("Mesh saved at: " + savePath);
  144. }
  145. else
  146. {
  147. Debug.LogError("MeshFilter or Mesh is missing.");
  148. }
  149. }
  150. public void CreatePrefab(GameObject prefabObject, string prefabPath)
  151. {
  152. if (!Directory.Exists("Assets/Prefabs"))
  153. AssetDatabase.CreateFolder("Assets", "Prefabs");
  154. string localPath = "Assets/Prefabs/" + gameObject.name + ".prefab";
  155. // Make sure the file name is unique, in case an existing Prefab has the same name.
  156. localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);
  157. // 保存预制体
  158. PrefabUtility.SaveAsPrefabAsset(prefabObject, localPath, out bool success);
  159. // 销毁临时游戏对象
  160. DestroyImmediate(prefabObject);
  161. if (success)
  162. {
  163. Debug.Log("Prefab created at: " + prefabPath);
  164. }
  165. else
  166. {
  167. Debug.LogError("Failed to create prefab.");
  168. }
  169. }
  170. public Material mat;
  171. void Start()
  172. {
  173. StartCoroutine(Download());
  174. }
  175. void ProcessBinaryData(byte[] binaryData)
  176. {
  177. // Process the binary data here
  178. // You can interpret the bytes based on the structure of your data
  179. // For example, if it represents integers or floats, you need to interpret them accordingly
  180. // Example: int value = BitConverter.ToInt32(binaryData, startIndex);
  181. }
  182. }