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