I'm trying to upload point cloud in unity C#. Because there is no element that draws point directly, I create a cube for each point of point cloud.
So, For n points in point cloud, 8n vertices (because it is cube), 12 meshes exist for my code.
- A mesh = 3 index --> a cube : 12 meshes --> 3*12 index (var name : triangles)
- point XYZ are saved in PointList : List < Vector3 >
- each points has color in ColorList : List< Vector3 >
I checked all vertices and meshes have correct values, and drawing points (cubes) works well.
However there is a problem. The original point cloud has 1412765 points(image1). And the number of vertices is same to it. But! If I try to draw all of them, maybe some of them are not drawn. (image2. I guess around 10000 cubes is the maximum.)
float resolution = 2.5f;
int pointSize = PointList.Count;
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
Vector3[] vertices;
vertices = new Vector3[pointSize * 8];
for (int i = 0; i < pointSize; i++)
{
float x = PointList[i].x;
float y = PointList[i].y;
float z = PointList[i].z;
vertices[i * 8 + 0] = new Vector3(x - resolution, y - resolution, z - resolution);
vertices[i * 8 + 1] = new Vector3(x + resolution, y - resolution, z - resolution);
vertices[i * 8 + 2] = new Vector3(x + resolution, y + resolution, z - resolution);
vertices[i * 8 + 3] = new Vector3(x - resolution, y + resolution, z - resolution);
vertices[i * 8 + 4] = new Vector3(x - resolution, y + resolution, z + resolution);
vertices[i * 8 + 5] = new Vector3(x + resolution, y + resolution, z + resolution);
vertices[i * 8 + 6] = new Vector3(x + resolution, y - resolution, z + resolution);
vertices[i * 8 + 7] = new Vector3(x - resolution, y - resolution, z + resolution);
}
mesh.vertices = vertices;
int[] triangles;
triangles = new int[pointSize * 12 * 3];
triangles[0] = 0;
triangles[1] = 2;
triangles[2] = 1;
triangles[3] = 0;
triangles[4] = 3;
triangles[5] = 2;
triangles[6] = 2;
triangles[7] = 3;
triangles[8] = 4;
triangles[9] = 2;
triangles[10] = 4;
triangles[11] = 5;
triangles[12] = 1;
triangles[13] = 2;
triangles[14] = 5;
triangles[15] = 1;
triangles[16] = 5;
triangles[17] = 6;
triangles[18] = 0;
triangles[19] = 7;
triangles[20] = 4;
triangles[21] = 0;
triangles[22] = 4;
triangles[23] = 3;
triangles[24] = 5;
triangles[25] = 4;
triangles[26] = 7;
triangles[27] = 5;
triangles[28] = 7;
triangles[29] = 6;
triangles[30] = 0;
triangles[31] = 6;
triangles[32] = 7;
triangles[33] = 0;
triangles[34] = 1;
triangles[35] = 6;
for (int i = 1; i < pointSize; i++)
{
for (int j = 0; j < 36; j++)
{
int val = triangles[j] + 8 * i;
triangles[i * 36 + j] = triangles[j] + 8 * i;
}
}
mesh.triangles = triangles;
Color32[] colors = new Color32[mesh.vertices.Length];
for (int i = 0; i < pointSize; i++)
{
for (int j = 0; j < 8; j++)
{
Color32 newColor = new Color32(
(byte)ColorList[i].x,
(byte)ColorList[i].y,
(byte)ColorList[i].z,
255);
colors[i * 8 + j] = newColor;
}
}
mesh.colors32 = colors;
Debug.Log("points : " + PointList.Count);
Debug.Log("vertices (point*8): " + mesh.vertices.Length);
Debug.Log("triangles (point*36): " + mesh.triangles.Length);
Debug.Log("colors (points*8): " + mesh.colors32.Length);
}