BakedPointCloud.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma warning disable 649
  2. // Pcx - Point cloud importer & renderer for Unity
  3. // https://github.com/keijiro/Pcx
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. namespace TriLibCore.Samples
  7. {
  8. /// A container class for texture-baked point clouds.
  9. public sealed class BakedPointCloud : ScriptableObject
  10. {
  11. #region Public properties
  12. /// Number of points
  13. public int pointCount { get { return _pointCount; } }
  14. /// Position map texture
  15. public Texture2D positionMap { get { return _positionMap; } }
  16. /// Color map texture
  17. public Texture2D colorMap { get { return _colorMap; } }
  18. #endregion
  19. #region Serialized data members
  20. [SerializeField] int _pointCount;
  21. [SerializeField] Texture2D _positionMap;
  22. [SerializeField] Texture2D _colorMap;
  23. #endregion
  24. #region Editor functions
  25. #if UNITY_EDITOR
  26. public void Initialize(IList<Vector3> positions, IList<Color32> colors)
  27. {
  28. _pointCount = positions.Count;
  29. var width = Mathf.CeilToInt(Mathf.Sqrt(_pointCount));
  30. _positionMap = new Texture2D(width, width, TextureFormat.RGBAHalf, false);
  31. _positionMap.name = "Position Map";
  32. _positionMap.filterMode = FilterMode.Point;
  33. _colorMap = new Texture2D(width, width, TextureFormat.RGBA32, false);
  34. _colorMap.name = "Color Map";
  35. _colorMap.filterMode = FilterMode.Point;
  36. var i1 = 0;
  37. var i2 = 0U;
  38. for (var y = 0; y < width; y++)
  39. {
  40. for (var x = 0; x < width; x++)
  41. {
  42. var i = i1 < _pointCount ? i1 : (int)(i2 % _pointCount);
  43. var p = positions[i];
  44. _positionMap.SetPixel(x, y, new Color(p.x, p.y, p.z));
  45. _colorMap.SetPixel(x, y, colors[i]);
  46. i1 ++;
  47. i2 += 132049U; // prime
  48. }
  49. }
  50. _positionMap.Apply(false, true);
  51. _colorMap.Apply(false, true);
  52. }
  53. #endif
  54. #endregion
  55. }
  56. }