CGALDotNetUnity icon indicating copy to clipboard operation
CGALDotNetUnity copied to clipboard

Unity mesh to Polyhedron3 is not working

Open Mitko88 opened this issue 1 year ago • 3 comments

It seems the conversion to Polyhedron3 is not working. I cannot get a valid closed triangle mesh for any Unity mesh. You can try with a cube.

Mitko88 avatar Aug 01 '22 01:08 Mitko88

I've managed to transfer some of my meshes to valid closed triangle polyhdron3 by using StitchBorders function. However, there were some meshes for which StitchBorders function kills Unity.

Mitko88 avatar Aug 02 '22 01:08 Mitko88

Hi- I found a similar issue with the cube in unity- I think it has something to do with the fact that a unity cube has 24 points (not 8).

I wrote a simple function to check this, and to convert to polyhedra:

GameObject createCube(float x_scale = 1, float y_scale = 1, float z_scale = 1, Vector3 offset = default)
    {
        //Create triangles
        var indices = new int[36]
        {
            //front
            0, 3,1,
            1, 3,2,
            //back
            4, 5,7,
            5, 6,7,
            //top
            3,7,2,
            7,6,2,
            // bottom
            0,1,4,
            1,5,4,
            //left
            3,4,7,
            4,3,0,
            //right
            1,2,6,
            1,6,5
        };
        //The triangles points.
        var points = new Vector3[8]
        {
            new Vector3(0, 0, 0),
            new Vector3(1*x_scale, 0, 0),
            new Vector3(1*x_scale, 1*y_scale, 0),
            new Vector3(0, 1*y_scale, 0),
            new Vector3(0, 0, 1*z_scale),
            new Vector3(1 * x_scale, 0, 1*z_scale),
            new Vector3(1 * x_scale , 1*y_scale, 1*z_scale),
            new Vector3(0, 1*y_scale, 1*z_scale)
        };
        for (int i = 0; i < points.Length; i++)
        {
            points[i] = points[i] + offset;
        }

        GameObject go = new GameObject();
        Mesh m = go.AddComponent<MeshFilter>().mesh;
        m.vertices = points;
        m.triangles = indices;
        MeshRenderer m_r = go.AddComponent<MeshRenderer>();
        m_r.material = Resources.Load("Materials/TransparentRed", typeof(Material)) as Material; ;
        return go;
    }

    Polyhedron3<EEK> ConvertGoToPolyhedron(GameObject go)
    {
        // this takes a unity gameobject, and turns it into an EEK polyhedron (middle accuracy, middle speed).
        // to convert back, just call .toUnityMesh() on the polyhedron itself.
        Vector3[] unityMeshPoints = go.GetComponent<MeshFilter>().mesh.vertices;
        Point3d[] cgalMeshPoints = new Point3d[unityMeshPoints.Length];
        for (int i = 0; i < unityMeshPoints.Length; i++)
        {
            cgalMeshPoints[i] = unityMeshPoints[i].ToCGALPoint3d();
        }
        var polyhedron = new Polyhedron3<EEK>(cgalMeshPoints, go.GetComponent<MeshFilter>().mesh.triangles);
        polyhedron.Translate(new Point3d(go.transform.position.x, go.transform.position.y, go.transform.position.z));

        return polyhedron;
    }
`

AlastairShipman1 avatar Nov 08 '22 10:11 AlastairShipman1

I spent some time on this topic. I had a large 3D model of a building imported into Unity, and 15% of all objects could not be transferred to polyhedrons. There were many issues with the imported geometry. I had stairs where the steps and risers were merged into one object, and they needed first to be split, which is not an easy to do. When it comes to the Unity cube, you are right. The problem is there are more vertices, which cgal considers as an object which self-intersect.

Mitko88 avatar Nov 08 '22 22:11 Mitko88