| | 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 | | { |
| 309 | 24 | | meshRootGameObjectValue = value; |
| 309 | 25 | | UpdateRenderersCollection(); |
| 309 | 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>(); |
| | 35 | |
|
| | 36 | | Vector3 lastBoundsCalculationPosition; |
| | 37 | | Vector3 lastBoundsCalculationScale; |
| | 38 | | Quaternion lastBoundsCalculationRotation; |
| | 39 | | Bounds mergedBoundsValue; |
| | 40 | |
|
| | 41 | | public Bounds mergedBounds |
| | 42 | | { |
| | 43 | | get |
| | 44 | | { |
| 229 | 45 | | if (meshRootGameObject.transform.position != lastBoundsCalculationPosition) |
| | 46 | | { |
| 40 | 47 | | mergedBoundsValue.center += meshRootGameObject.transform.position - lastBoundsCalculationPosition; |
| 40 | 48 | | lastBoundsCalculationPosition = meshRootGameObject.transform.position; |
| | 49 | | } |
| | 50 | |
|
| 229 | 51 | | if (meshRootGameObject.transform.lossyScale != lastBoundsCalculationScale || meshRootGameObject.transfor |
| 6 | 52 | | RecalculateBounds(); |
| | 53 | |
|
| 229 | 54 | | return mergedBoundsValue; |
| | 55 | | } |
| 0 | 56 | | set { mergedBoundsValue = value; } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public void UpdateRenderersCollection() |
| | 60 | | { |
| 730 | 61 | | if (meshRootGameObjectValue != null) |
| | 62 | | { |
| 650 | 63 | | renderers = meshRootGameObjectValue.GetComponentsInChildren<Renderer>(true); |
| 650 | 64 | | meshFilters = meshRootGameObjectValue.GetComponentsInChildren<MeshFilter>(true); |
| | 65 | |
|
| 650 | 66 | | TextMeshPro[] tmpros = meshRootGameObjectValue.GetComponentsInChildren<TextMeshPro>(true); |
| 650 | 67 | | if (tmpros.Length > 0) |
| | 68 | | { |
| 48 | 69 | | renderers = renderers.Union(tmpros.Select(x => x.renderer)).ToArray(); |
| 48 | 70 | | meshFilters = meshFilters.Union(tmpros.Select(x => x.meshFilter)).ToArray(); |
| | 71 | | } |
| | 72 | |
|
| 650 | 73 | | RecalculateBounds(); |
| 650 | 74 | | OnAnyUpdated?.Invoke(); |
| 650 | 75 | | OnUpdated?.Invoke(); |
| | 76 | | } |
| 725 | 77 | | } |
| | 78 | |
|
| | 79 | | public void RecalculateBounds() |
| | 80 | | { |
| 656 | 81 | | if (renderers == null || renderers.Length == 0) |
| 283 | 82 | | return; |
| | 83 | |
|
| 373 | 84 | | lastBoundsCalculationPosition = meshRootGameObject.transform.position; |
| 373 | 85 | | lastBoundsCalculationScale = meshRootGameObject.transform.lossyScale; |
| 373 | 86 | | lastBoundsCalculationRotation = meshRootGameObject.transform.rotation; |
| | 87 | |
|
| 373 | 88 | | mergedBoundsValue = MeshesInfoUtils.BuildMergedBounds(renderers); |
| 373 | 89 | | } |
| | 90 | |
|
| | 91 | | public void CleanReferences() |
| | 92 | | { |
| 80 | 93 | | OnCleanup?.Invoke(); |
| 80 | 94 | | meshRootGameObjectValue = null; |
| 80 | 95 | | currentShape = null; |
| 80 | 96 | | renderers = null; |
| 80 | 97 | | colliders.Clear(); |
| 80 | 98 | | } |
| | 99 | |
|
| | 100 | | public void UpdateExistingMeshAtIndex(Mesh mesh, uint meshFilterIndex = 0) |
| | 101 | | { |
| 3 | 102 | | if (meshFilters != null && meshFilters.Length > meshFilterIndex) |
| | 103 | | { |
| 3 | 104 | | meshFilters[meshFilterIndex].sharedMesh = mesh; |
| 3 | 105 | | OnUpdated?.Invoke(); |
| 3 | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 0 | 109 | | Debug.LogError($"MeshFilter index {meshFilterIndex} out of bounds - MeshesInfo.UpdateExistingMesh failed |
| | 110 | | } |
| 0 | 111 | | } |
| | 112 | | } |
| | 113 | | } |