| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public abstract class ParametrizedShape<T> : BaseShape |
| | 11 | | where T : BaseShape.Model, new() |
| | 12 | | { |
| 176 | 13 | | public Dictionary<IDCLEntity, Rendereable> attachedRendereables = new Dictionary<IDCLEntity, Rendereable>(); |
| | 14 | | bool visibilityDirty = false; |
| | 15 | | bool collisionsDirty = false; |
| | 16 | |
|
| | 17 | | public virtual Mesh GenerateGeometry() |
| | 18 | | { |
| 0 | 19 | | return null; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | protected virtual void DestroyGeometry() |
| | 23 | | { |
| 47 | 24 | | if (currentMesh == null) |
| 0 | 25 | | return; |
| | 26 | |
|
| 47 | 27 | | Object.Destroy(currentMesh); |
| 47 | 28 | | currentMesh = null; |
| 47 | 29 | | } |
| | 30 | |
|
| 0 | 31 | | public Mesh currentMesh { get; protected set; } |
| | 32 | | private Model previousModel; |
| | 33 | | private Model cachedModel; |
| | 34 | |
|
| 176 | 35 | | public ParametrizedShape() |
| | 36 | | { |
| 176 | 37 | | OnAttach += OnShapeAttached; |
| 176 | 38 | | OnDetach += OnShapeDetached; |
| 176 | 39 | | } |
| | 40 | |
|
| | 41 | | public override void UpdateFromModel(BaseModel newModel) |
| | 42 | | { |
| 247 | 43 | | cachedModel = (Model) newModel; |
| 247 | 44 | | base.UpdateFromModel(newModel); |
| 247 | 45 | | } |
| | 46 | |
|
| | 47 | | void UpdateRenderer(IDCLEntity entity, Model model = null) |
| | 48 | | { |
| 267 | 49 | | if (model == null) |
| 191 | 50 | | model = (T) this.model; |
| | 51 | |
|
| 267 | 52 | | if (visibilityDirty) |
| | 53 | | { |
| 226 | 54 | | ConfigureVisibility(entity.meshRootGameObject, model.visible && entity.isInsideSceneBoundaries, entity.m |
| 226 | 55 | | visibilityDirty = false; |
| | 56 | | } |
| | 57 | |
|
| 267 | 58 | | if (collisionsDirty) |
| | 59 | | { |
| 214 | 60 | | CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions && entity.isInside |
| 214 | 61 | | collisionsDirty = false; |
| | 62 | | } |
| | 63 | |
|
| 267 | 64 | | if (entity.meshesInfo.meshFilters.Length > 0 && entity.meshesInfo.meshFilters[0].sharedMesh != currentMesh) |
| | 65 | | { |
| 3 | 66 | | entity.meshesInfo.UpdateExistingMeshAtIndex(currentMesh, 0); |
| | 67 | | } |
| | 68 | |
|
| 267 | 69 | | DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); |
| 267 | 70 | | } |
| | 71 | |
|
| | 72 | | void OnShapeAttached(IDCLEntity entity) |
| | 73 | | { |
| 191 | 74 | | if (entity == null) |
| 0 | 75 | | return; |
| | 76 | |
|
| | 77 | | // First we remove the old rendereable, then we compute and add the new one. |
| 191 | 78 | | RemoveRendereableFromDataStore(entity); |
| | 79 | |
|
| 191 | 80 | | entity.EnsureMeshGameObject(componentName + " mesh"); |
| | 81 | |
|
| 191 | 82 | | if (currentMesh == null) |
| | 83 | | { |
| 166 | 84 | | currentMesh = GenerateGeometry(); |
| | 85 | | } |
| | 86 | |
|
| 191 | 87 | | MeshFilter meshFilter = entity.meshRootGameObject.AddComponent<MeshFilter>(); |
| 191 | 88 | | MeshRenderer meshRenderer = entity.meshRootGameObject.AddComponent<MeshRenderer>(); |
| | 89 | |
|
| 191 | 90 | | entity.meshesInfo.renderers = new Renderer[] { meshRenderer }; |
| 191 | 91 | | entity.meshesInfo.currentShape = this; |
| | 92 | |
|
| 191 | 93 | | meshFilter.sharedMesh = currentMesh; |
| | 94 | |
|
| 191 | 95 | | if (Configuration.ParcelSettings.VISUAL_LOADING_ENABLED) |
| | 96 | | { |
| 1 | 97 | | MaterialTransitionController transition = entity.meshRootGameObject.AddComponent<MaterialTransitionContr |
| 1 | 98 | | Material finalMaterial = Utils.EnsureResourcesMaterial("Materials/Default"); |
| 1 | 99 | | transition.delay = 0; |
| 1 | 100 | | transition.useHologram = false; |
| 1 | 101 | | transition.fadeThickness = 20; |
| 1 | 102 | | transition.OnDidFinishLoading(finalMaterial); |
| | 103 | |
|
| 3 | 104 | | transition.onFinishedLoading += () => { OnShapeFinishedLoading(entity); }; |
| 1 | 105 | | } |
| | 106 | | else |
| | 107 | | { |
| 190 | 108 | | meshRenderer.sharedMaterial = Utils.EnsureResourcesMaterial("Materials/Default"); |
| | 109 | | } |
| | 110 | |
|
| 191 | 111 | | visibilityDirty = true; |
| 191 | 112 | | collisionsDirty = true; |
| 191 | 113 | | UpdateRenderer(entity); |
| 191 | 114 | | OnShapeFinishedLoading(entity); |
| 191 | 115 | | AddRendereableToDataStore(entity); |
| 191 | 116 | | } |
| | 117 | |
|
| | 118 | |
|
| | 119 | | async UniTaskVoid OnShapeFinishedLoading(IDCLEntity entity) |
| | 120 | | { |
| | 121 | | // We need to wait for a frame so that MaterialTransitionController has been destroyed. |
| 192 | 122 | | await UniTask.Yield(); |
| | 123 | |
|
| 192 | 124 | | entity.OnShapeUpdated?.Invoke(entity); |
| 192 | 125 | | DCL.Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); |
| 192 | 126 | | } |
| | 127 | |
|
| | 128 | | void OnShapeDetached(IDCLEntity entity) |
| | 129 | | { |
| 147 | 130 | | if (entity == null || entity.meshRootGameObject == null) |
| 0 | 131 | | return; |
| | 132 | |
|
| 147 | 133 | | if (attachedEntities.Count == 0) |
| | 134 | | { |
| 122 | 135 | | DestroyGeometry(); |
| 122 | 136 | | Utils.CleanMaterials(entity.meshRootGameObject.GetComponent<Renderer>()); |
| 122 | 137 | | currentMesh = null; |
| | 138 | | } |
| | 139 | |
|
| 147 | 140 | | Utils.SafeDestroy(entity.meshRootGameObject); |
| 147 | 141 | | entity.meshesInfo.CleanReferences(); |
| | 142 | |
|
| 147 | 143 | | RemoveRendereableFromDataStore(entity); |
| 147 | 144 | | } |
| | 145 | |
|
| | 146 | | public override IEnumerator ApplyChanges(BaseModel newModelRaw) |
| | 147 | | { |
| 247 | 148 | | var newModel = (T) newModelRaw; |
| | 149 | |
|
| 247 | 150 | | if (previousModel != null) |
| | 151 | | { |
| 71 | 152 | | visibilityDirty = newModel.visible != previousModel.visible; |
| 71 | 153 | | collisionsDirty = newModel.withCollisions != previousModel.withCollisions || newModel.isPointerBlocker ! |
| | 154 | | } |
| | 155 | |
|
| 247 | 156 | | bool shouldGenerateMesh = ShouldGenerateNewMesh(previousModel); |
| | 157 | |
|
| | 158 | | //NOTE(Brian): Only generate meshes here if they already are attached to something. |
| | 159 | | // Otherwise, the mesh will be created on the OnShapeAttached. |
| 247 | 160 | | if (attachedEntities.Count > 0) |
| | 161 | | { |
| 60 | 162 | | using (var iterator = attachedEntities.GetEnumerator()) |
| | 163 | | { |
| 136 | 164 | | while (iterator.MoveNext()) |
| | 165 | | { |
| 76 | 166 | | var entity = iterator.Current; |
| 76 | 167 | | RemoveRendereableFromDataStore(entity); |
| | 168 | | } |
| 60 | 169 | | } |
| | 170 | |
|
| 60 | 171 | | if (shouldGenerateMesh) |
| | 172 | | { |
| 6 | 173 | | DestroyGeometry(); |
| 6 | 174 | | currentMesh = GenerateGeometry(); |
| | 175 | | } |
| | 176 | |
|
| 60 | 177 | | using (var iterator = attachedEntities.GetEnumerator()) |
| | 178 | | { |
| 60 | 179 | | bool cachedVisibilityDirty = visibilityDirty; |
| 60 | 180 | | bool cachedCollisionDirty = collisionsDirty; |
| 136 | 181 | | while (iterator.MoveNext()) |
| | 182 | | { |
| | 183 | | //NOTE(Alex): Since UpdateRenderer updates the dirty flags as well we have to make sure every en |
| | 184 | | // gets updated accordingly to the original flags. |
| 76 | 185 | | visibilityDirty = cachedVisibilityDirty; |
| 76 | 186 | | collisionsDirty = cachedCollisionDirty; |
| | 187 | |
|
| 76 | 188 | | var entity = iterator.Current; |
| 76 | 189 | | UpdateRenderer(entity, newModel); |
| | 190 | |
|
| 76 | 191 | | entity.OnShapeUpdated?.Invoke(entity); |
| | 192 | | } |
| 60 | 193 | | } |
| | 194 | |
|
| 60 | 195 | | using (var iterator = attachedEntities.GetEnumerator()) |
| | 196 | | { |
| 136 | 197 | | while (iterator.MoveNext()) |
| | 198 | | { |
| 76 | 199 | | var entity = iterator.Current; |
| 76 | 200 | | AddRendereableToDataStore(entity); |
| | 201 | | } |
| 60 | 202 | | } |
| | 203 | | } |
| | 204 | |
|
| 247 | 205 | | previousModel = newModel; |
| 247 | 206 | | return null; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 210 | | { |
| 191 | 211 | | if (attachedEntities.Contains(entity)) |
| 0 | 212 | | return; |
| | 213 | |
|
| 191 | 214 | | base.AttachTo(entity); |
| 191 | 215 | | } |
| | 216 | |
|
| 119 | 217 | | public override bool IsVisible() { return cachedModel.visible; } |
| | 218 | |
|
| 149 | 219 | | public override bool HasCollisions() { return cachedModel.withCollisions; } |
| | 220 | |
|
| 0 | 221 | | protected virtual bool ShouldGenerateNewMesh(BaseShape.Model newModel) { return true; } |
| | 222 | |
|
| | 223 | | private void RemoveRendereableFromDataStore(IDCLEntity entity) |
| | 224 | | { |
| 414 | 225 | | if (!attachedRendereables.ContainsKey(entity)) |
| 191 | 226 | | return; |
| | 227 | |
|
| 223 | 228 | | DataStore.i.sceneWorldObjects.RemoveRendereable(entity.scene.sceneData.id, attachedRendereables[entity]); |
| 223 | 229 | | attachedRendereables.Remove(entity); |
| 223 | 230 | | } |
| | 231 | |
|
| | 232 | | private void AddRendereableToDataStore(IDCLEntity entity) |
| | 233 | | { |
| 267 | 234 | | if (attachedRendereables.ContainsKey(entity)) |
| 0 | 235 | | return; |
| | 236 | |
|
| 267 | 237 | | int triangleCount = currentMesh.triangles.Length; |
| | 238 | |
|
| 267 | 239 | | var newRendereable = |
| | 240 | | new Rendereable() |
| | 241 | | { |
| | 242 | | container = entity.meshRootGameObject, |
| | 243 | | totalTriangleCount = triangleCount, |
| | 244 | | meshes = new HashSet<Mesh>() { currentMesh }, |
| | 245 | | meshToTriangleCount = new Dictionary<Mesh, int>() { { currentMesh, triangleCount } } |
| | 246 | | }; |
| | 247 | |
|
| 267 | 248 | | newRendereable.renderers = MeshesInfoUtils.ExtractUniqueRenderers(entity.meshRootGameObject); |
| 267 | 249 | | newRendereable.ownerId = entity.entityId; |
| | 250 | |
|
| 267 | 251 | | attachedRendereables.Add(entity, newRendereable); |
| 267 | 252 | | DataStore.i.sceneWorldObjects.AddRendereable(entity.scene.sceneData.id, newRendereable); |
| 267 | 253 | | } |
| | 254 | | } |
| | 255 | | } |