在Unity中实现点云的效果 | 您所在的位置:网站首页 › unity云渲染不能交互 › 在Unity中实现点云的效果 |
介绍
在unity中实现点云的效果可以丰富我们的场景,并且可以与kinect等rgbd相机结合,增加互动。 如果要在unity中实现点云的效果这里有两种方法,一种是用自带的粒子系统,将每个点云中的点对应于每个粒子,并把对应的坐标,颜色赋值给粒子。第二种是用游戏物体的MeshFilter组件,只渲染顶点而不Mesh,再重写shader,赋予每个顶点颜色。 每种方法各有利弊,下面是实现过程: 1. 利用Unity中的粒子系统
VertexColor.shader Shader "Custom/VertexColor" { SubShader { Pass { LOD 200 CGPROGRAM #pragma vertex vert #pragma fragment frag struct VertexInput { float4 v : POSITION; float4 color: COLOR; }; struct VertexOutput { float4 pos : SV_POSITION; float4 col : COLOR; }; VertexOutput vert(VertexInput v) { VertexOutput o; o.pos = mul(UNITY_MATRIX_MVP, v.v); o.col = v.color; return o; } float4 frag(VertexOutput o) : COLOR { return o.col; } ENDCG } } } using UnityEngine; using System.Collections; using System.IO; 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()); } // 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(); obj.AddComponent(); Mesh tempMesh = new Mesh(); CreateMesh(ref tempMesh, ref arrayListXYZ, ref arrayListRGB, i * numPoints, numPoints); Material material = new Material(Shader.Find("Custom/VertexColor")); obj.GetComponent().mesh = tempMesh; obj.GetComponent().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(); objLeft.AddComponent(); Mesh tempMeshLeft = new Mesh(); CreateMesh(ref tempMeshLeft, ref arrayListXYZ, ref arrayListRGB, i * numPoints, leftPointsNum); Material materialLeft = new Material(Shader.Find("Custom/VertexColor")); objLeft.GetComponent().mesh = tempMeshLeft; objLeft.GetComponent().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); } } 总结利用粒子系统可以模拟出点云的效果,并且可以调整点的大小和形状,但是点一多起来就很很难渲染。而利用Mesh直接画点,不能调整大小,但是可以流畅得显示很多点。 |
CopyRight 2018-2019 实验室设备网 版权所有 |