Beispiel #1
0
 public static void GUI(this MeshMsg message)
 {
     foreach (PointMsg p in message.vertices)
     {
         p.GUI();
     }
     foreach (MeshTriangleMsg tri in message.triangles)
     {
         tri.GUI();
     }
 }
    /// <summary>
    /// Parses jsonMsgs and populates meshDicts
    /// </summary>
    private void ParseJSON()
    {
        while (true)
        {
            // Check if any json msgs have been recieved
            if (jsonMsgs.Count > 0)
            {
                Debug.Log("JSON Message Count: " + jsonMsgs.Count);
                // Parse json msg to mesh msg
                DateTime startTime = DateTime.Now;
                JSONNode rawMsg    = jsonMsgs.Dequeue();
                MeshMsg  meshMsg   = new MeshMsg(rawMsg);
//                meshMsgs.Enqueue(meshMsg);
                Debug.Log("Message Generation: " + DateTime.Now.Subtract(startTime).TotalMilliseconds.ToString() + "ms");
                startTime = DateTime.Now;
                meshDicts.Enqueue(visualizer.generateMesh(meshMsg));
                Debug.Log("Generate MeshArray: " + DateTime.Now.Subtract(startTime).TotalMilliseconds.ToString() + "ms");
            }
        }
    }
    /// <summary>
    /// Generates a dictionary of MeshArrays specified by the message.
    /// </summary>
    /// <param name="meshMsg">Voxblox Mesh message to generate Meshes with.</param>
    /// <returns>A dictionary of MeshArrays.</returns>
    public Dictionary <long[], MeshArray> generateMesh(MeshMsg meshMsg)
    {
        Dictionary <Int64[], MeshArray> generated_mesh_dict = new Dictionary <long[], MeshArray>(new LongArrayEqualityComparer());
        /// The length of one block. Also the scaling factor of the coordinates.
        float scale_factor = meshMsg.GetBlockEdgeLength();

        /// List of all the mesh blocks.
        MeshBlockMsg[] mesh_blocks = meshMsg.GetMeshBlocks();
        /// Iterate through each mesh block generating and updating meshes for each.
        for (int i = 0; i < mesh_blocks.Length; i++)
        {
            /// index of the mesh block.
            Int64[] index = mesh_blocks[i].GetIndex();

            ushort[] x = mesh_blocks[i].GetX();
            ushort[] y = mesh_blocks[i].GetY();
            ushort[] z = mesh_blocks[i].GetZ();

            // Create a list of vertices and their corresponding colors.
            List <Vector3> newVertices = new List <Vector3>();
            List <Color>   newColors   = new List <Color>();

            // update indicies, converting from block index to global position transforms.
            for (int j = 0; j < x.Length; j++)
            {
                float zv = ((float)z[j] / 32768.0f + index[2]) * scale_factor;
                float xv = ((float)x[j] / 32768.0f + index[0]) * scale_factor;
                float yv = ((float)y[j] / 32768.0f + index[1]) * scale_factor;
                if (flipYZ)
                {
                    newVertices.Add(new Vector3(xv, zv, yv));
                }
                else
                {
                    newVertices.Add(new Vector3(xv, yv, zv));
                }
            }
            // update colors
            byte[] r = mesh_blocks[i].GetR();
            byte[] g = mesh_blocks[i].GetG();
            byte[] b = mesh_blocks[i].GetB();

            for (int j = 0; j < r.Length; j++)
            {
                newColors.Add(new Color32(r[j], g[j], b[j], alpha));
            }

            // Vertices come in triples each corresponding to one face.
            int[] newTriangles = new int[newVertices.Count / 3 * 3];
            for (int j = 0; j < newTriangles.Length; j++)
            {
                newTriangles[j] = j;
            }

            // correct for inverted mesh. By reversing the lists, the normal vectors point the right direction.
            newVertices.Reverse();
            newColors.Reverse();

            generated_mesh_dict[index] = new MeshArray(newVertices.ToArray(), newTriangles, newColors.ToArray());
        }
        return(generated_mesh_dict);
    }