public static void CacheItem(string url, Mesh mesh) { string path = Path.Combine(Application.persistentDataPath, url); byte[] bytes = MeshSerializer.WriteMesh(mesh, true); File.WriteAllBytes(path, bytes); }
public void SaveChunk(Mesh mesh) { string path = Path.Combine(Application.persistentDataPath, "chunkData/" + mesh.name); byte[] bytes = MeshSerializer.WriteMesh(mesh, true); File.WriteAllBytes(path, bytes); }
public IEnumerator LoadObjectFromDiskCR(string path, MeshLoader loader, GCodeHandler mc) { int layernum = 0; //create object to put all mesh types in var objectName = loader.GetObjectNameFromPath(path); var rootFolderOfObject = loader.dataPath + "/" + objectName; mc.RootForObject.transform.localPosition = Vector3.zero; mc.RootForObject.transform.localScale = Vector3.one; mc.RootForObject.transform.localRotation = Quaternion.identity; SortedDictionary <int, List <string> > sortedLayers = new SortedDictionary <int, List <string> >(); foreach (var folder in Directory.GetDirectories(rootFolderOfObject)) { var files = Directory.GetFiles(folder); foreach (var file in Directory.GetFiles(folder)) { var l = Convert.ToInt32(file.Substring(file.LastIndexOf(@" ") + 1, file.LastIndexOf(".") - file.LastIndexOf(@" ") - 1)); if (!sortedLayers.ContainsKey(l)) { sortedLayers.Add(l, new List <string>()); sortedLayers[l].Add(file); } else { sortedLayers[l].Add(file); } } } foreach (var layers in sortedLayers) { foreach (var file in layers.Value) { var typeString = file.Substring(file.LastIndexOf(@"\") + 1, file.LastIndexOf(' ') - file.LastIndexOf(@"\") - 1); mc.CreateTypeObject(typeString); var layername = file.Substring(file.LastIndexOf(@"\") + 1, file.LastIndexOf(".") - file.LastIndexOf(@"\") - 1); //get mesh from file var mesh = MeshSerializer.DeserializeMesh(File.ReadAllBytes(file)); //get the biggest layer number var l = mc.createLayerObject(layername, typeString, mesh); if (l > layernum) { layernum = l; } } yield return(null); } mc.endloading(layernum); }
/// <summary> /// Combine all peaces of mesh of the root, and return a byte array containing its serialization. Please notice that the root must be an empty gameObject /// </summary> /// <param name="root"></param> /// <returns></returns> public byte[] combineMeshAsByte(GameObject root, GameObject anchor) { GameObject parallelSceneRoot = Instantiate(root); parallelSceneRoot.transform.parent = anchor.transform; //Vector3 realPosition = parallelSceneRoot.transform.localPosition; //Quaternion realRotation = parallelSceneRoot.transform.localRotation; parallelSceneRoot.name = "parallelSceneroot"; parallelSceneRoot.transform.localPosition = Vector3.zero; parallelSceneRoot.transform.localRotation = Quaternion.identity; MeshFilter[] meshFilters = parallelSceneRoot.GetComponentsInChildren <MeshFilter>(); CombineInstance[] combine = new CombineInstance[meshFilters.Length]; Mesh finalMesh = new Mesh(); for (int i = 0; i < meshFilters.Length; i++) { if (meshFilters[i] != null) { combine[i].mesh = meshFilters[i].sharedMesh; //Debug.Log("parent mesh ser name" + meshFilters[i].transform.parent.name); // I have to specify the mesh transform w.r.t. a reference frame (different from the object's mesh), in this case I use the anchor frame since the parallelSceneRoot // is a child of anchor and I m creating the local transform matrix. combine[i].transform = Matrix4x4.TRS(meshFilters[i].transform.localPosition, meshFilters[i].transform.localRotation, meshFilters[i].transform.localScale); meshFilters[i].gameObject.SetActive(false); } } MeshFilter rootMesh = parallelSceneRoot.transform.GetComponent <MeshFilter>(); //Because the scene root has no MeshFilter component but only its childs, so we need to add it if (rootMesh != null) { Debug.Log("reset mesh parallelSceneRoot"); rootMesh.mesh = new Mesh(); } else { Debug.Log("parallelSceneRoot has no Mesh component, I add it"); rootMesh = parallelSceneRoot.AddComponent(typeof(MeshFilter)) as MeshFilter; MeshRenderer rootMeshRender = parallelSceneRoot.AddComponent <MeshRenderer>() as MeshRenderer; rootMeshRender.material = material; } finalMesh.CombineMeshes(combine); byte[] meshByte = MeshSerializer.SerializeMesh(finalMesh); Debug.Log("combine mesh position" + rootMesh.transform.position); DestroyImmediate(parallelSceneRoot); return(meshByte); }
public Mesh GetMesh() { if (data == null || data.Length < 1) { return(null); } return(MeshSerializer.DeserializeMesh(data)); }
public void SaveLayerAsAsset(Mesh mesh, string folder, string fileName) { if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } //Write the mesh to disk again File.WriteAllBytes(folder + fileName, MeshSerializer.SerializeMesh(mesh)); }
public static Mesh GetCacheItem(string url) { string path = Path.Combine(Application.persistentDataPath, url); if (File.Exists(path) == true) { byte[] bytes = File.ReadAllBytes(path); return(MeshSerializer.ReadMesh(bytes)); } return(null); }
// Use this for initialization public byte[] SaveMesh() { Mesh inputMesh = GetComponent <MeshFilter>().mesh; string fullFileName = Application.dataPath + "/" + fileName; feedSave = MeshSerializer.WriteMesh(inputMesh, saveTangents); print("Saved " + name + " mesh to " + fullFileName); return(feedSave); }
public void SaveLayerAsAsset(Mesh mesh, string name) { if (!Directory.Exists(FolderToExportTo)) { Directory.CreateDirectory(FolderToExportTo); } //Write the mesh to disk again mesh.name = name; File.WriteAllBytes(FolderToExportTo + name + ".mesh", MeshSerializer.SerializeMesh(mesh));// GOAAAL }
//public void sendMessage(Vector3[] vertices, Vector2[] uvs, int[] triangles) public void sendMessage(Mesh mesh) { //PointsMessage message = new PointsMessage(); //message.vertices = vertices; //message.uvs = uvs; //message.triangles = triangles; //NetworkServer.SendToAll(CustomMessage.hiMessage, message); var data = MeshSerializer.WriteMesh(mesh, true); photonView.RPC("TransferMesh", PhotonTargets.All, CustomMessage.hiMessage, data); }
public void sendSpatialMesh(Mesh mesh) { //PointsMessage message = new PointsMessage(); //message.vertices = vertices; //message.uvs = uvs; //message.triangles = triangles; //myClient.Send(SpatialMeshMsg.meshMsg, message); var data = MeshSerializer.WriteMesh(mesh, true); photonView.RPC("TransferMesh", PhotonTargets.All, SpatialMeshMsg.meshMsg, data); }
Vector3[] newVertices() { Mesh mesh = MeshGetters.getMesh(this.gameObject); int[] triangles = mesh.triangles; Vector3[] triangleA = new Vector3[3];//Lets not allocate new vector3s so much Vector3[] triangleB = new Vector3[3]; bool verticesUpdatedSuccessfully; Vector3[] newVertices; do { newVertices = mesh.vertices; verticesUpdatedSuccessfully = true; //Modify the vertices for (int i = 0; i < newVertices.Length; i++) { wiggleVector3(ref newVertices[i]); } //Check the triangles for collisions for (int i = 0; i < triangles.Length; i += 3) { triangleA[0] = newVertices[triangles[i]]; triangleA[1] = newVertices[triangles[i + 1]]; triangleA[2] = newVertices[triangles[i + 2]]; for (int j = i + 3; j < triangles.Length; j += 3) { triangleB[0] = newVertices[triangles[j]]; triangleB[1] = newVertices[triangles[j + 1]]; triangleB[2] = newVertices[triangles[j + 2]]; if (!GeometryFunctions.triangleNonintersectCheck(triangleA, triangleB)) { verticesUpdatedSuccessfully = false; break; } } } } while (!verticesUpdatedSuccessfully); if (this.saveModel) { MeshSerializer.serializeMesh(mesh, this.name); } return(newVertices); }
void CreateMesh() { Debug.Log("CreateMesh"); int index = 0; foreach (byte[] mesh in meshDic.Values) { GameObject tempObj = Instantiate(this.originMesh, transform); tempObj.GetComponent <MeshFilter>().sharedMesh = MeshSerializer.ReadMesh(mesh); tempObj.GetComponent <MeshCollider>().sharedMesh = MeshSerializer.ReadMesh(mesh); tempObj.name = string.Format("Mesh {0}", index++); } }
public MeshData(SpatialAwarenessMeshObject aMesh) { if (aMesh != null) { data = MeshSerializer.SerializeMesh(aMesh.Filter.sharedMesh); transform = new TransformS(aMesh.Renderer.transform); } else { data = new byte[0]; transform = new TransformS(null); } }
/// <summary> /// Save the model in binary format. /// </summary> /// <param name="filename">The name of the file to save.</param> public void saveModel(String filename) { if (entity != null) { using (MeshSerializer meshSerializer = new MeshSerializer()) { using (MeshPtr mesh = entity.getMesh()) { meshSerializer.exportMesh(mesh.Value, filename); } } } }
public void SaveIt() { if (inputMesh == null) { inputMesh = GameObject.FindGameObjectWithTag("Mesh").GetComponent <MeshFilter> ().mesh; } if (inputMesh != null) { save.Play(); var fullFileName = Application.dataPath + "/" + fileName; MeshSerializer.WriteMeshToFile(inputMesh, fullFileName, saveTangents); Debug.Log("saved " + name + " mesh to " + fullFileName); } }
public void SaveMesh() { //string path = Path.Combine(Application.persistentDataPath, url); //combineMesh(SceneRoot); #if WINDOWS_UWP var folder = WindowsStorage.ApplicationData.Current.LocalFolder; string path = folder.Path + "/meshMap.bin"; byte[] bytes = MeshSerializer.SerializeMesh(rootMesh.mesh); File.WriteAllBytes(path, bytes); #else Debug.Log("Load on Device is only supported in Universal Windows Applications"); #endif }
public Mesh LoadObject(Mesh input) { if (ClayMesh == null) { ClayMesh = GameObject.FindGameObjectWithTag("Mesh").GetComponent <MeshFilter> (); } mat.mainTexture = null; if (fileName != null) { input = MeshSerializer.ReadMeshFromFile(fileName); } return(input); }
public void SetCurrentState() // 현재상태 불러오기 { sd.mFurniture = gameObject.name; sd.mPosition = gameObject.transform.position; sd.mRotation = gameObject.transform.rotation; sd.mScale = gameObject.transform.lossyScale; if (gameObject.GetComponent <changeColor>() != null) { sd.mColor = gameObject.GetComponent <changeColor>().GetColor(); } if (gameObject.GetComponent <MeshFilter>() != null) { sd.mesh = MeshSerializer.WriteMesh(gameObject.GetComponent <MeshFilter>().mesh, true); } }
// Start is called before the first frame update void Start() { Mesh mesh = GetComponent <MeshFilter>().mesh; //deserializeMesh returns a default mesh, if the requested one doesn't exist Mesh loadedMesh = MeshSerializer.deserializeMesh(this.name); mesh.triangles = loadedMesh.triangles; mesh.vertices = loadedMesh.vertices; mesh.colors = loadedMesh.colors; //UPDATE THE MESH COLLIDER DestroyImmediate(this.GetComponent <MeshCollider>()); var collider = gameObject.AddComponent <MeshCollider>(); collider.sharedMesh = mesh; collider.convex = true; }
public static GameObject LoadWall(string fileNameToLoad, string newWallName = "Wall") { GameObject obj = new GameObject(newWallName); MeshFilter mf = obj.AddComponent <MeshFilter>(); MeshRenderer mr = obj.AddComponent <MeshRenderer>(); Mesh newMesh = new Mesh(); newMesh = MeshSerializer.LoadMeshFromPath(fileNameToLoad); newMesh.RecalculateBounds(); mf.mesh = newMesh; mr.material = new Material(Shader.Find("Standard")); //mr.material.mainTexture = WallTexture; return(obj); }
{ //With help of: https://dzone.com/articles/convert-object-byte-array-and, FullSerializer, SerializeHelper protected override fsResult DoSerialize(GameObject model, Dictionary <string, fsData> serialized) { var result = fsResult.Success; if (model.hideFlags != HideFlags.None) { model.hideFlags = HideFlags.None; } if (!model.GetComponent <MeshRenderer>().enabled) { model.GetComponent <MeshRenderer>().enabled = true; } GameObject go = Object.Instantiate(model); go.name = model.name; //We should check for Editing Mode (because it's only required if we are serializing the desried GameObject in Editor), //but, why? It's more safe and more slow :) (This remember me to PHP haha) GameObject foundGo = null; Mesh mesh = PluginsHelper.FindMesh(go, ref foundGo); if (mesh != null) { MeshSerializer.WriteMesh(mesh, true).SaveAsAsset(mesh, foundGo.name); } byte[] bytes = go.SaveObjectTree(); string path = bytes.SaveAsAsset(go, go.name); if (Application.isPlaying) { Object.Destroy(go); } else { Object.DestroyImmediate(go); } result += SerializeMember(serialized, null, "path", path); return(result); }
public override void OnInspectorGUI() { DrawDefaultInspector(); if (GUILayout.Button("Serialize XML")) { MeshSerializer tar = (MeshSerializer)target; if (tar != null && tar.outputMesh != null) { var outputMesh = tar.outputMesh; var path = EditorUtility.SaveFilePanel("Save mesh as XML", "", "out.xml", "xml"); if (!string.IsNullOrEmpty(path)) { XmlSerializer ser = new XmlSerializer(typeof(MeshContentSerialized)); TextWriter textOut = new StreamWriter(path); ser.Serialize(textOut, outputMesh); } } } if (GUILayout.Button("Serialize Binary")) { MeshSerializer tar = (MeshSerializer)target; if (tar != null && tar.outputMesh != null) { var outputMesh = tar.outputMesh; var path = EditorUtility.SaveFilePanel("Save mesh as Binary avmesh", "", "out.avmesh", "avmesh"); if (!string.IsNullOrEmpty(path)) { IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, outputMesh); } } } }
public static void Bundle(string[] inputFiles, string outputFile) { AssetDatabase.Refresh(); Debug.Log(string.Format("Bundling output: {0}", outputFile)); Debug.Log(string.Join(", ", inputFiles)); //UnityEngine.Object[] allAssets = inputFiles.SelectMany(f=>AssetDatabase.LoadAllAssetsAtPath(f,typeof(UnityEngine.Object)).ToList()).Where (f=>f is Mesh).ToArray(); UnityEngine.Mesh[] meshes = inputFiles.Select(f => AssetDatabase.LoadAssetAtPath(Path.Combine("Assets", f), typeof(Mesh)) as Mesh).Where(m => m != null).ToArray(); if (meshes.Length == 0) { throw new UnityException("No meshes found to process!"); } /* * List<Mesh> meshes = new List<Mesh>(); * * foreach( string file in inputFiles ){ * var objects = AssetDatabase.LoadAssetAtPath("Assets/"+file,typeof(MeshFilter)); * Debug.Log ( string.Join(", ",objects.Select(o=>o.name).ToArray() ) ); * } */ foreach (Mesh mesh in meshes) { Debug.Log(string.Format("Serializing mesh {0}", outputFile)); byte[] meshData = MeshSerializer.WriteMesh(mesh, true); File.WriteAllBytes(outputFile, meshData); break; } /* * BuildPipeline.BuildAssetBundle(allAssets[0], * allAssets, * outputFile, * BuildAssetBundleOptions.CollectDependencies | * BuildAssetBundleOptions.CompleteAssets | * BuildAssetBundleOptions.DeterministicAssetBundle, * BuildTarget.Android); */ }
public static GameObject ReadAssetFromDisk(string filePath, string alternativeName) { var bin = File.ReadAllBytes(filePath); Mesh m = MeshSerializer.ReadMesh(bin); GameObject g = new GameObject(System.IO.Path.GetFileNameWithoutExtension(filePath)); if (!string.IsNullOrEmpty(alternativeName)) { g.name = alternativeName; } var mf = g.AddComponent <MeshFilter>(); mf.sharedMesh = m; var coll = g.AddComponent <MeshCollider>(); coll.sharedMesh = m; g.AddComponent <MeshRenderer>(); return(g); }
public static Mesh LoadMesh(string filename) { filename = filename.Replace(":", "-"); string path = Application.persistentDataPath + "/" + filename + ".mesh"; try { Byte[] bytes = System.IO.File.ReadAllBytes(path); return(MeshSerializer.ReadMesh(bytes)); } catch (Exception e) { if (verbose) { Debug.Log("Load Mesh Exception: " + e.Message); } } return(null); }
public static void SaveMesh(string filename, Mesh mesh) { filename = filename.Replace(":", "-"); try { Byte[] bytes = MeshSerializer.WriteMesh(mesh, true); string path = Application.persistentDataPath + "/" + filename + ".mesh"; System.IO.File.WriteAllBytes(path, bytes); if (verbose) { Debug.Log("Mesh saved @ " + path); } } catch (System.Exception e) { if (verbose) { Debug.Log("Save Mesh Exception: " + e.Message); } } }
public void SetSdToObject() // load한 데이터를 오브젝트에 넣음 { gameObject.name = sd.mFurniture; gameObject.transform.position = sd.mPosition; gameObject.transform.rotation = sd.mRotation; gameObject.transform.localScale = Vector3.one; gameObject.transform.localScale = new Vector3( sd.mScale.x / gameObject.transform.lossyScale.x, sd.mScale.y / gameObject.transform.lossyScale.y, sd.mScale.z / gameObject.transform.lossyScale.z); if (gameObject.GetComponent <changeColor>() != null) { gameObject.GetComponent <changeColor>().Start(); gameObject.GetComponent <changeColor>().SetColor(sd.mColor); } if (gameObject.GetComponent <MeshFilter>() != null) { gameObject.GetComponent <MeshFilter>().mesh = MeshSerializer.ReadMesh(sd.mesh); } }
public void LoadChunk(int x, int y) { //if chunk has already been initialised, return or set active if not active if (GameObject.Find("chunk: " + x + ", " + y)) { if (GameObject.Find("chunk: " + x + ", " + y).activeSelf) { return; } GameObject.Find("chunk: " + x + ", " + y).SetActive(true); } //chunk gameobject setup GameObject newChunk = MakeChunk(x, y); Mesh chunkMesh = new Mesh(); chunkMesh.name = x + ", " + y; // loading a chunk mesh, if not found a new one is generated string path = Path.Combine(Application.persistentDataPath, x + ", " + y); if (File.Exists(path) == true) { byte[] bytes = File.ReadAllBytes(path); chunkMesh = MeshSerializer.ReadMesh(bytes); } else { chunkMesh = GenerateChunk(x, y, chunkSize, magnitude, frequency); } chunkMesh.RecalculateNormals(); newChunk.GetComponent <MeshFilter>().mesh = chunkMesh; newChunk.GetComponent <MeshCollider>().sharedMesh = chunkMesh; newChunk.GetComponent <MeshRenderer>().material = chunkMat; }
void Start() { string path = "Assets/Resources/Meshes/"; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch (IOException e) { Debug.LogError(e.Message); } Mesh inputMesh = GetComponent <MeshFilter>().mesh; string fullFileName = path + fileName; MeshSerializer.WriteMeshToFile(inputMesh, fullFileName, saveTangents); Debug.LogError("Saved " + name + " mesh to " + fullFileName); }