BakedPointCloud.cs 2.1 KB

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