| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace DCL.ECSComponents |
| | 9 | | { |
| | 10 | | public class GltfContainerCollidersHandler |
| | 11 | | { |
| 33 | 12 | | private readonly List<Collider> invisibleMeshesColliders = new List<Collider>(10); // colliders from `_collider` |
| 33 | 13 | | private readonly List<Collider> visibleMeshesColliders = new List<Collider>(10); // colliders from renderers |
| | 14 | | private IReadOnlyCollection<Renderer> cachedRenderers; |
| | 15 | |
|
| | 16 | | // We restore all modifications on colliders to avoid issues with sdk6 GLTFShape |
| | 17 | | public void CleanUp() |
| | 18 | | { |
| 56 | 19 | | for (int i = 0; i < invisibleMeshesColliders.Count; i++) |
| | 20 | | { |
| 8 | 21 | | Collider collider = invisibleMeshesColliders[i]; |
| 8 | 22 | | collider.enabled = false; |
| 8 | 23 | | collider.gameObject.layer = PhysicsLayers.characterOnlyLayer; |
| | 24 | | } |
| | 25 | |
|
| 46 | 26 | | for (int i = 0; i < visibleMeshesColliders.Count; i++) |
| | 27 | | { |
| 3 | 28 | | Collider collider = visibleMeshesColliders[i]; |
| | 29 | |
|
| 3 | 30 | | if (collider) |
| 3 | 31 | | Object.Destroy(collider); |
| | 32 | | } |
| | 33 | |
|
| 20 | 34 | | invisibleMeshesColliders.Clear(); |
| 20 | 35 | | visibleMeshesColliders.Clear(); |
| 20 | 36 | | } |
| | 37 | |
|
| | 38 | | // Since gltf are modified when creating colliders and stored in cache library with those modifications |
| | 39 | | // we check if colliders already exists and disable them |
| | 40 | | // otherwise we create them (disabled) |
| | 41 | | public IReadOnlyList<Collider> InitInvisibleMeshesColliders(IReadOnlyList<MeshFilter> meshFilters) |
| | 42 | | { |
| 31 | 43 | | invisibleMeshesColliders.Clear(); |
| | 44 | |
|
| 152 | 45 | | for (int i = 0; i < meshFilters.Count; i++) |
| | 46 | | { |
| 45 | 47 | | if (!IsCollider(meshFilters[i])) |
| | 48 | | continue; |
| | 49 | |
|
| 21 | 50 | | Collider collider = meshFilters[i].gameObject.GetComponent<Collider>(); |
| | 51 | |
|
| 21 | 52 | | if (collider != null) |
| | 53 | | { |
| 0 | 54 | | collider.enabled = false; |
| 0 | 55 | | invisibleMeshesColliders.Add(collider); |
| 0 | 56 | | continue; |
| | 57 | | } |
| | 58 | |
|
| 21 | 59 | | MeshCollider newCollider = meshFilters[i].gameObject.AddComponent<MeshCollider>(); |
| 21 | 60 | | newCollider.sharedMesh = meshFilters[i].sharedMesh; |
| 21 | 61 | | newCollider.enabled = false; |
| 21 | 62 | | invisibleMeshesColliders.Add(newCollider); |
| | 63 | | } |
| | 64 | |
|
| 31 | 65 | | return invisibleMeshesColliders; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | // Since gltf are modified when creating colliders and stored in cache library with those modifications |
| | 69 | | // we check if colliders already exists and disable them |
| | 70 | | // if `createColliders` we create them (disabled) |
| | 71 | | public IReadOnlyList<Collider> InitVisibleMeshesColliders(IReadOnlyCollection<Renderer> renderers, bool createCo |
| | 72 | | { |
| 31 | 73 | | cachedRenderers = renderers; |
| | 74 | |
|
| | 75 | | try |
| | 76 | | { |
| 31 | 77 | | if (createColliders) |
| | 78 | | { |
| 11 | 79 | | SetVisibleMeshesColliders(renderers); |
| | 80 | | } |
| 31 | 81 | | } |
| | 82 | | catch (Exception e) |
| | 83 | | { |
| 0 | 84 | | Debug.LogException(e); |
| 0 | 85 | | } |
| | 86 | |
|
| 31 | 87 | | return visibleMeshesColliders; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // Create visible meshes colliders if there are none created yet |
| | 91 | | // otherwise return the already created colliders |
| | 92 | | public IReadOnlyList<Collider> GetVisibleMeshesColliders() |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| 17 | 96 | | if (visibleMeshesColliders.Count == 0) |
| 1 | 97 | | SetVisibleMeshesColliders(cachedRenderers); |
| 17 | 98 | | } |
| 0 | 99 | | catch (Exception e) { Debug.LogException(e); } |
| | 100 | |
|
| 17 | 101 | | return visibleMeshesColliders; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | public IReadOnlyList<Collider> GetInvisibleMeshesColliders() |
| | 105 | | { |
| 38 | 106 | | return invisibleMeshesColliders; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | private void SetVisibleMeshesColliders(IReadOnlyCollection<Renderer> renderers) |
| | 110 | | { |
| | 111 | | const string POINTER_COLLIDER_NAME = "OnPointerEventCollider"; |
| | 112 | |
|
| 12 | 113 | | if (visibleMeshesColliders.Count > 0) |
| 0 | 114 | | return; |
| | 115 | |
|
| 52 | 116 | | foreach (Renderer renderer in renderers) |
| | 117 | | { |
| 14 | 118 | | Mesh colliderMesh = null; |
| | 119 | |
|
| 14 | 120 | | if (renderer is SkinnedMeshRenderer skinnedMeshRenderer) |
| | 121 | | { |
| 1 | 122 | | colliderMesh = skinnedMeshRenderer.sharedMesh; |
| | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 13 | 126 | | MeshFilter meshFilter = renderer.GetComponent<MeshFilter>(); |
| 13 | 127 | | colliderMesh = meshFilter ? meshFilter.sharedMesh : null; |
| | 128 | | } |
| | 129 | |
|
| 14 | 130 | | if (!colliderMesh) |
| | 131 | | continue; |
| | 132 | |
|
| 14 | 133 | | GameObject colliderGo = new GameObject(POINTER_COLLIDER_NAME); |
| 14 | 134 | | MeshCollider newCollider = colliderGo.AddComponent<MeshCollider>(); |
| | 135 | |
|
| 14 | 136 | | newCollider.sharedMesh = colliderMesh; |
| 14 | 137 | | newCollider.enabled = false; |
| 14 | 138 | | colliderGo.transform.SetParent(renderer.transform); |
| 14 | 139 | | colliderGo.transform.ResetLocalTRS(); |
| 14 | 140 | | visibleMeshesColliders.Add(newCollider); |
| | 141 | | } |
| 12 | 142 | | } |
| | 143 | |
|
| | 144 | | // Compatibility layer for old GLTF importer and GLTFast |
| | 145 | | private static bool IsCollider(MeshFilter meshFilter) |
| | 146 | | { |
| | 147 | | const StringComparison IGNORE_CASE = StringComparison.OrdinalIgnoreCase; |
| | 148 | | const string COLLIDER_SUFFIX = "_collider"; |
| | 149 | |
|
| 45 | 150 | | return meshFilter.name.Contains(COLLIDER_SUFFIX, IGNORE_CASE) |
| | 151 | | || meshFilter.transform.parent.name.Contains(COLLIDER_SUFFIX, IGNORE_CASE); |
| | 152 | | } |
| | 153 | | } |
| | 154 | | } |