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