| | 1 | | using System.Collections; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | | using Object = UnityEngine.Object; |
| | 5 | |
|
| | 6 | | namespace DCL.Rendering |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// This class is used for tracking all the renderers, skinnedMeshRenderers and Animations of the world. |
| | 10 | | /// |
| | 11 | | /// It currently uses a very lazy FindObjectsOfType approach, but is enough for its purposes as its used |
| | 12 | | /// to optimize a bigger bottleneck. |
| | 13 | | /// </summary> |
| | 14 | | public class CullingObjectsTracker : ICullingObjectsTracker |
| | 15 | | { |
| | 16 | | Renderer[] renderers; |
| | 17 | | SkinnedMeshRenderer[] skinnedRenderers; |
| | 18 | | Animation[] animations; |
| | 19 | |
|
| 67 | 20 | | bool dirty = true; |
| | 21 | | private int ignoredLayersMask; |
| | 22 | |
|
| 67 | 23 | | public CullingObjectsTracker () |
| | 24 | | { |
| 67 | 25 | | PoolManager.i.OnGet -= MarkDirty; |
| 67 | 26 | | PoolManager.i.OnGet += MarkDirty; |
| 67 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// If the dirty flag is true, this coroutine will re-populate all the tracked objects. |
| | 31 | | /// </summary> |
| | 32 | | public IEnumerator PopulateRenderersList() |
| | 33 | | { |
| 16 | 34 | | if (!dirty) |
| 1 | 35 | | yield break; |
| | 36 | |
|
| 15 | 37 | | renderers = Object.FindObjectsOfType<Renderer>() |
| 25 | 38 | | .Where(x => !(x is SkinnedMeshRenderer) |
| | 39 | | && !(x is ParticleSystemRenderer) |
| | 40 | | && ((1 << x.gameObject.layer) & ignoredLayersMask) == 0) |
| | 41 | | .ToArray(); |
| | 42 | |
|
| 15 | 43 | | yield return null; |
| | 44 | |
|
| 11 | 45 | | skinnedRenderers = Object.FindObjectsOfType<SkinnedMeshRenderer>() |
| 7 | 46 | | .Where(x => ((1 << x.gameObject.layer) & ignoredLayersMask) == 0) |
| | 47 | | .ToArray(); |
| | 48 | |
|
| 11 | 49 | | yield return null; |
| | 50 | |
|
| 10 | 51 | | animations = Object.FindObjectsOfType<Animation>(); |
| | 52 | |
|
| 10 | 53 | | dirty = false; |
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// This will re-populate all the tracked objects in a sync way. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="includeInactives">True for add inactive objects to the tracked list.</param> |
| | 60 | | public void ForcePopulateRenderersList(bool includeInactives) |
| | 61 | | { |
| 61 | 62 | | renderers = Object.FindObjectsOfType<Renderer>(includeInactives) |
| 258 | 63 | | .Where(x => !(x is SkinnedMeshRenderer) |
| | 64 | | && !(x is ParticleSystemRenderer) |
| | 65 | | && ((1 << x.gameObject.layer) & ignoredLayersMask) == 0) |
| | 66 | | .ToArray(); |
| | 67 | |
|
| 61 | 68 | | skinnedRenderers = Object.FindObjectsOfType<SkinnedMeshRenderer>(includeInactives) |
| 0 | 69 | | .Where(x => ((1 << x.gameObject.layer) & ignoredLayersMask) == 0) |
| | 70 | | .ToArray(); |
| 61 | 71 | | } |
| | 72 | |
|
| 162 | 73 | | public void SetIgnoredLayersMask(int ignoredLayersMask) { this.ignoredLayersMask = ignoredLayersMask; } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Sets the dirty flag to true to make PopulateRenderersList retrieve all the scene objects on its next call. |
| | 77 | | /// </summary> |
| 2758 | 78 | | public void MarkDirty() { dirty = true; } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Returns true if dirty. |
| | 82 | | /// </summary> |
| | 83 | | /// <returns>True if dirty.</returns> |
| 3 | 84 | | public bool IsDirty() { return dirty; } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Returns the renderers list. |
| | 88 | | /// </summary> |
| | 89 | | /// <returns>An array with all the tracked renderers.</returns> |
| 72 | 90 | | public Renderer[] GetRenderers() { return renderers; } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Returns the skinned renderers list. |
| | 94 | | /// </summary> |
| | 95 | | /// <returns>An array with all the tracked skinned renderers.</returns> |
| 72 | 96 | | public SkinnedMeshRenderer[] GetSkinnedRenderers() { return skinnedRenderers; } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Returns the animations list. |
| | 100 | | /// </summary> |
| | 101 | | /// <returns>An array with all the tracked animations.</returns> |
| 66 | 102 | | public Animation[] GetAnimations() { return animations; } |
| | 103 | |
|
| | 104 | | public void Dispose() |
| | 105 | | { |
| 63 | 106 | | dirty = true; |
| 63 | 107 | | renderers = null; |
| 63 | 108 | | animations = null; |
| 63 | 109 | | skinnedRenderers = null; |
| 63 | 110 | | PoolManager.i.OnGet -= MarkDirty; |
| 63 | 111 | | } |
| | 112 | | } |
| | 113 | | } |