| | 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 | | [Serializable] |
| | 11 | | public class MeshesInfo |
| | 12 | | { |
| | 13 | | public static event Action OnAnyUpdated; |
| | 14 | | public event Action OnUpdated; |
| | 15 | | public event Action OnCleanup; |
| | 16 | |
|
| | 17 | | public GameObject innerGameObject; |
| | 18 | |
|
| | 19 | | public GameObject meshRootGameObject |
| | 20 | | { |
| 0 | 21 | | get { return meshRootGameObjectValue; } |
| | 22 | | set |
| | 23 | | { |
| 474 | 24 | | meshRootGameObjectValue = value; |
| 474 | 25 | | UpdateRenderersCollection(); |
| 474 | 26 | | } |
| | 27 | | } |
| | 28 | |
|
| | 29 | | GameObject meshRootGameObjectValue; |
| | 30 | |
|
| | 31 | | public IShape currentShape; |
| | 32 | | public Renderer[] renderers; |
| | 33 | | public MeshFilter[] meshFilters; |
| 0 | 34 | | public List<Collider> colliders = new List<Collider>(); |
| 0 | 35 | | public Animation animation { get; private set; } |
| | 36 | |
|
| | 37 | | Vector3 lastBoundsCalculationPosition; |
| | 38 | | Vector3 lastBoundsCalculationScale; |
| | 39 | | Quaternion lastBoundsCalculationRotation; |
| | 40 | | Bounds mergedBoundsValue; |
| | 41 | |
|
| | 42 | | public Bounds mergedBounds |
| | 43 | | { |
| | 44 | | get |
| | 45 | | { |
| 188 | 46 | | if (meshRootGameObject.transform.position != lastBoundsCalculationPosition) |
| | 47 | | { |
| 41 | 48 | | mergedBoundsValue.center += meshRootGameObject.transform.position - lastBoundsCalculationPosition; |
| 41 | 49 | | lastBoundsCalculationPosition = meshRootGameObject.transform.position; |
| | 50 | | } |
| | 51 | |
|
| 188 | 52 | | if (meshRootGameObject.transform.lossyScale != lastBoundsCalculationScale || |
| | 53 | | meshRootGameObject.transform.rotation != lastBoundsCalculationRotation) |
| 4 | 54 | | RecalculateBounds(); |
| | 55 | |
|
| 188 | 56 | | return mergedBoundsValue; |
| | 57 | | } |
| 0 | 58 | | set { mergedBoundsValue = value; } |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public void UpdateRenderersCollection(Renderer[] renderers, MeshFilter[] meshFilters, Animation animation = null |
| | 62 | | { |
| 26 | 63 | | if (meshRootGameObjectValue != null) |
| | 64 | | { |
| 26 | 65 | | this.renderers = renderers; |
| 26 | 66 | | this.meshFilters = meshFilters; |
| 26 | 67 | | this.animation = animation; |
| | 68 | |
|
| 26 | 69 | | RecalculateBounds(); |
| 26 | 70 | | OnAnyUpdated?.Invoke(); |
| 26 | 71 | | OnUpdated?.Invoke(); |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void UpdateRenderersCollection() |
| | 76 | | { |
| 1080 | 77 | | if (meshRootGameObjectValue != null) |
| | 78 | | { |
| 852 | 79 | | renderers = meshRootGameObjectValue.GetComponentsInChildren<Renderer>(true); |
| 852 | 80 | | meshFilters = meshRootGameObjectValue.GetComponentsInChildren<MeshFilter>(true); |
| 852 | 81 | | animation = meshRootGameObjectValue.GetComponentInChildren<Animation>(); |
| | 82 | |
|
| 852 | 83 | | TextMeshPro[] tmpros = meshRootGameObjectValue.GetComponentsInChildren<TextMeshPro>(true); |
| 852 | 84 | | if (tmpros.Length > 0) |
| | 85 | | { |
| 112 | 86 | | renderers = renderers.Union(tmpros.Select(x => x.renderer)).ToArray(); |
| 112 | 87 | | meshFilters = meshFilters.Union(tmpros.Select(x => x.meshFilter)).ToArray(); |
| | 88 | | } |
| | 89 | |
|
| 852 | 90 | | RecalculateBounds(); |
| 852 | 91 | | OnAnyUpdated?.Invoke(); |
| 852 | 92 | | OnUpdated?.Invoke(); |
| | 93 | | } |
| 1003 | 94 | | } |
| | 95 | |
|
| | 96 | | public void RecalculateBounds() |
| | 97 | | { |
| 882 | 98 | | if (renderers == null || renderers.Length == 0) |
| 300 | 99 | | return; |
| | 100 | |
|
| 582 | 101 | | lastBoundsCalculationPosition = meshRootGameObject.transform.position; |
| 582 | 102 | | lastBoundsCalculationScale = meshRootGameObject.transform.lossyScale; |
| 582 | 103 | | lastBoundsCalculationRotation = meshRootGameObject.transform.rotation; |
| | 104 | |
|
| 582 | 105 | | mergedBoundsValue = MeshesInfoUtils.BuildMergedBounds(renderers); |
| 582 | 106 | | } |
| | 107 | |
|
| | 108 | | public void CleanReferences() |
| | 109 | | { |
| 265 | 110 | | OnCleanup?.Invoke(); |
| 265 | 111 | | meshRootGameObjectValue = null; |
| 265 | 112 | | currentShape = null; |
| 265 | 113 | | renderers = null; |
| 265 | 114 | | colliders.Clear(); |
| 265 | 115 | | } |
| | 116 | |
|
| | 117 | | public void UpdateExistingMeshAtIndex(Mesh mesh, uint meshFilterIndex = 0) |
| | 118 | | { |
| 3 | 119 | | if (meshFilters != null && meshFilters.Length > meshFilterIndex) |
| | 120 | | { |
| 3 | 121 | | meshFilters[meshFilterIndex].sharedMesh = mesh; |
| 3 | 122 | | OnUpdated?.Invoke(); |
| 3 | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 0 | 126 | | Debug.LogError( |
| | 127 | | $"MeshFilter index {meshFilterIndex} out of bounds - MeshesInfo.UpdateExistingMesh failed"); |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | | } |
| | 131 | | } |