DisplayPointCloudByGpu.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class DisplayPointCloudByGpu : MonoBehaviour
  6. {
  7. int numPoints = 60000;
  8. void Start()
  9. {
  10. // 1. 读取数据
  11. // 提前将点云存成txt文件放在Assert/StreamingAssets文件夹下,文本的每行代表一个点,由点的x,y,z,r,g,b六个float组成
  12. //string fileAddress = (Application.streamingAssetsPath + "/" + "test.txt");
  13. //FileInfo fInfo0 = new FileInfo(fileAddress);
  14. //Debug.Log(fileAddress);
  15. //string s = "";
  16. // StreamReader r;
  17. ArrayList arrayListXYZ = new ArrayList();
  18. ArrayList arrayListRGB = new ArrayList();
  19. //if (fInfo0.Exists)
  20. //{
  21. // r = new StreamReader(fileAddress);
  22. //}
  23. //else
  24. //{
  25. // Debug.Log("NO THIS FILE!");
  26. // return;
  27. //}
  28. //// 将文本中的点云数据读入分别存到xyz数组和rgb数组中
  29. //while ((s = r.ReadLine()) != null)
  30. //{
  31. // string[] words = s.Split(" "[0]);
  32. // Vector3 xyz = new Vector3(float.Parse(words[0]), -float.Parse(words[1]), float.Parse(words[2]));
  33. // arrayListXYZ.Add(xyz);
  34. // Color colorRGB = new Color(float.Parse(words[3]) / 255.0f, float.Parse(words[4]) / 255.0f, float.Parse(words[5]) / 255.0f);
  35. // arrayListRGB.Add(colorRGB);
  36. // //Debug.Log(xyz.ToString() + "," + colorRGB.ToString());
  37. //}
  38. for (int j = 0; j < 100; j++)
  39. {
  40. Vector3 xyz = new Vector3(j*0.1f, 0, j*0.1f);
  41. arrayListXYZ.Add(xyz);
  42. Color colorRGB = new Color(255 / 255.0f,0 / 255.0f, 0 / 255.0f);
  43. arrayListRGB.Add(colorRGB);
  44. }
  45. // 2. 渲染
  46. int num = arrayListRGB.Count;
  47. int meshNum = num / numPoints;
  48. int leftPointsNum = num % numPoints;
  49. int i = 0;
  50. for (; i < meshNum; i++)
  51. {
  52. GameObject obj = new GameObject();
  53. obj.name = i.ToString();
  54. obj.AddComponent<MeshFilter>();
  55. obj.AddComponent<MeshRenderer>();
  56. Mesh tempMesh = new Mesh();
  57. CreateMesh(ref tempMesh, ref arrayListXYZ, ref arrayListRGB, i * numPoints, numPoints);
  58. Material material = new Material(Shader.Find("Custom/VertexColor"));
  59. obj.GetComponent<MeshFilter>().mesh = tempMesh;
  60. obj.GetComponent<MeshRenderer>().material = material;
  61. }
  62. GameObject objLeft = new GameObject();
  63. objLeft.name = i.ToString();
  64. //objLeft.transform.position = new Vector3(Random.Range(-10, 10), Random.Range(-10, 10), Random.Range(-10, 10));
  65. objLeft.AddComponent<MeshFilter>();
  66. objLeft.AddComponent<MeshRenderer>();
  67. Mesh tempMeshLeft = new Mesh();
  68. CreateMesh(ref tempMeshLeft, ref arrayListXYZ, ref arrayListRGB, i * numPoints, leftPointsNum);
  69. Material materialLeft = new Material(Shader.Find("Custom/VertexColor"));
  70. objLeft.GetComponent<MeshFilter>().mesh = tempMeshLeft;
  71. objLeft.GetComponent<MeshRenderer>().material = materialLeft;
  72. }
  73. void CreateMesh(ref Mesh mesh, ref ArrayList arrayListXYZ, ref ArrayList arrayListRGB, int beginIndex, int pointsNum)
  74. {
  75. Vector3[] points = new Vector3[pointsNum];
  76. Color[] colors = new Color[pointsNum];
  77. int[] indecies = new int[pointsNum];
  78. for (int i = 0; i < pointsNum; ++i)
  79. {
  80. points[i] = (Vector3)arrayListXYZ[beginIndex + i];
  81. indecies[i] = i;
  82. colors[i] = (Color)arrayListRGB[beginIndex + i];
  83. }
  84. mesh.vertices = points;
  85. mesh.colors = colors;
  86. mesh.SetIndices(indecies, MeshTopology.Points, 0);
  87. }
  88. }