Exemplo n.º 1
0
    public static void CreateMeshFromOff()
    {
        var path    = AssetDatabase.GetAssetPath(Selection.activeObject);
        var mesh    = ObjectFileFormat.OffToMesh(new StreamReader(path));
        var newPath = AssetDatabase.GenerateUniqueAssetPath(Path.ChangeExtension(path, ".asset"));

        AssetDatabase.CreateAsset(mesh, newPath);
        AssetDatabase.Refresh();
    }
Exemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        selectableObjects = new List <Selectable>();
        buttons           = this.gameObject.GetComponentsInChildren <Button>();

        StreamReader strR = new StreamReader("./Assets/Models/bunny.off");
        Mesh         mesh = ObjectFileFormat.OffToMesh(strR);

        strR.Close();
        addMeshToScene("bunny", mesh, 10f);
    }
Exemplo n.º 3
0
    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);
    }
Exemplo n.º 4
0
    public static void CreateOffFromMesh()
    {
        var mesh = Selection.activeObject as Mesh;

        if (mesh == null)
        {
            throw new System.ArgumentException("No mesh selected.");
        }
        var path    = AssetDatabase.GetAssetPath(mesh);
        var newPath = AssetDatabase.GenerateUniqueAssetPath(Path.ChangeExtension(path, ".off"));

        ObjectFileFormat.MeshToOff(mesh, new StreamWriter(newPath));
        AssetDatabase.Refresh();
    }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
    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");
        }
    }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        public void HandleCut(Vector3[] positions)
        {
            //Generate cut meshes from points
            Vector2 B1;

            //LOOP
            Transform[] childs = this.objectContainer.GetComponentsInChildren <Transform>();
            foreach (Transform childTransform in childs)
            {
                //Check its not parent transform
                if (childTransform.GetComponent <BoxCollider>() == null || childTransform.gameObject == this.objectContainer)
                {
                    continue;
                }

                GameObject cuttedGameObject = childTransform.gameObject;

                //Generated cut meshes. Mesh is generated at world origin.
                Mesh cutMesh  = GeomUtils.GenerateMeshFromPositions(positions, 1, out B1, cuttedGameObject);
                Mesh cutMesh2 = GeomUtils.GenerateMeshFromPositions(positions, 2, out B1, cuttedGameObject);

                //Generate cut GO
                GameObject cutMeshGO  = AddMeshToScene("Cut Mesh", cutMesh, 1f);
                GameObject cutMeshGO2 = AddMeshToScene("Cut Mesh 2", cutMesh2, 1f);

                //Move cut mesh to previous position to check intersection
                cutMeshGO.transform.position   = Vector3.zero;
                cutMeshGO.transform.position  += new Vector3(B1.x, B1.y, 0);
                cutMeshGO2.transform.position  = Vector3.zero;
                cutMeshGO2.transform.position += new Vector3(B1.x, B1.y, 0);

                //Check intersection between parts and the plane drawn
                if (GeomUtils.Intersects(cutMeshGO, cuttedGameObject) && GeomUtils.CheckTrailIsLongEnough(positions, cuttedGameObject))
                {
                    try
                    {
                        //Generate cutted mesh off file and matrix
                        string cuttedMeshOff    = ObjectFileFormat.MeshToOff(cuttedGameObject.GetComponent <MeshFilter>().mesh, cuttedGameObject.transform);
                        string cuttedMeshMatrix = GeomUtils.GetTransformationMatrix(cuttedGameObject.transform, 0f, /*-2f * cutMeshGO.transform.rotation.eulerAngles.y*/ 0f, 0f);

                        //Do cut with cut mesh 1
                        Mesh mesh1 = CutMesh(cutMesh, cutMeshGO, cuttedGameObject.name, cuttedMeshOff, cuttedMeshMatrix);
                        //Do cut with cut mesh 2
                        Mesh mesh2 = CutMesh(cutMesh2, cutMeshGO2, cuttedGameObject.name, cuttedMeshOff, cuttedMeshMatrix);

                        // Add result to scene

                        GameObject Result1 = GeomUtils.AddMeshToScene("Split-" + DateTime.Now.ToString("hhmmssffff") + ".1", mesh1, new Vector3(0, -0.5f, 0), objectContainer.transform);
                        GameObject Result2 = GeomUtils.AddMeshToScene("Split-" + DateTime.Now.ToString("hhmmssffff") + ".2", mesh2, new Vector3(0, 0.5f, 0), objectContainer.transform);

                        //Destroy cutted game object container
                        Destroy(GameObject.Find(cuttedGameObject.name).transform.parent.gameObject);
                    }
                    catch (FileNotFoundException e)
                    {
                        Debug.Log(e);
                        errorMesage.text = "Le fichier de maillage est introuvable";
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e);
                        errorMesage.text = "Trail is too short.";
                    }
                }
                //Destroy cut GO
                Destroy(cutMeshGO);
                Destroy(cutMeshGO2);
            }
        }
    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();
        }
    }