| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class ABDetectorTracker : IDisposable |
| | 9 | | { |
| | 10 | | private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle"; |
| | 11 | | private const string FROM_RAW_GLTF_TAG = "FromRawGLTF"; |
| | 12 | | private const string AB_DETECTOR_MATERIALS_PREFAB_NAME = "AbDetectorMaterials"; |
| | 13 | |
|
| | 14 | | private readonly DebugConfig debugConfig; |
| | 15 | | private readonly DataStore_Player player; |
| | 16 | |
|
| 0 | 17 | | private readonly Dictionary<Renderer, Material[]> rendererDict = |
| | 18 | | new Dictionary<Renderer, Material[]>(); |
| 0 | 19 | | private readonly Multimap<IParcelScene, Renderer> parcelToRendererMultimap = |
| | 20 | | new Multimap<IParcelScene, Renderer>(); |
| | 21 | |
|
| | 22 | | private ABDetectorMaterialsHolder abDetectorMaterialsHolder; |
| | 23 | | private readonly IWorldState worldState; |
| | 24 | |
|
| 0 | 25 | | public ABDetectorTracker(DebugConfig debugConfig, DataStore_Player player, IWorldState worldState) |
| | 26 | | { |
| 0 | 27 | | this.debugConfig = debugConfig; |
| 0 | 28 | | this.player = player; |
| 0 | 29 | | this.worldState = worldState; |
| | 30 | |
|
| 0 | 31 | | debugConfig.showGlobalABDetectionLayer.OnChange += OnGlobalABDetectionChanged; |
| 0 | 32 | | debugConfig.showSceneABDetectionLayer.OnChange += OnSceneABDetectionChanged; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Dispose() |
| | 36 | | { |
| 0 | 37 | | debugConfig.showGlobalABDetectionLayer.OnChange -= OnGlobalABDetectionChanged; |
| 0 | 38 | | debugConfig.showSceneABDetectionLayer.OnChange -= OnSceneABDetectionChanged; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private IParcelScene FindSceneForPlayer() |
| | 42 | | { |
| 0 | 43 | | var currentPos = player.playerGridPosition.Get(); |
| 0 | 44 | | if (worldState.TryGetScene(worldState.GetSceneIdByCoords(currentPos), |
| | 45 | | out IParcelScene resultScene)) |
| 0 | 46 | | return resultScene; |
| | 47 | |
|
| 0 | 48 | | return null; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | private void LoadMaterialsIfNeeded() |
| | 52 | | { |
| 0 | 53 | | if (abDetectorMaterialsHolder == null) |
| | 54 | | { |
| 0 | 55 | | abDetectorMaterialsHolder = Resources.Load<GameObject> |
| | 56 | | (AB_DETECTOR_MATERIALS_PREFAB_NAME) |
| | 57 | | .GetComponent<ABDetectorMaterialsHolder>(); |
| | 58 | | } |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnGlobalABDetectionChanged(bool current, bool previous) |
| | 62 | | { |
| 0 | 63 | | LoadMaterialsIfNeeded(); |
| | 64 | |
|
| 0 | 65 | | if (current) |
| | 66 | | { |
| 0 | 67 | | RemoveGlobalABDetectionPainting(); |
| 0 | 68 | | ApplyGlobalABDetectionPainting(); |
| 0 | 69 | | } |
| | 70 | | else |
| | 71 | | { |
| 0 | 72 | | RemoveGlobalABDetectionPainting(); |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private void OnSceneABDetectionChanged(bool current, bool previous) |
| | 77 | | { |
| 0 | 78 | | LoadMaterialsIfNeeded(); |
| | 79 | |
|
| 0 | 80 | | if (current) |
| | 81 | | { |
| 0 | 82 | | RemoveGlobalABDetectionPainting(); |
| 0 | 83 | | ApplyABDetectionPaintingForCurrentScene(); |
| 0 | 84 | | } |
| | 85 | | else |
| | 86 | | { |
| 0 | 87 | | RemoveABDetectionPaintingForCurrentScene(); |
| | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void ApplyGlobalABDetectionPainting() |
| | 92 | | { |
| 0 | 93 | | var gameObjects = GameObject.FindObjectsOfType(typeof(GameObject)); |
| 0 | 94 | | foreach (var gameObject in gameObjects) |
| | 95 | | { |
| 0 | 96 | | var converted = (GameObject) gameObject; |
| 0 | 97 | | if (converted.transform.parent == null) |
| | 98 | | { |
| 0 | 99 | | ApplyMaterials(converted.transform); |
| | 100 | | } |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void RemoveGlobalABDetectionPainting() |
| | 105 | | { |
| 0 | 106 | | foreach (KeyValuePair<Renderer,Material[]> keyValuePair in rendererDict) |
| | 107 | | { |
| 0 | 108 | | keyValuePair.Key.materials = keyValuePair.Value; |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | rendererDict.Clear(); |
| 0 | 112 | | parcelToRendererMultimap.Clear(); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private void ApplyABDetectionPaintingForCurrentScene() |
| | 116 | | { |
| 0 | 117 | | var currentScene = FindSceneForPlayer(); |
| 0 | 118 | | ApplyMaterials(currentScene.GetSceneTransform(), currentScene); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | private void RemoveABDetectionPaintingForCurrentScene() |
| | 122 | | { |
| 0 | 123 | | var currentScene = FindSceneForPlayer(); |
| 0 | 124 | | if (parcelToRendererMultimap.ContainsKey(currentScene)) |
| | 125 | | { |
| 0 | 126 | | foreach (var renderer in parcelToRendererMultimap.GetValues(currentScene)) |
| | 127 | | { |
| 0 | 128 | | if (rendererDict.TryGetValue(renderer, out var materials)) |
| | 129 | | { |
| 0 | 130 | | renderer.materials = materials; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | rendererDict.Remove(renderer); |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | parcelToRendererMultimap.Remove(currentScene); |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void ApplyMaterials(Transform someTransform, IParcelScene optionalParcelScene = null) |
| | 142 | | { |
| 0 | 143 | | if (someTransform.childCount > 0) |
| | 144 | | { |
| 0 | 145 | | for (int i = 0; i < someTransform.childCount; i++) |
| | 146 | | { |
| 0 | 147 | | var childTransform = someTransform.GetChild(i).transform; |
| 0 | 148 | | var renderers = childTransform. |
| | 149 | | GetComponents<Renderer>(); |
| | 150 | |
|
| 0 | 151 | | foreach (Renderer renderer in renderers) |
| | 152 | | { |
| 0 | 153 | | rendererDict[renderer] = renderer.materials; |
| | 154 | |
|
| 0 | 155 | | if (optionalParcelScene != null) |
| | 156 | | { |
| 0 | 157 | | parcelToRendererMultimap.Add(optionalParcelScene, renderer); |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | if (renderer.tag.Equals(FROM_ASSET_BUNDLE_TAG)) |
| | 161 | | { |
| 0 | 162 | | renderer.material = abDetectorMaterialsHolder.ABMaterial; |
| 0 | 163 | | } |
| 0 | 164 | | else if(renderer.tag.Equals(FROM_RAW_GLTF_TAG)) |
| | 165 | | { |
| 0 | 166 | | renderer.material = abDetectorMaterialsHolder.GLTFMaterial; |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 0 | 170 | | ApplyMaterials(childTransform, optionalParcelScene); |
| | 171 | | } |
| | 172 | | } |
| 0 | 173 | | } |
| | 174 | | } |
| | 175 | | } |