| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | | using Decentraland.Sdk.Ecs6; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public class LoadableShape : BaseShape, IAssetCatalogReferenceHolder |
| | 11 | | { |
| | 12 | | [Serializable] |
| | 13 | | public new class Model : BaseShape.Model |
| | 14 | | { |
| | 15 | | public string src; |
| | 16 | | public string assetId; |
| | 17 | |
|
| | 18 | | public override BaseModel GetDataFromJSON(string json) => |
| | 19 | | Utils.SafeFromJson<Model>(json); |
| | 20 | |
|
| | 21 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 22 | | { |
| | 23 | | switch (pbModel.PayloadCase) |
| | 24 | | { |
| | 25 | | case ComponentBodyPayload.PayloadOneofCase.GltfShape: |
| | 26 | | var gltfModel = new Model(); |
| | 27 | | if (pbModel.GltfShape.HasSrc) gltfModel.src = pbModel.GltfShape.Src; |
| | 28 | | if (pbModel.GltfShape.HasWithCollisions) gltfModel.withCollisions = pbModel.GltfShape.WithCollis |
| | 29 | | if (pbModel.GltfShape.HasVisible) gltfModel.visible = pbModel.GltfShape.Visible; |
| | 30 | | if (pbModel.GltfShape.HasIsPointerBlocker) gltfModel.isPointerBlocker = pbModel.GltfShape.IsPoin |
| | 31 | |
|
| | 32 | | return gltfModel; |
| | 33 | | case ComponentBodyPayload.PayloadOneofCase.ObjShape: |
| | 34 | | var objModel = new Model(); |
| | 35 | | if (pbModel.ObjShape.HasSrc) objModel.src = pbModel.ObjShape.Src; |
| | 36 | | if (pbModel.ObjShape.HasWithCollisions) objModel.withCollisions = pbModel.ObjShape.WithCollision |
| | 37 | | if (pbModel.ObjShape.HasVisible) objModel.visible = pbModel.ObjShape.Visible; |
| | 38 | | if (pbModel.ObjShape.HasIsPointerBlocker) objModel.isPointerBlocker = pbModel.ObjShape.IsPointer |
| | 39 | |
|
| | 40 | | return objModel; |
| | 41 | | default: |
| | 42 | | return Utils.SafeUnimplemented<LoadableShape, Model>( |
| | 43 | | expected: ComponentBodyPayload.PayloadOneofCase.GltfShape, actual: pbModel.PayloadCase); |
| | 44 | | } |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public bool isLoaded { get; protected set; } |
| | 49 | |
|
| | 50 | | public Action<LoadableShape> OnLoaded; |
| | 51 | |
|
| | 52 | | protected Model previousModel = new (); |
| | 53 | |
|
| | 54 | | protected LoadableShape() |
| | 55 | | { |
| | 56 | | model = new Model(); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public override int GetClassId() => |
| | 60 | | -1; |
| | 61 | |
|
| | 62 | | public override IEnumerator ApplyChanges(BaseModel newModel) => |
| | 63 | | null; |
| | 64 | |
|
| | 65 | | public override bool IsVisible() => |
| | 66 | | ((Model)this.model).visible; |
| | 67 | |
|
| | 68 | | public override bool HasCollisions() => |
| | 69 | | ((Model)this.model).withCollisions; |
| | 70 | |
|
| | 71 | | public string GetAssetId() => |
| | 72 | | ((Model)this.model).assetId; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public class LoadableShape<LoadWrapperType, LoadWrapperModelType> : LoadableShape |
| | 76 | | where LoadWrapperType: LoadWrapper, new() |
| | 77 | | where LoadWrapperModelType: LoadableShape.Model, new() |
| | 78 | | { |
| | 79 | | private bool failed = false; |
| | 80 | | private event Action<BaseDisposable> OnFinishCallbacks; |
| | 81 | | public System.Action<IDCLEntity> OnEntityShapeUpdated; |
| | 82 | |
|
| | 83 | | new public LoadWrapperModelType model |
| | 84 | | { |
| | 85 | | get |
| | 86 | | { |
| 496 | 87 | | if (base.model == null) |
| 0 | 88 | | base.model = new LoadWrapperModelType(); |
| | 89 | |
|
| 496 | 90 | | return base.model as LoadWrapperModelType; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | set |
| | 94 | | { |
| 9 | 95 | | base.model = value; |
| 9 | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | | new protected LoadWrapperModelType previousModel |
| | 100 | | { |
| | 101 | | get |
| | 102 | | { |
| 386 | 103 | | if (base.previousModel == null) |
| 0 | 104 | | base.previousModel = new LoadWrapperModelType(); |
| | 105 | |
|
| 386 | 106 | | return base.previousModel as LoadWrapperModelType; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | set |
| | 110 | | { |
| 79 | 111 | | base.previousModel = value; |
| 79 | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 67 | 115 | | public LoadableShape() |
| | 116 | | { |
| 67 | 117 | | OnDetach += DetachShape; |
| 67 | 118 | | OnAttach += AttachShape; |
| 67 | 119 | | } |
| | 120 | |
|
| | 121 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 122 | | { |
| 79 | 123 | | LoadWrapperModelType model = (LoadWrapperModelType)newModel; |
| | 124 | |
|
| 79 | 125 | | bool updateVisibility = true; |
| 79 | 126 | | bool updateCollisions = true; |
| 79 | 127 | | bool triggerAttachment = true; |
| | 128 | |
|
| 79 | 129 | | if (previousModel != null) |
| | 130 | | { |
| 70 | 131 | | updateVisibility = previousModel.visible != model.visible; |
| 70 | 132 | | updateCollisions = previousModel.withCollisions != model.withCollisions || previousModel.isPointerBlocke |
| | 133 | |
|
| 70 | 134 | | triggerAttachment = (!string.IsNullOrEmpty(model.src) && previousModel.src != model.src) || |
| | 135 | | (!string.IsNullOrEmpty(model.assetId) && previousModel.assetId != model.assetId); |
| | 136 | | } |
| | 137 | |
|
| 178 | 138 | | foreach (var entity in attachedEntities) |
| | 139 | | { |
| 10 | 140 | | if (triggerAttachment) |
| 2 | 141 | | AttachShape(entity); |
| | 142 | |
|
| 10 | 143 | | if (updateVisibility) |
| 7 | 144 | | ConfigureVisibility(entity); |
| | 145 | |
|
| 10 | 146 | | if (updateCollisions) |
| 0 | 147 | | ConfigureColliders(entity); |
| | 148 | |
|
| 10 | 149 | | RaiseOnShapeUpdated(entity); |
| | 150 | | } |
| | 151 | |
|
| 79 | 152 | | previousModel = model; |
| 79 | 153 | | return null; |
| | 154 | | } |
| | 155 | |
|
| | 156 | | protected virtual void AttachShape(IDCLEntity entity) |
| | 157 | | { |
| 60 | 158 | | ContentProvider provider = null; |
| | 159 | |
|
| 60 | 160 | | if (!string.IsNullOrEmpty(model.assetId)) |
| | 161 | | { |
| 1 | 162 | | provider = AssetCatalogBridge.i.GetContentProviderForAssetIdInSceneObjectCatalog(model.assetId); |
| | 163 | |
|
| 1 | 164 | | if (string.IsNullOrEmpty(model.src)) |
| | 165 | | { |
| 0 | 166 | | SceneObject sceneObject = AssetCatalogBridge.i.GetSceneObjectById(model.assetId); |
| | 167 | |
|
| 0 | 168 | | if (sceneObject == null) |
| | 169 | | { |
| | 170 | | #if UNITY_EDITOR |
| 0 | 171 | | Debug.LogWarning($"LoadableShape '{model.assetId}' not found in catalog, probably asset pack del |
| | 172 | | #endif |
| 0 | 173 | | failed = true; |
| 0 | 174 | | OnLoadFailed(null, new Exception($"LoadableShape '{model.assetId}' not found in catalog, probabl |
| 0 | 175 | | return; |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | model.src = sceneObject.model; |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| 60 | 182 | | if (provider == null) |
| 59 | 183 | | provider = scene.contentProvider; |
| | 184 | |
|
| 60 | 185 | | if (provider.HasContentsUrl(model.src)) |
| | 186 | | { |
| 60 | 187 | | isLoaded = false; |
| 60 | 188 | | entity.EnsureMeshGameObject(componentName + " mesh"); |
| | 189 | |
|
| 60 | 190 | | LoadWrapperType loadableShape = |
| | 191 | | Environment.i.world.state.GetOrAddLoaderForEntity<LoadWrapperType>(entity); |
| | 192 | |
|
| 60 | 193 | | if (loadableShape is LoadWrapper_GLTF gltfLoadWrapper) |
| 59 | 194 | | gltfLoadWrapper.customContentProvider = provider; |
| | 195 | |
|
| 60 | 196 | | entity.meshesInfo.currentShape = this; |
| | 197 | |
|
| 60 | 198 | | loadableShape.entity = entity; |
| | 199 | |
|
| 60 | 200 | | bool initialVisibility = model.visible; |
| | 201 | |
|
| 60 | 202 | | if (!DataStore.i.debugConfig.isDebugMode.Get()) |
| 53 | 203 | | initialVisibility &= entity.isInsideSceneBoundaries; |
| | 204 | |
|
| 60 | 205 | | loadableShape.initialVisibility = initialVisibility; |
| | 206 | |
|
| 60 | 207 | | loadableShape.Load(model.src, OnLoadCompleted, OnLoadFailed); |
| | 208 | | } |
| | 209 | | else |
| | 210 | | { |
| 0 | 211 | | var message = $"LoadableShape '{model.src}' not found"; |
| | 212 | |
|
| 0 | 213 | | if (DataStore.i.debugConfig.isDebugMode.Get()) |
| | 214 | | { |
| 0 | 215 | | Debug.LogError(message); |
| | 216 | | } |
| | 217 | | else |
| | 218 | | { |
| | 219 | | #if UNITY_EDITOR |
| 0 | 220 | | Debug.LogWarning(message); |
| | 221 | | #endif |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | failed = true; |
| | 225 | | } |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | void ConfigureVisibility(IDCLEntity entity) |
| | 229 | | { |
| 72 | 230 | | var loadable = Environment.i.world.state.GetLoaderForEntity(entity); |
| | 231 | |
|
| 72 | 232 | | bool initialVisibility = model.visible; |
| | 233 | |
|
| 72 | 234 | | if (!DataStore.i.debugConfig.isDebugMode.Get()) |
| 62 | 235 | | initialVisibility &= entity.isInsideSceneBoundaries; |
| | 236 | |
|
| 72 | 237 | | if (loadable != null) |
| 72 | 238 | | loadable.initialVisibility = initialVisibility; |
| | 239 | |
|
| 72 | 240 | | ConfigureVisibility(entity.meshRootGameObject, initialVisibility, entity.meshesInfo.renderers); |
| | 241 | |
|
| 72 | 242 | | UpdateAnimationStatus(entity, initialVisibility); |
| 72 | 243 | | } |
| | 244 | |
|
| | 245 | | private void UpdateAnimationStatus(IDCLEntity entity, bool enabled) |
| | 246 | | { |
| 99 | 247 | | if (!scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.ANIMATOR) && |
| | 248 | | entity.meshesInfo.animation != null) |
| 8 | 249 | | entity.meshesInfo.animation.enabled = enabled; |
| 99 | 250 | | } |
| | 251 | |
|
| | 252 | | protected virtual void ConfigureColliders(IDCLEntity entity) |
| | 253 | | { |
| 58 | 254 | | CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions && entity.isInsideScen |
| 58 | 255 | | } |
| | 256 | |
|
| | 257 | | protected void OnLoadFailed(LoadWrapper loadWrapper, Exception exception) |
| | 258 | | { |
| 0 | 259 | | if (loadWrapper != null) |
| 0 | 260 | | CleanFailedWrapper(loadWrapper); |
| | 261 | |
|
| 0 | 262 | | failed = true; |
| 0 | 263 | | OnFinishCallbacks?.Invoke(this); |
| 0 | 264 | | OnFinishCallbacks = null; |
| 0 | 265 | | } |
| | 266 | |
|
| | 267 | | private void CleanFailedWrapper(LoadWrapper loadWrapper) |
| | 268 | | { |
| 0 | 269 | | if (loadWrapper == null) |
| 0 | 270 | | return; |
| | 271 | |
|
| 0 | 272 | | if (loadWrapper.entity == null) |
| 0 | 273 | | return; |
| | 274 | |
|
| 0 | 275 | | if (loadWrapper.entity.gameObject == null) |
| 0 | 276 | | return; |
| | 277 | |
|
| 0 | 278 | | GameObject go = loadWrapper.entity.gameObject; |
| | 279 | |
|
| 0 | 280 | | go.name += " - Failed loading"; |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | protected void OnLoadCompleted(LoadWrapper loadWrapper) |
| | 284 | | { |
| 65 | 285 | | IDCLEntity entity = loadWrapper.entity; |
| | 286 | |
|
| 65 | 287 | | if (entity.meshesInfo.currentShape == null) |
| | 288 | | { |
| 0 | 289 | | OnLoadFailed(loadWrapper, new Exception($"Entity {entity.entityId} current shape of mesh information is |
| 0 | 290 | | return; |
| | 291 | | } |
| | 292 | |
|
| 65 | 293 | | isLoaded = true; |
| 65 | 294 | | OnLoaded?.Invoke(this); |
| | 295 | |
|
| 65 | 296 | | entity.meshesInfo.meshRootGameObject = entity.meshRootGameObject; |
| 65 | 297 | | entity.OnInsideBoundariesChanged += OnInsideBoundariesChanged; |
| | 298 | |
|
| 65 | 299 | | ConfigureVisibility(entity); |
| 65 | 300 | | ConfigureColliders(entity); |
| 65 | 301 | | RaiseOnShapeUpdated(entity); |
| 65 | 302 | | RaiseOnShapeLoaded(entity); |
| | 303 | |
|
| 65 | 304 | | OnFinishCallbacks?.Invoke(this); |
| 65 | 305 | | OnFinishCallbacks = null; |
| 65 | 306 | | } |
| | 307 | |
|
| | 308 | | private void OnInsideBoundariesChanged(IDCLEntity entity, bool isInsideBoundaries) |
| | 309 | | { |
| 27 | 310 | | UpdateAnimationStatus(entity, isInsideBoundaries); |
| 27 | 311 | | } |
| | 312 | |
|
| | 313 | | protected virtual void DetachShape(IDCLEntity entity) |
| | 314 | | { |
| 49 | 315 | | if (entity == null || entity.meshRootGameObject == null) |
| 0 | 316 | | return; |
| | 317 | |
|
| 49 | 318 | | LoadWrapper loadWrapper = Environment.i.world.state.GetLoaderForEntity(entity); |
| | 319 | |
|
| 49 | 320 | | if (loadWrapper != null) |
| | 321 | | { |
| 49 | 322 | | entity.OnInsideBoundariesChanged -= OnInsideBoundariesChanged; |
| 49 | 323 | | loadWrapper.Unload(); |
| | 324 | | } |
| | 325 | |
|
| 49 | 326 | | Environment.i.world.state.RemoveLoaderForEntity(entity); |
| 49 | 327 | | entity.meshesInfo.CleanReferences(); |
| 49 | 328 | | } |
| | 329 | |
|
| | 330 | | public override void CallWhenReady(Action<ISharedComponent> callback) |
| | 331 | | { |
| 4 | 332 | | if (attachedEntities.Count == 0 || isLoaded || failed) { callback.Invoke(this); } |
| 2 | 333 | | else { OnFinishCallbacks += callback; } |
| 2 | 334 | | } |
| | 335 | |
|
| | 336 | | private void RaiseOnShapeUpdated(IDCLEntity entity) |
| | 337 | | { |
| 75 | 338 | | if (!isLoaded) |
| 4 | 339 | | return; |
| | 340 | |
|
| 71 | 341 | | entity.OnShapeUpdated?.Invoke(entity); |
| 71 | 342 | | } |
| | 343 | |
|
| | 344 | | private void RaiseOnShapeLoaded(IDCLEntity entity) |
| | 345 | | { |
| 65 | 346 | | if (!isLoaded) |
| 0 | 347 | | return; |
| | 348 | |
|
| 65 | 349 | | entity.OnShapeLoaded?.Invoke(entity); |
| 5 | 350 | | } |
| | 351 | | } |
| | 352 | | } |