PointCloudRenderer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Pcx - Point cloud importer & renderer for Unity
  2. // https://github.com/keijiro/Pcx
  3. using UnityEngine;
  4. namespace TriLibCore.Samples
  5. {
  6. /// A renderer class that renders a point cloud contained by PointCloudData.
  7. [ExecuteInEditMode]
  8. public sealed class PointCloudRenderer : MonoBehaviour
  9. {
  10. #region Editable attributes
  11. [SerializeField] PointCloudData _sourceData = null;
  12. [SerializeField] private bool _destroyData;
  13. public PointCloudData sourceData {
  14. get { return _sourceData; }
  15. set { _sourceData = value; }
  16. }
  17. [SerializeField] Color _pointTint = new Color(0.5f, 0.5f, 0.5f, 1);
  18. public Color pointTint {
  19. get { return _pointTint; }
  20. set { _pointTint = value; }
  21. }
  22. [SerializeField] float _pointSize = 0.05f;
  23. public float pointSize {
  24. get { return _pointSize; }
  25. set { _pointSize = value; }
  26. }
  27. public bool destroyData
  28. {
  29. get
  30. {
  31. return _destroyData;
  32. }
  33. set
  34. {
  35. _destroyData = value;
  36. }
  37. }
  38. #endregion
  39. #region Public properties (nonserialized)
  40. public ComputeBuffer sourceBuffer { get; set; }
  41. #endregion
  42. #region Internal resources
  43. [SerializeField, HideInInspector] Shader _pointShader = null;
  44. [SerializeField, HideInInspector] Shader _diskShader = null;
  45. #endregion
  46. #region Private objects
  47. Material _pointMaterial;
  48. Material _diskMaterial;
  49. #endregion
  50. #region MonoBehaviour implementation
  51. void OnValidate()
  52. {
  53. _pointSize = Mathf.Max(0, _pointSize);
  54. }
  55. void OnDestroy()
  56. {
  57. if (_pointMaterial != null)
  58. {
  59. if (Application.isPlaying)
  60. {
  61. Destroy(_pointMaterial);
  62. Destroy(_diskMaterial);
  63. if (_destroyData && sourceData != null)
  64. {
  65. Destroy(sourceData);
  66. }
  67. }
  68. else
  69. {
  70. DestroyImmediate(_pointMaterial);
  71. DestroyImmediate(_diskMaterial);
  72. if (_destroyData && sourceData != null)
  73. {
  74. DestroyImmediate(sourceData);
  75. }
  76. }
  77. }
  78. }
  79. void OnRenderObject()
  80. {
  81. // We need a source data or an externally given buffer.
  82. if (_sourceData == null && sourceBuffer == null) return;
  83. // Check the camera condition.
  84. var camera = Camera.current;
  85. if ((camera.cullingMask & (1 << gameObject.layer)) == 0) return;
  86. if (camera.name == "Preview Scene Camera") return;
  87. // TODO: Do view frustum culling here.
  88. // Lazy initialization
  89. if (_pointMaterial == null)
  90. {
  91. _pointShader = Shader.Find("Point Cloud/Point");
  92. _pointMaterial = new Material(_pointShader);
  93. _pointMaterial.hideFlags = HideFlags.DontSave;
  94. _pointMaterial.EnableKeyword("_COMPUTE_BUFFER");
  95. _diskShader = Shader.Find("Point Cloud/Disk");
  96. _diskMaterial = new Material(_diskShader);
  97. _diskMaterial.hideFlags = HideFlags.DontSave;
  98. _diskMaterial.EnableKeyword("_COMPUTE_BUFFER");
  99. }
  100. // Use the external buffer if given any.
  101. var pointBuffer = sourceBuffer != null ?
  102. sourceBuffer : _sourceData.computeBuffer;
  103. if (_pointSize == 0)
  104. {
  105. _pointMaterial.SetPass(0);
  106. _pointMaterial.SetColor("_Tint", _pointTint);
  107. _pointMaterial.SetMatrix("_Transform", transform.localToWorldMatrix);
  108. _pointMaterial.SetBuffer("_PointBuffer", pointBuffer);
  109. Graphics.DrawProceduralNow(MeshTopology.Points, pointBuffer.count, 1);
  110. }
  111. else
  112. {
  113. _diskMaterial.SetPass(0);
  114. _diskMaterial.SetColor("_Tint", _pointTint);
  115. _diskMaterial.SetMatrix("_Transform", transform.localToWorldMatrix);
  116. _diskMaterial.SetBuffer("_PointBuffer", pointBuffer);
  117. _diskMaterial.SetFloat("_PointSize", pointSize);
  118. Graphics.DrawProceduralNow(MeshTopology.Points, pointBuffer.count, 1);
  119. }
  120. }
  121. #endregion
  122. }
  123. }