| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Components; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Models |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// (NOTE) Kinerius: this class is a data holder that was made in the past for multiple systems to have access to th |
| | 13 | | /// This has a big architectural issue where if we decide to remove any component (renderer, mesh filter, collider) |
| | 14 | | /// from anywhere within the system, we have to explicitly update this class values, it makes no sense, |
| | 15 | | /// we need to reconsider this class functionality and purpose since it has an ambiguous design and it might be the |
| | 16 | | /// </summary> |
| | 17 | | [Serializable] |
| | 18 | | public class MeshesInfo |
| | 19 | | { |
| | 20 | | public static event Action OnAnyUpdated; |
| | 21 | | public event Action OnUpdated; |
| | 22 | | public event Action OnCleanup; |
| | 23 | |
|
| | 24 | | public GameObject innerGameObject; |
| | 25 | |
|
| | 26 | | public GameObject meshRootGameObject |
| | 27 | | { |
| | 28 | | get |
| | 29 | | { |
| 6377 | 30 | | return meshRootGameObjectValue; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | set |
| | 34 | | { |
| 336 | 35 | | meshRootGameObjectValue = value; |
| 336 | 36 | | UpdateRenderersCollection(); |
| 336 | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| | 40 | | GameObject meshRootGameObjectValue; |
| | 41 | |
|
| 20 | 42 | | public bool RootIsPoolableObject { get; set; } |
| | 43 | |
|
| | 44 | | public IShape currentShape; |
| 25000 | 45 | | public Renderer[] renderers { get; private set; } |
| | 46 | | public MeshFilter[] meshFilters; |
| 311 | 47 | | public HashSet<Collider> colliders = new (); |
| 995 | 48 | | public Animation animation { get; private set; } |
| | 49 | |
|
| | 50 | | Vector3 lastBoundsCalculationPosition; |
| | 51 | | Vector3 lastBoundsCalculationScale; |
| | 52 | | Quaternion lastBoundsCalculationRotation; |
| | 53 | | Bounds mergedBoundsValue; |
| | 54 | |
|
| | 55 | | public Bounds mergedBounds |
| | 56 | | { |
| | 57 | | get |
| | 58 | | { |
| 343 | 59 | | if (meshRootGameObject == null) { RecalculateBounds(); } |
| | 60 | | else |
| | 61 | | { |
| 343 | 62 | | if (meshRootGameObject.transform.position != lastBoundsCalculationPosition) |
| | 63 | | { |
| 38 | 64 | | mergedBoundsValue.center += meshRootGameObject.transform.position - lastBoundsCalculationPositio |
| 38 | 65 | | lastBoundsCalculationPosition = meshRootGameObject.transform.position; |
| | 66 | | } |
| | 67 | |
|
| 343 | 68 | | if (meshRootGameObject.transform.lossyScale != lastBoundsCalculationScale || |
| | 69 | | meshRootGameObject.transform.rotation != lastBoundsCalculationRotation) |
| 142 | 70 | | RecalculateBounds(); |
| | 71 | | } |
| | 72 | |
|
| 343 | 73 | | return mergedBoundsValue; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | set |
| | 77 | | { |
| 0 | 78 | | mergedBoundsValue = value; |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public void UpdateRenderersCollection(Renderer[] renderers, MeshFilter[] meshFilters, Animation animation = null |
| | 83 | | { |
| 0 | 84 | | if (meshRootGameObjectValue != null) |
| | 85 | | { |
| 0 | 86 | | this.renderers = renderers; |
| 0 | 87 | | this.meshFilters = meshFilters; |
| 0 | 88 | | this.animation = animation; |
| | 89 | |
|
| 0 | 90 | | RecalculateBounds(); |
| 0 | 91 | | OnAnyUpdated?.Invoke(); |
| 0 | 92 | | OnUpdated?.Invoke(); |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void UpdateRenderersCollection() |
| | 97 | | { |
| 891 | 98 | | if (meshRootGameObjectValue == null) |
| 200 | 99 | | return; |
| | 100 | |
|
| 691 | 101 | | renderers = meshRootGameObjectValue.GetComponentsInChildren<Renderer>(true); |
| 691 | 102 | | meshFilters = meshRootGameObjectValue.GetComponentsInChildren<MeshFilter>(true); |
| 691 | 103 | | animation = meshRootGameObjectValue.GetComponentInChildren<Animation>(); |
| | 104 | |
|
| 691 | 105 | | TextMeshPro[] tmpros = meshRootGameObjectValue.GetComponentsInChildren<TextMeshPro>(true); |
| | 106 | |
|
| 691 | 107 | | if (tmpros.Length > 0) |
| | 108 | | { |
| 52 | 109 | | renderers = renderers.Union(tmpros.Select(x => x.renderer)).ToArray(); |
| 52 | 110 | | meshFilters = meshFilters.Union(tmpros.Select(x => x.meshFilter)).ToArray(); |
| | 111 | | } |
| | 112 | |
|
| 691 | 113 | | RecalculateBounds(); |
| 691 | 114 | | OnAnyUpdated?.Invoke(); |
| 691 | 115 | | OnUpdated?.Invoke(); |
| 686 | 116 | | } |
| | 117 | |
|
| | 118 | | public async UniTask RecalculateBounds() |
| | 119 | | { |
| 833 | 120 | | if ((renderers == null || renderers.Length == 0) && colliders.Count == 0) |
| | 121 | | { |
| 256 | 122 | | mergedBoundsValue = new Bounds(); |
| 256 | 123 | | return; |
| | 124 | | } |
| | 125 | |
|
| 1731 | 126 | | await UniTask.WaitForFixedUpdate(); |
| | 127 | |
|
| 798 | 128 | | if (meshRootGameObjectValue == null) return; |
| | 129 | |
|
| 356 | 130 | | lastBoundsCalculationPosition = meshRootGameObjectValue.transform.position; |
| 356 | 131 | | lastBoundsCalculationScale = meshRootGameObjectValue.transform.lossyScale; |
| 356 | 132 | | lastBoundsCalculationRotation = meshRootGameObjectValue.transform.rotation; |
| | 133 | |
|
| 356 | 134 | | mergedBoundsValue = MeshesInfoUtils.BuildMergedBounds(renderers, colliders); |
| 833 | 135 | | } |
| | 136 | |
|
| | 137 | | public void CleanReferences() |
| | 138 | | { |
| 202 | 139 | | OnCleanup?.Invoke(); |
| 202 | 140 | | meshRootGameObjectValue = null; |
| 202 | 141 | | animation = null; |
| 202 | 142 | | currentShape = null; |
| 202 | 143 | | renderers = null; |
| 202 | 144 | | colliders.Clear(); |
| | 145 | |
|
| 202 | 146 | | var arrayLength = meshFilters.Length; |
| 888 | 147 | | for (int i = 0; i < arrayLength; i++) |
| | 148 | | { |
| 242 | 149 | | meshFilters[i] = null; |
| | 150 | | } |
| 202 | 151 | | innerGameObject = null; |
| | 152 | |
|
| 202 | 153 | | OnCleanup = null; |
| 202 | 154 | | } |
| | 155 | |
|
| | 156 | | public void UpdateExistingMeshAtIndex(Mesh mesh, uint meshFilterIndex = 0) |
| | 157 | | { |
| 3 | 158 | | if (meshFilters != null && meshFilters.Length > meshFilterIndex) |
| | 159 | | { |
| 3 | 160 | | meshFilters[meshFilterIndex].sharedMesh = mesh; |
| 3 | 161 | | OnUpdated?.Invoke(); |
| | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 0 | 165 | | Debug.LogError( |
| | 166 | | $"MeshFilter index {meshFilterIndex} out of bounds - MeshesInfo.UpdateExistingMesh failed"); |
| | 167 | | } |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | public void OverrideRenderers(Renderer[] renderers) |
| | 171 | | { |
| 380 | 172 | | this.renderers = renderers.Where(r => r != null).ToArray(); |
| 190 | 173 | | } |
| | 174 | | } |
| | 175 | | } |