123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- public class CustomMeshCreator : MonoBehaviour
- {
- string filePath = "C:\\Users\\36299\\model\\";
-
- public Vector3[] vertexPositions;
- public Vector3[] vertexNormals;
- public Vector2[] vertexTextureCoords;
-
- public bool q;
- public bool w;
- public bool e;
- public bool r;
- public bool y;
- public bool u;
- public bool a;
- public bool c;
- public List<GameObject> list;
- public IEnumerator Download()
- {
- GameObject objp = new GameObject();
-
-
- Debug.Log("刷新");
- if(list!=null)
- {
- for (int l = 0; l < list.Count; l++)
- {
- Destroy(list[l]);
- }
- }
- list = new List<GameObject>();
- for (int k = 0; k < 72; k++)
- {
- try
- {
- int qq = q == true ? 1 : -1;
- int ww = w == true ? 1 : -1;
- int ee = e == true ? 1 : -1;
- int rr = r == true ? 1 : -1;
- int yy = y == true ? 1 : -1;
- int uu = u == true ? 1 : -1;
- int aa = a == true ? 1 : -1;
- int cc = c == true ? 1 : -1;
- List<Vector3> p = new List<Vector3>();
- List<Vector3> n = new List<Vector3>();
- List<Vector2> t = new List<Vector2>();
- List<int> tt = new List<int>();
-
- if (File.Exists(filePath + "obj" + k + ".bin"))
- {
-
- string binaryData = File.ReadAllText(filePath + "obj" + k + ".bin");
- JsonData data = JsonMapper.ToObject(binaryData);
- for (int i = 2; i < data["vertexPositions"].Count; i += 3)
- {
- 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())));
- }
- for (int i = 2; i < data["vertexNormals"].Count; i += 3)
- {
- 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())));
- }
- for (int i = 1; i < data["vertexTextureCoords"].Count; i += 2)
- {
- t.Add(new Vector2(aa * (float.Parse(data["vertexTextureCoords"][i-1 ].ToString())), cc * (float.Parse(data["vertexTextureCoords"][i].ToString()))));
- }
- for (int i = 0; i < data["indices"].Count; i++)
- {
- tt.Add(int.Parse(data["indices"][i].ToString()));
- }
- }
- else
- {
- Debug.LogError("File not found: " + filePath);
- }
- vertexPositions = p.ToArray();
- vertexNormals = n.ToArray();
- vertexTextureCoords = t.ToArray();
-
- Mesh mesh = new Mesh();
- GameObject nobj = new GameObject();
- nobj.transform.parent = objp.transform;
- nobj.name = "obj" + k;
- list.Add(nobj);
-
- mesh.vertices = vertexPositions;
- mesh.normals = vertexNormals;
- mesh.uv = vertexTextureCoords;
-
- int[] triangles = new int[vertexPositions.Length];
-
- mesh.triangles = tt.ToArray();
-
- MeshFilter meshFilter = nobj.GetComponent<MeshFilter>();
- if (meshFilter == null)
- {
- meshFilter = nobj.AddComponent<MeshFilter>();
- }
- meshFilter.mesh = mesh;
- if (!Directory.Exists("Assets/SavedMeshes"))
- AssetDatabase.CreateFolder("Assets", "SavedMeshes");
-
- string savePath = "Assets/SavedMeshes/" + nobj.name + ".asset";
- AssetDatabase.CreateAsset(mesh, savePath);
- AssetDatabase.SaveAssets();
-
- MeshRenderer meshRenderer = nobj.GetComponent<MeshRenderer>();
- if (meshRenderer == null)
- {
- meshRenderer = nobj.AddComponent<MeshRenderer>();
- meshRenderer.material = mat;
- }
- }
- catch
- {
- }
- }
- CreatePrefab(objp, "objp");
- yield return new WaitForSeconds(3f);
-
- }
- public MeshFilter meshFilter;
- public Renderer renderer;
- void SaveMesh(GameObject go)
- {
-
- MeshFilter meshFilter = go.GetComponent<MeshFilter>();
-
- if (meshFilter != null && meshFilter.sharedMesh != null)
- {
-
- Mesh mesh = meshFilter.sharedMesh;
-
- Mesh clonedMesh = Instantiate(mesh);
-
- string savePath = "Assets/SavedMeshes/"+ go.name+ ".asset";
-
- AssetDatabase.CreateAsset(clonedMesh, savePath);
- AssetDatabase.SaveAssets();
- Debug.Log("Mesh saved at: " + savePath);
- }
- else
- {
- Debug.LogError("MeshFilter or Mesh is missing.");
- }
- }
- public void CreatePrefab(GameObject prefabObject, string prefabPath)
- {
- if (!Directory.Exists("Assets/Prefabs"))
- AssetDatabase.CreateFolder("Assets", "Prefabs");
- string localPath = "Assets/Prefabs/" + gameObject.name + ".prefab";
-
- localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);
-
- PrefabUtility.SaveAsPrefabAsset(prefabObject, localPath, out bool success);
-
- DestroyImmediate(prefabObject);
- if (success)
- {
- Debug.Log("Prefab created at: " + prefabPath);
- }
- else
- {
- Debug.LogError("Failed to create prefab.");
- }
- }
- public Material mat;
- void Start()
- {
- StartCoroutine(Download());
- }
- void ProcessBinaryData(byte[] binaryData)
- {
-
-
-
-
- }
- }
|