1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class DisplayPointCloudByGpu : MonoBehaviour
- {
- int numPoints = 60000;
- void Start()
- {
- // 1. 读取数据
- // 提前将点云存成txt文件放在Assert/StreamingAssets文件夹下,文本的每行代表一个点,由点的x,y,z,r,g,b六个float组成
- //string fileAddress = (Application.streamingAssetsPath + "/" + "test.txt");
- //FileInfo fInfo0 = new FileInfo(fileAddress);
- //Debug.Log(fileAddress);
- //string s = "";
- // StreamReader r;
- ArrayList arrayListXYZ = new ArrayList();
- ArrayList arrayListRGB = new ArrayList();
- //if (fInfo0.Exists)
- //{
- // r = new StreamReader(fileAddress);
- //}
- //else
- //{
- // Debug.Log("NO THIS FILE!");
- // return;
- //}
- //// 将文本中的点云数据读入分别存到xyz数组和rgb数组中
- //while ((s = r.ReadLine()) != null)
- //{
- // string[] words = s.Split(" "[0]);
- // Vector3 xyz = new Vector3(float.Parse(words[0]), -float.Parse(words[1]), float.Parse(words[2]));
- // arrayListXYZ.Add(xyz);
- // Color colorRGB = new Color(float.Parse(words[3]) / 255.0f, float.Parse(words[4]) / 255.0f, float.Parse(words[5]) / 255.0f);
- // arrayListRGB.Add(colorRGB);
- // //Debug.Log(xyz.ToString() + "," + colorRGB.ToString());
- //}
- for (int j = 0; j < 100; j++)
- {
- Vector3 xyz = new Vector3(j*0.1f, 0, j*0.1f);
- arrayListXYZ.Add(xyz);
- Color colorRGB = new Color(255 / 255.0f,0 / 255.0f, 0 / 255.0f);
- arrayListRGB.Add(colorRGB);
- }
- // 2. 渲染
- int num = arrayListRGB.Count;
- int meshNum = num / numPoints;
- int leftPointsNum = num % numPoints;
- int i = 0;
- for (; i < meshNum; i++)
- {
- GameObject obj = new GameObject();
- obj.name = i.ToString();
- obj.AddComponent<MeshFilter>();
- obj.AddComponent<MeshRenderer>();
- Mesh tempMesh = new Mesh();
- CreateMesh(ref tempMesh, ref arrayListXYZ, ref arrayListRGB, i * numPoints, numPoints);
- Material material = new Material(Shader.Find("Custom/VertexColor"));
- obj.GetComponent<MeshFilter>().mesh = tempMesh;
- obj.GetComponent<MeshRenderer>().material = material;
- }
- GameObject objLeft = new GameObject();
- objLeft.name = i.ToString();
- //objLeft.transform.position = new Vector3(Random.Range(-10, 10), Random.Range(-10, 10), Random.Range(-10, 10));
- objLeft.AddComponent<MeshFilter>();
- objLeft.AddComponent<MeshRenderer>();
- Mesh tempMeshLeft = new Mesh();
- CreateMesh(ref tempMeshLeft, ref arrayListXYZ, ref arrayListRGB, i * numPoints, leftPointsNum);
- Material materialLeft = new Material(Shader.Find("Custom/VertexColor"));
- objLeft.GetComponent<MeshFilter>().mesh = tempMeshLeft;
- objLeft.GetComponent<MeshRenderer>().material = materialLeft;
- }
- void CreateMesh(ref Mesh mesh, ref ArrayList arrayListXYZ, ref ArrayList arrayListRGB, int beginIndex, int pointsNum)
- {
- Vector3[] points = new Vector3[pointsNum];
- Color[] colors = new Color[pointsNum];
- int[] indecies = new int[pointsNum];
- for (int i = 0; i < pointsNum; ++i)
- {
- points[i] = (Vector3)arrayListXYZ[beginIndex + i];
- indecies[i] = i;
- colors[i] = (Color)arrayListRGB[beginIndex + i];
- }
- mesh.vertices = points;
- mesh.colors = colors;
- mesh.SetIndices(indecies, MeshTopology.Points, 0);
- }
- }
|