NRPointCloudVisualizer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.Persistence
  10. {
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using UnityEngine;
  14. using System;
  15. public class NRPointCloudVisualizer : IPointCloudDrawer
  16. {
  17. public Dictionary<Int64, PointCloudPoint> Points = new Dictionary<Int64, PointCloudPoint>();
  18. private Mesh m_Mesh;
  19. private GameObject visualizer;
  20. public static Dictionary<ConfidenceLevel, Color> ConfidenceColorMap = new Dictionary<ConfidenceLevel, Color>() {
  21. { ConfidenceLevel.Nice,new Color(1,0,0)},
  22. { ConfidenceLevel.Good,new Color(1,0,0.4f)},
  23. { ConfidenceLevel.Normal,new Color(1,0,0.8f)},
  24. { ConfidenceLevel.Poor,new Color(0.8f,0,1)},
  25. { ConfidenceLevel.Bad,new Color(0.4f,0,1)},
  26. { ConfidenceLevel.VeryBad,new Color(0,0,1)},
  27. };
  28. public NRPointCloudVisualizer()
  29. {
  30. visualizer = new GameObject("PointCloudVisualizer");
  31. m_Mesh = new Mesh();
  32. visualizer.AddComponent<MeshFilter>().mesh = m_Mesh;
  33. visualizer.AddComponent<MeshRenderer>().material = new Material(Resources.Load<Shader>("VertexColor"));
  34. }
  35. public void Draw()
  36. {
  37. PointCloudPoint[] pointlist = Points.Values.ToArray<PointCloudPoint>();
  38. Vector3[] points = new Vector3[pointlist.Length];
  39. int[] indecies = new int[pointlist.Length];
  40. Color[] colors = new Color[pointlist.Length];
  41. for (int i = 0; i < points.Length; ++i)
  42. {
  43. var pos = pointlist[i].Position;
  44. points[i] = new Vector3(pos.X, pos.Y, pos.Z);
  45. indecies[i] = i;
  46. colors[i] = ConfidenceColorMap[pointlist[i].confidenceLevel];
  47. }
  48. m_Mesh.vertices = points;
  49. m_Mesh.colors = colors;
  50. m_Mesh.SetIndices(indecies, MeshTopology.Points, 0);
  51. }
  52. public void AdjustPointSize(float size)
  53. {
  54. visualizer.GetComponent<MeshRenderer>().material.SetFloat("_PointSize", size);
  55. }
  56. public void Update(PointCloudPoint point)
  57. {
  58. Points[point.Id] = point;
  59. }
  60. }
  61. }