public static bool Intersects(GameObject cutGO, GameObject go2) { if (cutGO.GetComponent <BoxCollider>().bounds.Intersects(go2.GetComponent <BoxCollider>().bounds)) { string cutMeshOff = ObjectFileFormat.MeshToOff(cutGO.GetComponent <MeshFilter>().mesh, cutGO.transform); string cuttedMeshOff = ObjectFileFormat.MeshToOff(go2.GetComponent <MeshFilter>().mesh, go2.transform); int result = CGALPlugin.CheckIntersection(Marshal.StringToHGlobalAnsi(cutMeshOff), Marshal.StringToHGlobalAnsi(cuttedMeshOff)); if (result == 1) { return(true); } } SceneManager.instance.errorMesage.text = "Trail do not intersects any object."; return(false); }
private Mesh CutMesh(Mesh cutMesh, GameObject cutMeshGO, string cuttedGoName, string cuttedMeshOff, string cuttedMeshMatrix) { /* ********** handle cut mesh ********** */ string cutMeshOff = ObjectFileFormat.MeshToOff(cutMesh, cutMeshGO.transform); IntPtr cutMeshPtr = Marshal.StringToHGlobalAnsi(cutMeshOff); IntPtr cutMeshTransformPtr = Marshal.StringToHGlobalAnsi(GeomUtils.GetTransformationMatrix(cutMeshGO.transform, 0f, 0f, 0f)); //exportOff //ExportOff(cutMeshOff, "./Assets/" + cutMeshGO.name + ".off"); /* ********** handle cutted mesh ********** */ IntPtr cuttedMeshPtr = Marshal.StringToHGlobalAnsi(cuttedMeshOff); IntPtr cuttedMeshTransformPtr = Marshal.StringToHGlobalAnsi(cuttedMeshMatrix); //exportOff //ExportOff(cuttedMeshOff, "./Assets/" + cuttedGoName + ".off"); /* ********** compute cut with CGAL********** */ IntPtr ptrResult = CGALPlugin.BooleanOperationClean(cuttedMeshPtr, cuttedMeshTransformPtr, cutMeshPtr, cutMeshTransformPtr, Marshal.StringToHGlobalAnsi("difference")); //Convert IntPtr to string string result = Marshal.PtrToStringAnsi(ptrResult); //exportOff //ExportOff(result, DataPath + initialObjectName + parts + ".off"); // Open string stream on the result off string byte[] byteArray = Encoding.UTF8.GetBytes(result); MemoryStream stream = new MemoryStream(byteArray); //Convert off string to Mesh and add it to scene Mesh myMesh = ObjectFileFormat.OffToMesh(new StreamReader(stream)); stream.Close(); myMesh.RecalculateNormals(); //Export obj file ObjExporter.MeshToFile(myMesh, "Split-" + DateTime.Now.ToString("hhmmssffff"), "BenchMat"); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif return(myMesh); }
public IEnumerator FusionSelection() { //Wait for object selection or cancel while (fusionList.Count != 2 && this.FusionPanel.activeInHierarchy) { yield return(new WaitForSeconds(0.001f)); } //Do merge if (fusionList.Count == 2) { GameObject GO1 = GameObject.Find(fusionList.Keys[0]); GameObject GO2 = GameObject.Find(fusionList.Keys[1]); Debug.Log("MERGING " + GO1.name + " AND " + GO2.name); if (GeomUtils.Intersects(GO1, GO2)) { Debug.Log("INTERSECTS"); string GO1Off = ObjectFileFormat.MeshToOff(GO1.GetComponent <MeshFilter>().mesh, GO1.transform); string GO2Off = ObjectFileFormat.MeshToOff(GO2.GetComponent <MeshFilter>().mesh, GO2.transform); System.IntPtr GO1Ptr = Marshal.StringToHGlobalAnsi(GO1Off); IntPtr GO2Ptr = Marshal.StringToHGlobalAnsi(GO2Off); String matrix1 = GeomUtils.GetTransformationMatrix(GO1.transform, 0f, 0f, 0f); String matrix2 = GeomUtils.GetTransformationMatrix(GO1.transform, 0f, 0f, 0f); IntPtr GO1TransformPtr = Marshal.StringToHGlobalAnsi(matrix1); IntPtr GO2TransformPtr = Marshal.StringToHGlobalAnsi(matrix2); IntPtr ptrResult = CGALPlugin.BooleanOperationClean(GO1Ptr, GO1TransformPtr, GO2Ptr, GO2TransformPtr, Marshal.StringToHGlobalAnsi("union")); string result = Marshal.PtrToStringAnsi(ptrResult); //exportOff /*StreamWriter SW = new StreamWriter(SceneManager.DataPath + "Fusion-" + DateTime.Now.ToString("hhmmss") + (Time.deltaTime * 1000) + ".off"); * SW.Write(result); * SW.Close();*/ // Open string stream on the result off string byte[] byteArray = Encoding.UTF8.GetBytes(result); MemoryStream stream = new MemoryStream(byteArray); //Convert off string to Mesh and add it to scene Mesh myMesh = ObjectFileFormat.OffToMesh(new StreamReader(stream)); stream.Close(); myMesh.RecalculateNormals(); //Export obj file ObjExporter.MeshToFile(myMesh, "Fusion-" + DateTime.Now.ToString("hhmmssffff"), "BenchMat"); //Add mesh to scene GeomUtils.AddMeshToScene("Fusion-" + DateTime.Now.ToString("hhmmssffff"), myMesh, Vector3.zero, objectContainer.transform); //Destroy previous previous meshes Destroy(GO1.transform.parent.gameObject); Destroy(GO2.transform.parent.gameObject); } //Not intersecting else { SceneManager.instance.errorMesage.text = "Selected objects are not overlapped !"; } ResetFusionSelection(); } }