PointCloudMeshScript.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 UnityEngine;
  12. /// <summary>
  13. /// Point cloud mesh script.
  14. /// Create random points and put it in a Mesh with Point Topology. The color depend of the vertical (Y) position of the point.
  15. /// </summary>
  16. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
  17. public class PointCloudMeshScript : MonoBehaviour
  18. {
  19. private Mesh mesh;
  20. int numPoints = 60000;
  21. // Use this for initialization
  22. void Start()
  23. {
  24. mesh = new Mesh();
  25. GetComponent<MeshFilter>().mesh = mesh;
  26. GetComponent<MeshRenderer>().material = new Material(Shader.Find("Custom/VertexColor"));
  27. CreateMesh();
  28. }
  29. void CreateMesh()
  30. {
  31. Vector3[] points = new Vector3[numPoints];
  32. int[] indecies = new int[numPoints];
  33. Color[] colors = new Color[numPoints];
  34. int max = 10;
  35. int min = -10;
  36. for (int i = 0; i < points.Length; ++i)
  37. {
  38. int x = Random.Range(min, max);
  39. int y = Random.Range(min, max);
  40. int z = Random.Range(min, max);
  41. points[i] = new Vector3(x, y, z);
  42. indecies[i] = i;
  43. float value = (float)1.0 * (((float)y - (float)min) / ((float)max - (float)min));
  44. colors[i] = new Color(value, value, value, 1.0f);
  45. }
  46. mesh.vertices = points;
  47. mesh.colors = colors;
  48. mesh.SetIndices(indecies, MeshTopology.Points, 0);
  49. }
  50. }
  51. }