void CutMesh(Mesh cutMesh, GameObject cutMeshGO, string name) { string cutMeshOff = ObjectFileFormat.MeshToOff(cutMesh, cutMeshGO.transform); StreamWriter strw = new StreamWriter("./Assets/Models/" + cutMeshGO.name + ".off"); strw.Write(cutMeshOff); strw.Close(); string cuttedMeshOff = System.IO.File.ReadAllText("./Assets/Models/squirrel.off"); StringBuilder strB = new StringBuilder(); //Extract transformation matrix Matrix4x4 transformationMatrix = cuttedMeshTransform.localToWorldMatrix; strB.Append(transformationMatrix.m00).AppendLine(); strB.Append(transformationMatrix.m01).AppendLine(); strB.Append(transformationMatrix.m02).AppendLine(); strB.Append(transformationMatrix.m03).AppendLine(); strB.Append(transformationMatrix.m10).AppendLine(); strB.Append(transformationMatrix.m11).AppendLine(); strB.Append(transformationMatrix.m12).AppendLine(); strB.Append(transformationMatrix.m13).AppendLine(); strB.Append(transformationMatrix.m20).AppendLine(); strB.Append(transformationMatrix.m21).AppendLine(); strB.Append(transformationMatrix.m22).AppendLine(); strB.Append(transformationMatrix.m23).AppendLine(); strw = new StreamWriter("./Assets/models/matrix.txt"); strw.Write(strB.ToString()); strw.Close(); IntPtr cuttedMeshPtr = Marshal.StringToHGlobalAnsi(cuttedMeshOff); IntPtr cuttedMeshTransformPtr = Marshal.StringToHGlobalAnsi(strB.ToString()); IntPtr cutMeshPtr = Marshal.StringToHGlobalAnsi(cutMeshOff); IntPtr ptrResult = CGALController.booleanOperationClean(cuttedMeshPtr, cuttedMeshTransformPtr, cutMeshPtr, Marshal.StringToHGlobalAnsi("difference")); //Convert IntPtr to string string result = Marshal.PtrToStringAnsi(ptrResult); //exportOff (result, "mesh" + Time.fixedDeltaTime); // 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(); ObjExporter.MeshToFile(myMesh, name, "BenchMat"); addMeshToSceneAndExport(name, myMesh, transform.GetComponent <Transform> ().localScale.x); }
private bool Intersects(GameObject cutGO, GameObject go2) { if (cutGO.GetComponent <MeshCollider> ().bounds.Intersects(go2.GetComponent <MeshCollider> ().bounds)) { string cutMeshOff = ObjectFileFormat.MeshToOff(cutGO.GetComponent <MeshFilter> ().mesh, cutGO.transform); string cuttedMeshOff = ObjectFileFormat.MeshToOff(go2.GetComponent <MeshFilter> ().mesh, go2.transform); int result = CGALController.checkIntersection(Marshal.StringToHGlobalAnsi(cuttedMeshOff), Marshal.StringToHGlobalAnsi(cutMeshOff)); if (result == 1) { return(true); } } return(false); }
public void onClickBooleanOperation(string name) { //Get GameObjects and their mesh from scene //GameObject go1 = GameObject.Find ("bunny"); GameObject go1 = selectableObjects[0].gameObject; GameObject go2 = selectableObjects[1].gameObject; MeshCollider col1 = go1.GetComponent <MeshCollider>(); MeshCollider col2 = go2.GetComponent <MeshCollider>(); if (col1.bounds.Intersects(col2.bounds)) { Mesh mesh1 = go1.GetComponent <MeshFilter>().mesh; Mesh mesh2 = go2.GetComponent <MeshFilter>().mesh; //Convert mesh to off string to IntPtr IntPtr ptr1 = Marshal.StringToHGlobalAnsi(ObjectFileFormat.MeshToOff(mesh1, go1.transform)); IntPtr ptr2 = Marshal.StringToHGlobalAnsi(ObjectFileFormat.MeshToOff(mesh2, go2.transform)); //Boolean union computation IntPtr ptrResult = CGALController.booleanOperation(ptr1, ptr2, Marshal.StringToHGlobalAnsi(name)); //Convert IntPtr to string string result = Marshal.PtrToStringAnsi(ptrResult); /*Debug.Log (result); * //Write the computed off in a file * StreamWriter strW = new StreamWriter ("./Assets/result.off"); * strW.Write (result); * strW.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 addMeshToScene("result", ObjectFileFormat.OffToMesh(new StreamReader(stream)), 1f); //Remove previous objects from scene Destroy(go1); Destroy(go2); } else { Debug.Log("Selected objects do not overlap"); } }