예제 #1
0
    private void createPlanarMesh(Mesh mesh3d)
    {
        mesh = new Mesh();
        List <DynamicObject> objects = arranger.getObjects();

        foreach (DynamicObject obj in objects)
        {
            obj.assigned = false;
        }

        Debug.Log("Triangles count " + mesh3d.triangles.Length / 3);
        Debug.Log("UV count " + mesh3d.uv.LongLength);
        Debug.Log("Vertices count " + mesh3d.vertices.Length);

        gridVertices = new Vector3[mesh3d.triangles.Length];
        neighbours   = new Neighbour[mesh3d.triangles.Length / 3];
        texList      = new Texture2D[texObjectsCount = objects.Count, normalizationStepMax + 1];
        textureObjectsOnTriangles = new List <DynamicObject> [mesh3d.triangles.Length / 3];
        int found = 0;

        // iterating through every triangle
        for (int i = 0; i < mesh3d.triangles.Length; i += 3)
        {
            // UV triangle from 3D model
            Vector2 u1 = mesh3d.uv[mesh3d.triangles[i + 0]];
            Vector2 u2 = mesh3d.uv[mesh3d.triangles[i + 1]];
            Vector2 u3 = mesh3d.uv[mesh3d.triangles[i + 2]];
            //Triangle3D vvv = new Triangle3D(u1, u2, u3);
            //vvv.print("index " + i / 3);

            // verticles of new mesh will be created from UV points of 3D object
            // we need to normalize Y value because it's for texture
            gridVertices[i + 0] = new Vector3(u1.x, 1 - u1.y);
            gridVertices[i + 1] = new Vector3(u2.x, 1 - u2.y);
            gridVertices[i + 2] = new Vector3(u3.x, 1 - u3.y);

            found += assignObjects(mesh3d, objects, u1, u2, u3, i, lookAtAllObjects);

            if (!lookAtAllObjects)
            {
                neighbours[i / 3] = new NeighbourCreator(mesh3d, i, neighbourRadius).create();
            }
        }
        Debug.Log("assigned " + found + " object");
        if (!lookAtAllObjects)
        {
            List <DynamicObject> objectsInN;
            for (int i = 0; i < mesh3d.triangles.Length; i += 3)
            {
                Neighbour n = neighbours[i / 3];
                for (int j = 0; j < n.index.Length; j += 3)
                {
                    objectsInN = textureObjectsOnTriangles[n.index[j] / 3];
                    if (objectsInN != null)
                    {
                        foreach (DynamicObject obj in objectsInN)
                        {
                            n.objects.Add(obj);
                        }
                    }
                }
            }
        }
    }