| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | namespace DCL.Components |
| | 11 | | { |
| | 12 | | public class LoadableShape : BaseShape, IAssetCatalogReferenceHolder |
| | 13 | | { |
| | 14 | | [System.Serializable] |
| | 15 | | public new class Model : BaseShape.Model |
| | 16 | | { |
| | 17 | | public string src; |
| | 18 | | public string assetId; |
| | 19 | |
|
| | 20 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public bool isLoaded { get; protected set; } |
| | 24 | |
|
| | 25 | | public Action<LoadableShape> OnLoaded; |
| | 26 | |
|
| | 27 | | protected Model previousModel = new Model(); |
| | 28 | |
|
| | 29 | | protected static Dictionary<GameObject, LoadWrapper> attachedLoaders = new Dictionary<GameObject, LoadWrapper>() |
| | 30 | |
|
| | 31 | | public static LoadWrapper GetLoaderForEntity(IDCLEntity entity) |
| | 32 | | { |
| | 33 | | if (entity.meshRootGameObject == null) |
| | 34 | | { |
| | 35 | | Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()"); |
| | 36 | | return null; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result); |
| | 40 | | return result; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public static T GetOrAddLoaderForEntity<T>(IDCLEntity entity) |
| | 44 | | where T : LoadWrapper, new() |
| | 45 | | { |
| | 46 | | if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result)) |
| | 47 | | { |
| | 48 | | result = new T(); |
| | 49 | | attachedLoaders.Add(entity.meshRootGameObject, result); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | return result as T; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public LoadableShape() { model = new Model(); } |
| | 56 | |
|
| | 57 | | public override int GetClassId() { return -1; } |
| | 58 | |
|
| | 59 | | public override IEnumerator ApplyChanges(BaseModel newModel) { return null; } |
| | 60 | |
|
| | 61 | | public override bool IsVisible() |
| | 62 | | { |
| | 63 | | Model model = (Model) this.model; |
| | 64 | | return model.visible; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public override bool HasCollisions() |
| | 68 | | { |
| | 69 | | Model model = (Model) this.model; |
| | 70 | | return model.withCollisions; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public string GetAssetId() |
| | 74 | | { |
| | 75 | | Model model = (Model) this.model; |
| | 76 | | return model.assetId; |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public class LoadableShape<LoadWrapperType, LoadWrapperModelType> : LoadableShape |
| | 81 | | where LoadWrapperType : LoadWrapper, new() |
| | 82 | | where LoadWrapperModelType : LoadableShape.Model, new() |
| | 83 | | { |
| | 84 | | private bool failed = false; |
| | 85 | | private event Action<BaseDisposable> OnFinishCallbacks; |
| | 86 | | public System.Action<IDCLEntity> OnEntityShapeUpdated; |
| | 87 | |
|
| | 88 | | new public LoadWrapperModelType model |
| | 89 | | { |
| | 90 | | get |
| | 91 | | { |
| 618 | 92 | | if (base.model == null) |
| 0 | 93 | | base.model = new LoadWrapperModelType(); |
| | 94 | |
|
| 618 | 95 | | return base.model as LoadWrapperModelType; |
| | 96 | | } |
| 0 | 97 | | set { base.model = value; } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | new protected LoadWrapperModelType previousModel |
| | 101 | | { |
| | 102 | | get |
| | 103 | | { |
| 386 | 104 | | if (base.previousModel == null) |
| 0 | 105 | | base.previousModel = new LoadWrapperModelType(); |
| | 106 | |
|
| 386 | 107 | | return base.previousModel as LoadWrapperModelType; |
| | 108 | | } |
| 0 | 109 | | set { base.previousModel = value; } |
| | 110 | | } |
| | 111 | |
|
| 86 | 112 | | public LoadableShape() |
| | 113 | | { |
| 86 | 114 | | OnDetach += DetachShape; |
| 86 | 115 | | OnAttach += AttachShape; |
| 86 | 116 | | } |
| | 117 | |
|
| | 118 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 119 | | { |
| 79 | 120 | | LoadWrapperModelType model = (LoadWrapperModelType) newModel; |
| | 121 | |
|
| 79 | 122 | | bool updateVisibility = true; |
| 79 | 123 | | bool updateCollisions = true; |
| 79 | 124 | | bool triggerAttachment = true; |
| | 125 | |
|
| 79 | 126 | | if (previousModel != null) |
| | 127 | | { |
| 69 | 128 | | updateVisibility = previousModel.visible != model.visible; |
| 69 | 129 | | updateCollisions = previousModel.withCollisions != model.withCollisions || previousModel.isPointerBlocke |
| 69 | 130 | | triggerAttachment = (!string.IsNullOrEmpty(model.src) && previousModel.src != model.src) || |
| | 131 | | (!string.IsNullOrEmpty(model.assetId) && previousModel.assetId != model.assetId); |
| | 132 | | } |
| | 133 | |
|
| 168 | 134 | | foreach (var entity in attachedEntities) |
| | 135 | | { |
| 5 | 136 | | if (triggerAttachment) |
| 2 | 137 | | AttachShape(entity); |
| | 138 | |
|
| 5 | 139 | | if (updateVisibility) |
| 2 | 140 | | ConfigureVisibility(entity); |
| | 141 | |
|
| 5 | 142 | | if (updateCollisions) |
| 0 | 143 | | ConfigureColliders(entity); |
| | 144 | |
|
| 5 | 145 | | RaiseOnShapeUpdated(entity); |
| | 146 | | } |
| | 147 | |
|
| 79 | 148 | | previousModel = model; |
| 79 | 149 | | return null; |
| | 150 | | } |
| | 151 | |
|
| | 152 | | protected virtual void AttachShape(IDCLEntity entity) |
| | 153 | | { |
| 79 | 154 | | ContentProvider provider = null; |
| | 155 | |
|
| 79 | 156 | | if (!string.IsNullOrEmpty(model.assetId)) |
| | 157 | | { |
| 16 | 158 | | provider = AssetCatalogBridge.i.GetContentProviderForAssetIdInSceneObjectCatalog(model.assetId); |
| 16 | 159 | | if (string.IsNullOrEmpty(model.src)) |
| | 160 | | { |
| 0 | 161 | | SceneObject sceneObject = AssetCatalogBridge.i.GetSceneObjectById(model.assetId); |
| 0 | 162 | | if (sceneObject == null) |
| | 163 | | { |
| | 164 | | #if UNITY_EDITOR |
| 0 | 165 | | Debug.LogWarning($"LoadableShape '{model.assetId}' not found in catalog, probably asset pack del |
| | 166 | | #endif |
| 0 | 167 | | failed = true; |
| 0 | 168 | | OnLoadFailed(null); |
| 0 | 169 | | return; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | model.src = sceneObject.model; |
| | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| 79 | 176 | | if (provider == null) |
| 63 | 177 | | provider = scene.contentProvider; |
| | 178 | |
|
| 79 | 179 | | if (provider.HasContentsUrl(model.src)) |
| | 180 | | { |
| 79 | 181 | | isLoaded = false; |
| 79 | 182 | | entity.EnsureMeshGameObject(componentName + " mesh"); |
| | 183 | |
|
| 79 | 184 | | LoadWrapperType loadableShape = GetOrAddLoaderForEntity<LoadWrapperType>(entity); |
| | 185 | |
|
| 79 | 186 | | if (loadableShape is LoadWrapper_GLTF gltfLoadWrapper) |
| 78 | 187 | | gltfLoadWrapper.customContentProvider = provider; |
| | 188 | |
|
| 79 | 189 | | entity.meshesInfo.currentShape = this; |
| | 190 | |
|
| 79 | 191 | | loadableShape.entity = entity; |
| 79 | 192 | | loadableShape.useVisualFeedback = Configuration.ParcelSettings.VISUAL_LOADING_ENABLED; |
| 79 | 193 | | loadableShape.initialVisibility = model.visible; |
| 79 | 194 | | loadableShape.Load(model.src, OnLoadCompleted, OnLoadFailed); |
| 79 | 195 | | } |
| | 196 | | else |
| | 197 | | { |
| | 198 | | #if UNITY_EDITOR |
| 0 | 199 | | Debug.LogWarning($"LoadableShape '{model.src}' not found in scene '{scene.sceneData.id}' mappings"); |
| | 200 | | #endif |
| 0 | 201 | | failed = true; |
| | 202 | | } |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | void ConfigureVisibility(IDCLEntity entity) |
| | 206 | | { |
| 2 | 207 | | var loadable = GetLoaderForEntity(entity); |
| | 208 | |
|
| 2 | 209 | | if (loadable != null) |
| 2 | 210 | | loadable.initialVisibility = model.visible; |
| | 211 | |
|
| 2 | 212 | | ConfigureVisibility(entity.meshRootGameObject, model.visible, entity.meshesInfo.renderers); |
| 2 | 213 | | } |
| | 214 | |
|
| 140 | 215 | | protected virtual void ConfigureColliders(IDCLEntity entity) { CollidersManager.i.ConfigureColliders(entity.mesh |
| | 216 | |
|
| | 217 | | protected void OnLoadFailed(LoadWrapper loadWrapper) |
| | 218 | | { |
| 3 | 219 | | if (loadWrapper != null) |
| 3 | 220 | | CleanFailedWrapper(loadWrapper); |
| | 221 | |
|
| 3 | 222 | | failed = true; |
| 3 | 223 | | OnFinishCallbacks?.Invoke(this); |
| 3 | 224 | | OnFinishCallbacks = null; |
| 3 | 225 | | } |
| | 226 | |
|
| | 227 | | void CleanFailedWrapper(LoadWrapper loadWrapper) |
| | 228 | | { |
| 3 | 229 | | if (loadWrapper == null) |
| 0 | 230 | | return; |
| 3 | 231 | | if (loadWrapper.entity == null) |
| 0 | 232 | | return; |
| 3 | 233 | | if (loadWrapper.entity.gameObject == null) |
| 0 | 234 | | return; |
| | 235 | |
|
| 3 | 236 | | GameObject go = loadWrapper.entity.gameObject; |
| | 237 | |
|
| 3 | 238 | | go.name += " - Failed loading"; |
| | 239 | |
|
| 3 | 240 | | MaterialTransitionController[] transitionController = |
| | 241 | | go.GetComponentsInChildren<MaterialTransitionController>(true); |
| | 242 | |
|
| 6 | 243 | | for (int i = 0; i < transitionController.Length; i++) |
| | 244 | | { |
| 0 | 245 | | MaterialTransitionController material = transitionController[i]; |
| 0 | 246 | | Object.Destroy(material); |
| | 247 | | } |
| 3 | 248 | | } |
| | 249 | |
|
| | 250 | | protected void OnLoadCompleted(LoadWrapper loadWrapper) |
| | 251 | | { |
| 80 | 252 | | IDCLEntity entity = loadWrapper.entity; |
| | 253 | |
|
| 80 | 254 | | if (entity.meshesInfo.currentShape == null) |
| | 255 | | { |
| 0 | 256 | | OnLoadFailed(loadWrapper); |
| 0 | 257 | | return; |
| | 258 | | } |
| | 259 | |
|
| 80 | 260 | | isLoaded = true; |
| 80 | 261 | | OnLoaded?.Invoke(this); |
| | 262 | |
|
| 80 | 263 | | entity.meshesInfo.renderers = entity.meshRootGameObject.GetComponentsInChildren<Renderer>(); |
| | 264 | |
|
| 80 | 265 | | var model = (Model) (entity.meshesInfo.currentShape as LoadableShape).GetModel(); |
| | 266 | |
|
| 80 | 267 | | ConfigureVisibility(entity.meshRootGameObject, model.visible, loadWrapper.entity.meshesInfo.renderers); |
| 80 | 268 | | ConfigureColliders(entity); |
| 80 | 269 | | RaiseOnShapeUpdated(entity); |
| 80 | 270 | | RaiseOnShapeLoaded(entity); |
| | 271 | |
|
| 80 | 272 | | OnFinishCallbacks?.Invoke(this); |
| 80 | 273 | | OnFinishCallbacks = null; |
| 80 | 274 | | } |
| | 275 | |
|
| | 276 | | protected virtual void DetachShape(IDCLEntity entity) |
| | 277 | | { |
| 82 | 278 | | if (entity == null || entity.meshRootGameObject == null) |
| 3 | 279 | | return; |
| | 280 | |
|
| 79 | 281 | | LoadWrapper loadWrapper = GetLoaderForEntity(entity); |
| | 282 | |
|
| 79 | 283 | | loadWrapper?.Unload(); |
| | 284 | |
|
| 79 | 285 | | entity.meshesInfo.CleanReferences(); |
| 79 | 286 | | } |
| | 287 | |
|
| | 288 | | public override void CallWhenReady(Action<ISharedComponent> callback) |
| | 289 | | { |
| 19 | 290 | | if (attachedEntities.Count == 0 || isLoaded || failed) |
| | 291 | | { |
| 3 | 292 | | callback.Invoke(this); |
| 3 | 293 | | } |
| | 294 | | else |
| | 295 | | { |
| 16 | 296 | | OnFinishCallbacks += callback; |
| | 297 | | } |
| 16 | 298 | | } |
| | 299 | |
|
| | 300 | | private void RaiseOnShapeUpdated(IDCLEntity entity) |
| | 301 | | { |
| 85 | 302 | | if (!isLoaded) |
| 3 | 303 | | return; |
| | 304 | |
|
| 82 | 305 | | entity.OnShapeUpdated?.Invoke(entity); |
| 82 | 306 | | } |
| | 307 | |
|
| | 308 | | private void RaiseOnShapeLoaded(IDCLEntity entity) |
| | 309 | | { |
| 80 | 310 | | if (!isLoaded) |
| 0 | 311 | | return; |
| | 312 | |
|
| 80 | 313 | | entity.OnShapeLoaded?.Invoke(entity); |
| 5 | 314 | | } |
| | 315 | | } |
| | 316 | | } |