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