PointCloudRenderer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. namespace Immersal.Samples.Util
  10. {
  11. public class PointCloudRenderer : MonoBehaviour
  12. {
  13. private Material mat;
  14. private Mesh mesh;
  15. [HideInInspector]
  16. public GameObject go;
  17. [HideInInspector]
  18. public int mapHandle;
  19. public static bool visible = true;
  20. private Color[] m_PointcloudColors = new Color[] { new Color(0.22f, 1f, 0.46f),
  21. new Color(0.96f, 0.14f, 0.14f),
  22. new Color(0.16f, 0.69f, 0.95f),
  23. new Color(0.93f, 0.84f, 0.12f),
  24. new Color(0.57f, 0.93f, 0.12f),
  25. new Color(1f, 0.38f, 0.78f),
  26. new Color(0.4f, 0f, 0.9f),
  27. new Color(0.89f, 0.4f, 0f)
  28. };
  29. public void Init()
  30. {
  31. if (go == null)
  32. {
  33. mat = new Material(Shader.Find("Immersal/pointcloud3d"));
  34. go = new GameObject("meshcontainer");
  35. mesh = new Mesh();
  36. go.hideFlags = HideFlags.HideAndDontSave;
  37. go.transform.SetParent(gameObject.transform, false);
  38. MeshRenderer mr = go.AddComponent<MeshRenderer>();
  39. go.AddComponent<MeshFilter>().mesh = mesh;
  40. mr.material = mat;
  41. if (Application.isEditor)
  42. mr.material.EnableKeyword("IN_EDITOR");
  43. }
  44. }
  45. // Use this for initialization
  46. void Awake()
  47. {
  48. Init();
  49. go.SetActive(false);
  50. }
  51. void Update()
  52. {
  53. go.SetActive(visible);
  54. }
  55. public void CreateCloud(Vector3[] points, int totalPoints, Matrix4x4 offset)
  56. {
  57. const int max_vertices = 65535;
  58. int numPoints = totalPoints >= max_vertices ? max_vertices : totalPoints;
  59. Color32 fix_col = m_PointcloudColors[Random.Range(0, m_PointcloudColors.Length)];
  60. int[] indices = new int[numPoints];
  61. Vector3[] pts = new Vector3[numPoints];
  62. Color32[] col = new Color32[numPoints];
  63. for (int i = 0; i < numPoints; i++)
  64. {
  65. indices[i] = i;
  66. pts[i] = offset.MultiplyPoint3x4(points[i]);
  67. col[i] = fix_col;
  68. }
  69. mesh.Clear();
  70. mesh.vertices = pts;
  71. mesh.colors32 = col;
  72. mesh.SetIndices(indices, MeshTopology.Points, 0);
  73. mesh.bounds = new Bounds(transform.position, new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  74. }
  75. public void CreateCloud(Vector3[] points, int totalPoints)
  76. {
  77. CreateCloud(points, totalPoints, Matrix4x4.identity);
  78. }
  79. public void ClearCloud()
  80. {
  81. mesh.Clear();
  82. if (!Application.isPlaying)
  83. {
  84. DestroyImmediate(go);
  85. }
  86. else
  87. Destroy(this);
  88. }
  89. }
  90. }