SceneViewCamera.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace O3DWB
  5. {
  6. public class SceneViewCamera : Singleton<SceneViewCamera>
  7. {
  8. #region Private Variables
  9. /// <summary>
  10. /// This holds the camera's far clip plane. This value is not used to modify the
  11. /// actual clip plane value of the camera, but it will be used when constructing
  12. /// the camera view volume.
  13. /// </summary>
  14. private const float _farClipPlane = 1100.0f;
  15. private CameraViewVolume _viewVolume = new CameraViewVolume();
  16. private CameraDataSnapshot _cameraDataSnapshot;
  17. private List<GameObject> _visibleGameObjects = new List<GameObject>();
  18. private bool _objectVisibilityIsDirty = true;
  19. #endregion
  20. #region Public Static Properties
  21. public static Camera Camera { get { return Camera.current; } }
  22. #endregion
  23. #region Public Methods
  24. public void SetObjectVisibilityDirty()
  25. {
  26. _objectVisibilityIsDirty = true;
  27. }
  28. public CameraViewVolume GetViewVolume()
  29. {
  30. if (HasViewVolumeChanged())
  31. {
  32. _viewVolume.BuildForCamera(Camera, _farClipPlane);
  33. _objectVisibilityIsDirty = true;
  34. }
  35. return _viewVolume;
  36. }
  37. public bool IsGameObjectVisible(GameObject gameObject)
  38. {
  39. Box worldBox = gameObject.GetWorldBox();
  40. if (worldBox.IsInvalid()) return false;
  41. return GetViewVolume().ContainsWorldSpaceAABB(worldBox.ToBounds());
  42. }
  43. public bool IsGameObjectHierarchyVisible(GameObject root)
  44. {
  45. Box worldBox = root.GetHierarchyWorldBox();
  46. if (worldBox.IsInvalid()) return false;
  47. return GetViewVolume().ContainsWorldSpaceAABB(worldBox.ToBounds());
  48. }
  49. public List<GameObject> GetVisibleGameObjects()
  50. {
  51. if (_objectVisibilityIsDirty)
  52. {
  53. List<GameObject> potentiallyVisibleGameObjects = GetPotentiallyVisibleGameObjects();
  54. if (potentiallyVisibleGameObjects.Count == 0) _visibleGameObjects = new List<GameObject>();
  55. else
  56. {
  57. _visibleGameObjects = new List<GameObject>(potentiallyVisibleGameObjects.Count);
  58. foreach (GameObject gameObject in potentiallyVisibleGameObjects)
  59. {
  60. if (IsGameObjectVisible(gameObject)) _visibleGameObjects.Add(gameObject);
  61. }
  62. }
  63. _objectVisibilityIsDirty = false;
  64. }
  65. return _visibleGameObjects.FindAll(item => item != null);
  66. }
  67. #endregion
  68. #region Private Methods
  69. private bool HasViewVolumeChanged()
  70. {
  71. return EnsureDataSnapshotIsUpToDate();
  72. }
  73. private bool EnsureDataSnapshotIsUpToDate()
  74. {
  75. // If the snapshot data is null, we have to create it
  76. if (_cameraDataSnapshot == null)
  77. {
  78. _cameraDataSnapshot = new CameraDataSnapshot();
  79. _cameraDataSnapshot.TakeSnapshot(Camera);
  80. return true;
  81. }
  82. else
  83. {
  84. // Build a new camera data snapshot instance and check if it is different
  85. // from the current one. If it is, we will update the data snapshot reference.
  86. CameraDataSnapshot newCameraDataSnapshot = new CameraDataSnapshot();
  87. newCameraDataSnapshot.TakeSnapshot(Camera);
  88. if (newCameraDataSnapshot != _cameraDataSnapshot)
  89. {
  90. _cameraDataSnapshot = newCameraDataSnapshot;
  91. return true;
  92. }
  93. else return false;
  94. }
  95. }
  96. private List<GameObject> GetPotentiallyVisibleGameObjects()
  97. {
  98. var visibilitySphere = new CameraPotentialVisibleObjectsSphere();
  99. visibilitySphere.Calculate(Camera, GetViewVolume());
  100. return Octave3DScene.Get().OverlapSphere(visibilitySphere.Sphere);
  101. }
  102. #endregion
  103. }
  104. }
  105. #endif