| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Models; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public class BIWEntity |
| | 12 | | { |
| 1 | 13 | | public GameObject gameObject => rootEntity.gameObject; |
| 0 | 14 | | public Transform transform => rootEntity.gameObject.transform; |
| | 15 | |
|
| 0 | 16 | | public IDCLEntity rootEntity { protected set; get; } |
| | 17 | | public string entityUniqueId; |
| | 18 | |
|
| | 19 | | public event Action<BIWEntity> OnShapeFinishLoading; |
| | 20 | | public event Action<BIWEntity> OnStatusUpdate; |
| | 21 | | public event Action<BIWEntity> OnDelete; |
| | 22 | | public event Action<BIWEntity> OnErrorStatusChange; |
| | 23 | |
|
| 0 | 24 | | public bool isDeleted { get; private set; } |
| | 25 | |
|
| | 26 | | public bool isLocked |
| | 27 | | { |
| 99 | 28 | | get { return GetIsLockedValue(); } |
| | 29 | | set |
| | 30 | | { |
| 45 | 31 | | SetIsLockedValue(value); |
| 45 | 32 | | OnStatusUpdate?.Invoke(this); |
| 0 | 33 | | } |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private bool isSelectedValue = false; |
| | 37 | |
|
| | 38 | | public bool isSelected |
| | 39 | | { |
| 0 | 40 | | get { return isSelectedValue; } |
| | 41 | | set |
| | 42 | | { |
| 39 | 43 | | isSelectedValue = value; |
| 39 | 44 | | OnStatusUpdate?.Invoke(this); |
| 0 | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | private bool isNewValue = false; |
| | 49 | |
|
| | 50 | | public bool isNew |
| | 51 | | { |
| 0 | 52 | | get { return isNewValue; } |
| | 53 | | set |
| | 54 | | { |
| 114 | 55 | | isNewValue = value; |
| 114 | 56 | | OnStatusUpdate?.Invoke(this); |
| 0 | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| 145 | 60 | | private bool isVisibleValue = true; |
| | 61 | |
|
| | 62 | | public bool isVisible |
| | 63 | | { |
| 0 | 64 | | get { return isVisibleValue; } |
| | 65 | | set |
| | 66 | | { |
| 156 | 67 | | isVisibleValue = value; |
| | 68 | |
|
| 156 | 69 | | if (rootEntity != null && rootEntity.gameObject != null) |
| | 70 | | { |
| 154 | 71 | | rootEntity.gameObject.SetActive(isVisibleValue); |
| | 72 | | } |
| | 73 | |
|
| 156 | 74 | | OnStatusUpdate?.Invoke(this); |
| 0 | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | public bool isVoxel { get; set; } = false; |
| | 79 | |
|
| | 80 | | private CatalogItem associatedCatalogItem; |
| | 81 | |
|
| 0 | 82 | | public bool isFloor { get; set; } = false; |
| 0 | 83 | | public bool isNFT { get; private set; } = false; |
| | 84 | |
|
| | 85 | | private bool isShapeComponentSet = false; |
| | 86 | |
|
| | 87 | | private Animation[] meshAnimations; |
| | 88 | |
|
| | 89 | | private Vector3 currentRotation; |
| | 90 | | private Transform originalParent; |
| | 91 | |
|
| | 92 | | private Material[] originalMaterials; |
| | 93 | |
|
| | 94 | | private Material editMaterial; |
| | 95 | |
|
| 145 | 96 | | private Dictionary<string, List<GameObject>> collidersGameObjectDictionary = new Dictionary<string, List<GameObject> |
| | 97 | |
|
| | 98 | | private Vector3 lastPositionReported; |
| | 99 | | private Vector3 lastScaleReported; |
| | 100 | | private Quaternion lastRotationReported; |
| | 101 | |
|
| | 102 | | #region Error Handler definition |
| | 103 | |
|
| 0 | 104 | | public bool hasError { get; private set; } = false; |
| | 105 | |
|
| 0 | 106 | | public bool hasMissingCatalogItemError { get; private set; } = false; |
| 0 | 107 | | public bool isInsideBoundariesError { get; private set; } = false; |
| | 108 | |
|
| | 109 | | #endregion |
| | 110 | |
|
| | 111 | | public void Initialize(IDCLEntity entity, Material editMaterial) |
| | 112 | | { |
| 119 | 113 | | rootEntity = entity; |
| 119 | 114 | | rootEntity.OnShapeUpdated += OnShapeUpdate; |
| 119 | 115 | | rootEntity.OnNameChange += OnNameUpdate; |
| | 116 | |
|
| 119 | 117 | | this.editMaterial = editMaterial; |
| 119 | 118 | | isVoxel = false; |
| | 119 | |
|
| | 120 | |
|
| 119 | 121 | | entityUniqueId = rootEntity.scene.sceneData.id + rootEntity.entityId; |
| 119 | 122 | | if (rootEntity.gameObject != null) |
| 118 | 123 | | isVisible = rootEntity.gameObject.activeSelf; |
| | 124 | |
|
| 119 | 125 | | isShapeComponentSet = false; |
| 119 | 126 | | InitRotation(); |
| | 127 | |
|
| 119 | 128 | | if (rootEntity.meshRootGameObject && rootEntity.meshesInfo.renderers.Length > 0) |
| | 129 | | { |
| 7 | 130 | | ShapeInit(); |
| | 131 | | } |
| 119 | 132 | | } |
| | 133 | |
|
| | 134 | | public CatalogItem GetCatalogItemAssociated() |
| | 135 | | { |
| 279 | 136 | | if (associatedCatalogItem != null) |
| 44 | 137 | | return associatedCatalogItem; |
| | 138 | |
|
| 235 | 139 | | if (rootEntity == null) |
| 1 | 140 | | return null; |
| | 141 | |
|
| | 142 | | //We get the catalog reference |
| 234 | 143 | | IAssetCatalogReferenceHolder catalogHolder = rootEntity.TryGetComponent<IAssetCatalogReferenceHolder>(); |
| 234 | 144 | | if (catalogHolder == null) |
| 183 | 145 | | return null; |
| | 146 | |
|
| | 147 | | //we get the assetId to search in the catalog for the item |
| 51 | 148 | | string assetId = catalogHolder.GetAssetId(); |
| | 149 | |
|
| | 150 | | //We try to get the item from the main catalog that is usable right now |
| 51 | 151 | | if (!string.IsNullOrEmpty(assetId) && DataStore.i.builderInWorld.catalogItemDict.TryGetValue(assetId, out associ |
| 14 | 152 | | return associatedCatalogItem; |
| | 153 | |
|
| | 154 | | //If the item doesn't exist in the catalog, we fallback to the catalog of the scene |
| 37 | 155 | | if (!string.IsNullOrEmpty(assetId) && DataStore.i.builderInWorld.currentSceneCatalogItemDict.TryGetValue(assetId |
| 0 | 156 | | return associatedCatalogItem; |
| | 157 | |
|
| | 158 | | //Error 404: Item not found, we show a pink box to represent the item |
| 37 | 159 | | return null; |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | public bool HasShape() { return isShapeComponentSet; } |
| | 163 | |
|
| 0 | 164 | | public bool HasMovedSinceLastReport() { return Vector3.Distance(lastPositionReported, rootEntity.gameObject.transfor |
| | 165 | |
|
| 0 | 166 | | public bool HasScaledSinceLastReport() { return Math.Abs(lastScaleReported.magnitude - rootEntity.gameObject.transfo |
| | 167 | |
|
| 0 | 168 | | public bool HasRotatedSinceLastReport() { return Quaternion.Angle(lastRotationReported, rootEntity.gameObject.transf |
| | 169 | |
|
| 0 | 170 | | public void PositionReported() { lastPositionReported = rootEntity.gameObject.transform.position; } |
| | 171 | |
|
| 0 | 172 | | public void ScaleReported() { lastScaleReported = rootEntity.gameObject.transform.lossyScale; } |
| | 173 | |
|
| 0 | 174 | | public void RotationReported() { lastRotationReported = rootEntity.gameObject.transform.rotation; } |
| | 175 | |
|
| | 176 | | #region Error Handling |
| | 177 | |
|
| | 178 | | public void CheckErrors() |
| | 179 | | { |
| 110 | 180 | | bool isCurrentlyWithError = false; |
| | 181 | |
|
| | 182 | | //If the entity doesn't have a catalog item associated, we can be sure that the item is deleted |
| 110 | 183 | | if (GetCatalogItemAssociated() == null) |
| | 184 | | { |
| 86 | 185 | | hasMissingCatalogItemError = true; |
| 86 | 186 | | isCurrentlyWithError = true; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | //If entity is not inside boundaries it has an error |
| 110 | 190 | | if (isInsideBoundariesError) |
| 49 | 191 | | isCurrentlyWithError = true; |
| | 192 | |
|
| 110 | 193 | | bool hasErrorPreviously = hasError; |
| 110 | 194 | | hasError = isCurrentlyWithError; |
| | 195 | |
|
| 110 | 196 | | if (isCurrentlyWithError != hasErrorPreviously) |
| 80 | 197 | | OnErrorStatusChange?.Invoke(this); |
| 44 | 198 | | } |
| | 199 | |
|
| | 200 | | public void SetEntityBoundariesError(bool isInsideBoundaries) |
| | 201 | | { |
| 48 | 202 | | isInsideBoundariesError = !isInsideBoundaries; |
| 48 | 203 | | CheckErrors(); |
| 48 | 204 | | } |
| | 205 | |
|
| | 206 | | #endregion |
| | 207 | |
|
| | 208 | | public void Select() |
| | 209 | | { |
| 26 | 210 | | isSelected = true; |
| 26 | 211 | | originalParent = rootEntity.gameObject.transform.parent; |
| 26 | 212 | | SetEditMaterial(); |
| 26 | 213 | | lastPositionReported = rootEntity.gameObject.transform.position; |
| 26 | 214 | | lastScaleReported = rootEntity.gameObject.transform.lossyScale; |
| 26 | 215 | | lastRotationReported = rootEntity.gameObject.transform.rotation; |
| 26 | 216 | | } |
| | 217 | |
|
| | 218 | | public void Deselect() |
| | 219 | | { |
| 25 | 220 | | if (!isSelected) |
| 12 | 221 | | return; |
| | 222 | |
|
| 13 | 223 | | isNew = false; |
| 13 | 224 | | isSelected = false; |
| 13 | 225 | | if (rootEntity.gameObject != null) |
| 13 | 226 | | rootEntity.gameObject.transform.SetParent(originalParent); |
| | 227 | |
|
| 13 | 228 | | SetOriginalMaterials(); |
| 13 | 229 | | } |
| | 230 | |
|
| | 231 | | public void ToggleShowStatus() |
| | 232 | | { |
| 3 | 233 | | rootEntity.gameObject.SetActive(!rootEntity.gameObject.activeSelf); |
| 3 | 234 | | isVisible = rootEntity.gameObject.activeSelf; |
| 3 | 235 | | OnStatusUpdate?.Invoke(this); |
| 0 | 236 | | } |
| | 237 | |
|
| 6 | 238 | | public void ToggleLockStatus() { isLocked = !isLocked; } |
| | 239 | |
|
| 24 | 240 | | public void ShapeLoadFinish(ISharedComponent component) { OnShapeFinishLoading?.Invoke(this); } |
| | 241 | |
|
| | 242 | | public void Delete() |
| | 243 | | { |
| 13 | 244 | | Deselect(); |
| 13 | 245 | | Dispose(); |
| 13 | 246 | | isDeleted = true; |
| 13 | 247 | | OnDelete?.Invoke(this); |
| 1 | 248 | | } |
| | 249 | |
|
| | 250 | | public void Dispose() |
| | 251 | | { |
| 17 | 252 | | if (rootEntity != null) |
| | 253 | | { |
| 17 | 254 | | rootEntity.OnShapeUpdated -= OnShapeUpdate; |
| 17 | 255 | | rootEntity.OnNameChange -= OnNameUpdate; |
| | 256 | |
|
| 17 | 257 | | if (isNFT) |
| | 258 | | { |
| 0 | 259 | | foreach (KeyValuePair<Type, ISharedComponent> kvp in rootEntity.sharedComponents) |
| | 260 | | { |
| 0 | 261 | | if (kvp.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE) |
| | 262 | | { |
| 0 | 263 | | BIWNFTController.i.StopUsingNFT(((NFTShape.Model) kvp.Value.GetModel()).assetId); |
| 0 | 264 | | break; |
| | 265 | | } |
| | 266 | | } |
| | 267 | | } |
| | 268 | |
|
| 17 | 269 | | DCL.Environment.i.world.sceneBoundsChecker?.EvaluateEntityPosition(rootEntity); |
| 17 | 270 | | DCL.Environment.i.world.sceneBoundsChecker?.RemoveEntityToBeChecked(rootEntity); |
| | 271 | | } |
| | 272 | |
|
| 17 | 273 | | DestroyColliders(); |
| | 274 | |
|
| 17 | 275 | | associatedCatalogItem = null; |
| 17 | 276 | | } |
| | 277 | |
|
| | 278 | | public void DestroyColliders() |
| | 279 | | { |
| 212 | 280 | | foreach (List<GameObject> entityColliderGameObject in collidersGameObjectDictionary.Values) |
| | 281 | | { |
| 40 | 282 | | for (int i = entityColliderGameObject.Count - 1; i > 0; i--) |
| | 283 | | { |
| 0 | 284 | | GameObject.Destroy(entityColliderGameObject[i]); |
| | 285 | | } |
| | 286 | | } |
| | 287 | |
|
| 86 | 288 | | collidersGameObjectDictionary.Clear(); |
| 86 | 289 | | } |
| | 290 | |
|
| | 291 | | #region Components |
| | 292 | |
|
| | 293 | | #region Transfrom |
| | 294 | |
|
| 0 | 295 | | public void AddRotation(Vector3 newRotation) { currentRotation += newRotation; } |
| | 296 | |
|
| 0 | 297 | | public void SetRotation(Vector3 newRotation) { currentRotation = newRotation; } |
| | 298 | |
|
| 0 | 299 | | public Vector3 GetEulerRotation() { return currentRotation; } |
| | 300 | |
|
| | 301 | | public void InitRotation() |
| | 302 | | { |
| | 303 | | //TODO : We need to implement the initial rotation from the transform component instead of getting the current r |
| 119 | 304 | | if (rootEntity.gameObject != null) |
| 118 | 305 | | currentRotation = rootEntity.gameObject.transform.eulerAngles; |
| 119 | 306 | | } |
| | 307 | |
|
| | 308 | | #endregion |
| | 309 | |
|
| | 310 | | #region SmartItem |
| | 311 | |
|
| | 312 | | public bool HasSmartItemComponent() |
| | 313 | | { |
| 49 | 314 | | if (rootEntity == null) |
| 1 | 315 | | return false; |
| | 316 | |
|
| 48 | 317 | | return rootEntity.components.ContainsKey(CLASS_ID_COMPONENT.SMART_ITEM); |
| | 318 | | } |
| | 319 | |
|
| 0 | 320 | | public bool HasSmartItemActions() { return GetCatalogItemAssociated().HasActions(); } |
| | 321 | |
|
| 0 | 322 | | public SmartItemParameter[] GetSmartItemParameters() { return GetCatalogItemAssociated().parameters; } |
| | 323 | |
|
| 0 | 324 | | public SmartItemAction[] GetSmartItemActions() { return GetCatalogItemAssociated().actions; } |
| | 325 | |
|
| | 326 | | #endregion |
| | 327 | |
|
| | 328 | | #region Locked |
| | 329 | |
|
| | 330 | | public bool GetIsLockedValue() |
| | 331 | | { |
| 260 | 332 | | foreach (KeyValuePair<Type, ISharedComponent> kvp in rootEntity.sharedComponents) |
| | 333 | | { |
| 40 | 334 | | if (kvp.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT) |
| | 335 | | { |
| 18 | 336 | | return ((DCLLockedOnEdit.Model) kvp.Value.GetModel()).isLocked; |
| | 337 | | } |
| | 338 | | } |
| | 339 | |
|
| 81 | 340 | | return isFloor; |
| 18 | 341 | | } |
| | 342 | |
|
| | 343 | | public void SetIsLockedValue(bool isLocked) |
| | 344 | | { |
| 47 | 345 | | bool foundComponent = false; |
| | 346 | |
|
| 144 | 347 | | foreach (KeyValuePair<Type, ISharedComponent> kvp in rootEntity.sharedComponents) |
| | 348 | | { |
| 25 | 349 | | if (kvp.Value.GetClassId() == (int) CLASS_ID.LOCKED_ON_EDIT) |
| | 350 | | { |
| 11 | 351 | | ((DCLLockedOnEdit) kvp.Value).SetIsLocked(isLocked); |
| 11 | 352 | | foundComponent = true; |
| | 353 | | } |
| | 354 | | } |
| | 355 | |
|
| 47 | 356 | | if (!foundComponent) |
| | 357 | | { |
| 36 | 358 | | ParcelScene scene = rootEntity.scene as ParcelScene; |
| 36 | 359 | | DCLLockedOnEdit entityLocked = (DCLLockedOnEdit) scene.SharedComponentCreate(Guid.NewGuid().ToString(), Conv |
| 36 | 360 | | entityLocked.SetIsLocked(isLocked); |
| 36 | 361 | | scene.SharedComponentAttach(rootEntity.entityId, entityLocked.id); |
| | 362 | | } |
| 47 | 363 | | } |
| | 364 | |
|
| | 365 | | #endregion |
| | 366 | |
|
| | 367 | | #region DescriptiveName |
| | 368 | |
|
| | 369 | | public void SetDescriptiveName(string newName) |
| | 370 | | { |
| 19 | 371 | | if (rootEntity.TryGetSharedComponent(CLASS_ID.NAME, out ISharedComponent nameComponent)) |
| | 372 | | { |
| 15 | 373 | | ((DCLName) nameComponent).SetNewName(newName); |
| 15 | 374 | | } |
| | 375 | | else |
| | 376 | | { |
| 4 | 377 | | ParcelScene scene = rootEntity.scene as ParcelScene; |
| 4 | 378 | | DCLName name = (DCLName) scene.SharedComponentCreate(Guid.NewGuid().ToString(), Convert.ToInt32(CLASS_ID.NAM |
| 4 | 379 | | name.SetNewName(newName); |
| 4 | 380 | | scene.SharedComponentAttach(rootEntity.entityId, name.id); |
| | 381 | | } |
| | 382 | |
|
| 19 | 383 | | OnStatusUpdate?.Invoke(this); |
| 0 | 384 | | } |
| | 385 | |
|
| | 386 | | public string GetDescriptiveName() |
| | 387 | | { |
| 145 | 388 | | if (rootEntity != null && rootEntity.TryGetSharedComponent(CLASS_ID.NAME, out ISharedComponent nameComponent)) |
| | 389 | | { |
| 22 | 390 | | return ((DCLName.Model) nameComponent.GetModel()).value; |
| | 391 | | } |
| | 392 | |
|
| 123 | 393 | | return ""; |
| | 394 | | } |
| | 395 | |
|
| | 396 | | #endregion |
| | 397 | |
|
| | 398 | | #endregion |
| | 399 | |
|
| | 400 | | public void ResetTransfrom() |
| | 401 | | { |
| 0 | 402 | | currentRotation = Vector3.zero; |
| 0 | 403 | | rootEntity.gameObject.transform.eulerAngles = currentRotation; |
| 0 | 404 | | rootEntity.gameObject.transform.localScale = Vector3.one; |
| 0 | 405 | | OnStatusUpdate?.Invoke(this); |
| 0 | 406 | | } |
| | 407 | |
|
| | 408 | | void ShapeInit() |
| | 409 | | { |
| 47 | 410 | | isShapeComponentSet = true; |
| | 411 | |
|
| 47 | 412 | | isFloor = IsEntityAFloor(); |
| 47 | 413 | | isNFT = IsEntityNFT(); |
| | 414 | |
|
| 47 | 415 | | CreateCollidersForEntity(rootEntity); |
| | 416 | |
|
| 47 | 417 | | if (isFloor) |
| 8 | 418 | | isLocked = true; |
| | 419 | |
|
| 47 | 420 | | if (IsEntityAVoxel()) |
| 7 | 421 | | SetEntityAsVoxel(); |
| | 422 | |
|
| 47 | 423 | | HandleAnimation(); |
| | 424 | |
|
| 47 | 425 | | if (isNFT) |
| | 426 | | { |
| 6 | 427 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in rootEntity.sharedComponents) |
| | 428 | | { |
| 2 | 429 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE) |
| | 430 | | { |
| 2 | 431 | | BIWNFTController.i.UseNFT(((NFTShape.Model) keyValuePairBaseDisposable.Value.GetModel()).assetId); |
| 2 | 432 | | break; |
| | 433 | | } |
| | 434 | | } |
| | 435 | | } |
| | 436 | |
|
| 47 | 437 | | SaveOriginalMaterial(); |
| | 438 | |
|
| 47 | 439 | | DCL.Environment.i.world.sceneBoundsChecker.AddPersistent(rootEntity); |
| 47 | 440 | | SetEntityBoundariesError(DCL.Environment.i.world.sceneBoundsChecker.IsEntityInsideSceneBoundaries(rootEntity)); |
| 47 | 441 | | } |
| | 442 | |
|
| | 443 | | private void HandleAnimation() |
| | 444 | | { |
| | 445 | | // We don't want animation to be running on editor |
| 47 | 446 | | meshAnimations = rootEntity.gameObject.GetComponentsInChildren<Animation>(); |
| 47 | 447 | | if (HasSmartItemComponent()) |
| | 448 | | { |
| 0 | 449 | | DefaultAnimationStop(); |
| 0 | 450 | | } |
| | 451 | | else |
| | 452 | | { |
| 47 | 453 | | DefaultAnimationSample(0); |
| | 454 | | } |
| 47 | 455 | | } |
| | 456 | |
|
| | 457 | | private void DefaultAnimationStop() |
| | 458 | | { |
| 0 | 459 | | if (meshAnimations == null) |
| 0 | 460 | | return; |
| | 461 | |
|
| 0 | 462 | | for (int i = 0; i < meshAnimations.Length; i++) |
| | 463 | | { |
| 0 | 464 | | meshAnimations[i].Stop(); |
| | 465 | | } |
| 0 | 466 | | } |
| | 467 | |
|
| | 468 | | private void DefaultAnimationSample(float time) |
| | 469 | | { |
| 47 | 470 | | if (meshAnimations == null || meshAnimations.Length == 0) |
| 47 | 471 | | return; |
| | 472 | |
|
| 0 | 473 | | for (int i = 0; i < meshAnimations.Length; i++) |
| | 474 | | { |
| 0 | 475 | | meshAnimations[i].Stop(); |
| 0 | 476 | | meshAnimations[i].clip?.SampleAnimation(meshAnimations[i].gameObject, time); |
| | 477 | | } |
| 0 | 478 | | } |
| | 479 | |
|
| | 480 | | void SetOriginalMaterials() |
| | 481 | | { |
| 13 | 482 | | if (rootEntity.meshesInfo.renderers == null) |
| 12 | 483 | | return; |
| 1 | 484 | | if (isNFT) |
| 0 | 485 | | return; |
| | 486 | |
|
| 1 | 487 | | int matCont = 0; |
| 2 | 488 | | foreach (Renderer renderer in rootEntity.meshesInfo.renderers) |
| | 489 | | { |
| 0 | 490 | | if (renderer == null) |
| | 491 | | continue; |
| 0 | 492 | | Material[] materials = new Material[renderer.sharedMaterials.Length]; |
| | 493 | |
|
| 0 | 494 | | for (int i = 0; i < renderer.sharedMaterials.Length; i++) |
| | 495 | | { |
| 0 | 496 | | if (isNFT && matCont == 0) |
| | 497 | | { |
| 0 | 498 | | materials[i] = renderer.sharedMaterials[i]; |
| 0 | 499 | | matCont++; |
| 0 | 500 | | continue; |
| | 501 | | } |
| | 502 | |
|
| 0 | 503 | | materials[i] = originalMaterials[matCont]; |
| 0 | 504 | | matCont++; |
| | 505 | | } |
| | 506 | |
|
| 0 | 507 | | renderer.sharedMaterials = materials; |
| | 508 | | } |
| 1 | 509 | | } |
| | 510 | |
|
| | 511 | | void SetEntityAsVoxel() |
| | 512 | | { |
| 7 | 513 | | isVoxel = true; |
| 7 | 514 | | rootEntity.gameObject.tag = BIWSettings.VOXEL_TAG; |
| 7 | 515 | | } |
| | 516 | |
|
| | 517 | | void SaveOriginalMaterial() |
| | 518 | | { |
| 47 | 519 | | if (rootEntity.meshesInfo == null || |
| | 520 | | rootEntity.meshesInfo.renderers == null || |
| | 521 | | rootEntity.meshesInfo.renderers.Length == 0) |
| 23 | 522 | | return; |
| | 523 | |
|
| 24 | 524 | | if (isNFT) |
| 2 | 525 | | return; |
| | 526 | |
|
| 22 | 527 | | int totalMaterials = 0; |
| 88 | 528 | | foreach (Renderer renderer in rootEntity.meshesInfo.renderers) |
| | 529 | | { |
| 22 | 530 | | if (renderer == null) |
| | 531 | | continue; |
| 22 | 532 | | totalMaterials += renderer.sharedMaterials.Length; |
| | 533 | | } |
| | 534 | |
|
| 22 | 535 | | if (!isNFT || (isNFT && originalMaterials == null)) |
| 22 | 536 | | originalMaterials = new Material[totalMaterials]; |
| | 537 | |
|
| 22 | 538 | | int matCont = 0; |
| 88 | 539 | | foreach (Renderer renderer in rootEntity.meshesInfo.renderers) |
| | 540 | | { |
| 22 | 541 | | if (renderer == null) |
| | 542 | | continue; |
| | 543 | |
|
| 88 | 544 | | for (int i = 0; i < renderer.sharedMaterials.Length; i++) |
| | 545 | | { |
| 22 | 546 | | if (isNFT && matCont == 0) |
| | 547 | | { |
| 0 | 548 | | matCont++; |
| 0 | 549 | | continue; |
| | 550 | | } |
| | 551 | |
|
| 22 | 552 | | originalMaterials[matCont] = renderer.sharedMaterials[i]; |
| 22 | 553 | | matCont++; |
| | 554 | | } |
| | 555 | | } |
| 22 | 556 | | } |
| | 557 | |
|
| | 558 | | void SetEditMaterial() |
| | 559 | | { |
| 39 | 560 | | if (rootEntity.meshesInfo == null || |
| | 561 | | rootEntity.meshesInfo.renderers == null || |
| | 562 | | rootEntity.meshesInfo.renderers.Length < 1) |
| 33 | 563 | | return; |
| | 564 | |
|
| 6 | 565 | | if (isNFT) |
| 0 | 566 | | return; |
| | 567 | |
|
| 6 | 568 | | int matCont = 0; |
| 24 | 569 | | foreach (Renderer renderer in rootEntity.meshesInfo.renderers) |
| | 570 | | { |
| 6 | 571 | | if (renderer == null) |
| | 572 | | continue; |
| | 573 | |
|
| 6 | 574 | | Material[] materials = new Material[renderer.sharedMaterials.Length]; |
| | 575 | |
|
| 24 | 576 | | for (int i = 0; i < renderer.sharedMaterials.Length; i++) |
| | 577 | | { |
| 6 | 578 | | if (isNFT && matCont == 0) |
| | 579 | | { |
| 0 | 580 | | materials[i] = renderer.sharedMaterials[i]; |
| 0 | 581 | | matCont++; |
| 0 | 582 | | continue; |
| | 583 | | } |
| | 584 | |
|
| 6 | 585 | | materials[i] = editMaterial; |
| 6 | 586 | | matCont++; |
| | 587 | | } |
| | 588 | |
|
| 6 | 589 | | renderer.sharedMaterials = materials; |
| | 590 | | } |
| 6 | 591 | | } |
| | 592 | |
|
| 16 | 593 | | void OnNameUpdate(object model) { OnStatusUpdate?.Invoke(this); } |
| | 594 | |
|
| | 595 | | void OnShapeUpdate(IDCLEntity entity) |
| | 596 | | { |
| 40 | 597 | | ShapeInit(); |
| | 598 | |
|
| 40 | 599 | | if (isSelected) |
| 13 | 600 | | SetEditMaterial(); |
| 40 | 601 | | } |
| | 602 | |
|
| | 603 | | void CreateCollidersForEntity(IDCLEntity entity) |
| | 604 | | { |
| 47 | 605 | | MeshesInfo meshInfo = entity.meshesInfo; |
| 47 | 606 | | if (meshInfo == null || |
| | 607 | | meshInfo.currentShape == null || |
| | 608 | | !meshInfo.currentShape.IsVisible()) |
| 22 | 609 | | return; |
| | 610 | |
|
| 25 | 611 | | if (collidersGameObjectDictionary.ContainsKey(entity.scene.sceneData.id + entity.entityId) && !isNFT) |
| 1 | 612 | | return; |
| | 613 | |
|
| 24 | 614 | | if (entity.children.Count > 0) |
| | 615 | | { |
| 0 | 616 | | using (var iterator = entity.children.GetEnumerator()) |
| | 617 | | { |
| 0 | 618 | | while (iterator.MoveNext()) |
| | 619 | | { |
| 0 | 620 | | CreateCollidersForEntity(iterator.Current.Value); |
| | 621 | | } |
| 0 | 622 | | } |
| | 623 | | } |
| | 624 | |
|
| | 625 | | //Note: When we are duplicating the GLTF and NFT component, their colliders are duplicated too |
| | 626 | | //So we eliminate any previous collider to ensure that only 1 collider remain active |
| 24 | 627 | | Transform[] children = rootEntity.gameObject.transform.GetComponentsInChildren<Transform>(); |
| 390 | 628 | | foreach (Transform child in children) |
| | 629 | | { |
| 171 | 630 | | if (child.gameObject.layer == BIWSettings.COLLIDER_SELECTION_LAYER) |
| | 631 | | { |
| 0 | 632 | | GameObject.Destroy(child.gameObject); |
| | 633 | | } |
| | 634 | | } |
| | 635 | |
|
| 24 | 636 | | List<GameObject> colliderList = new List<GameObject>(); |
| | 637 | |
|
| 106 | 638 | | for (int i = 0; i < meshInfo.renderers.Length; i++) |
| | 639 | | { |
| 29 | 640 | | if (meshInfo.renderers[i] == null) |
| | 641 | | continue; |
| 29 | 642 | | GameObject entityColliderChildren = new GameObject(entity.entityId); |
| 29 | 643 | | entityColliderChildren.layer = BIWSettings.COLLIDER_SELECTION_LAYER_INDEX; |
| | 644 | |
|
| 29 | 645 | | Transform t = entityColliderChildren.transform; |
| 29 | 646 | | t.SetParent(meshInfo.renderers[i].transform); |
| 29 | 647 | | t.ResetLocalTRS(); |
| | 648 | |
|
| 29 | 649 | | var meshCollider = entityColliderChildren.AddComponent<MeshCollider>(); |
| | 650 | |
|
| 29 | 651 | | if (meshInfo.renderers[i] is SkinnedMeshRenderer skr) |
| | 652 | | { |
| 0 | 653 | | Mesh meshColliderForSkinnedMesh = new Mesh(); |
| 0 | 654 | | skr.BakeMesh(meshColliderForSkinnedMesh); |
| 0 | 655 | | meshCollider.sharedMesh = meshColliderForSkinnedMesh; |
| 0 | 656 | | t.localScale = new Vector3(1 / meshInfo.renderers[i].gameObject.transform.lossyScale.x, 1 / meshInfo.ren |
| 0 | 657 | | } |
| | 658 | | else |
| | 659 | | { |
| 29 | 660 | | meshCollider.sharedMesh = meshInfo.renderers[i].GetComponent<MeshFilter>().sharedMesh; |
| | 661 | | } |
| | 662 | |
|
| 29 | 663 | | meshCollider.enabled = true; |
| 29 | 664 | | colliderList.Add(entityColliderChildren); |
| | 665 | |
|
| 29 | 666 | | if (isNFT) |
| | 667 | | { |
| 8 | 668 | | if (collidersGameObjectDictionary.ContainsKey(entity.scene.sceneData.id + entity.entityId)) |
| 6 | 669 | | collidersGameObjectDictionary.Remove(entity.scene.sceneData.id + entity.entityId); |
| | 670 | |
|
| 8 | 671 | | collidersGameObjectDictionary.Add(entity.scene.sceneData.id + entity.entityId, colliderList); |
| | 672 | |
|
| 8 | 673 | | colliderList = new List<GameObject>(); |
| | 674 | | } |
| | 675 | | } |
| | 676 | |
|
| 24 | 677 | | if (!isNFT) |
| 22 | 678 | | collidersGameObjectDictionary.Add(entity.scene.sceneData.id + entity.entityId, colliderList); |
| 24 | 679 | | } |
| | 680 | |
|
| | 681 | | public bool IsEntityNFT() |
| | 682 | | { |
| 235 | 683 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in rootEntity.sharedComponents) |
| | 684 | | { |
| 71 | 685 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE) |
| 3 | 686 | | return true; |
| | 687 | | } |
| | 688 | |
|
| 45 | 689 | | return false; |
| 3 | 690 | | } |
| | 691 | |
|
| 47 | 692 | | bool IsEntityAFloor() { return GetCatalogItemAssociated()?.category == BIWSettings.FLOOR_CATEGORY; } |
| | 693 | |
|
| | 694 | | bool IsEntityAVoxel() |
| | 695 | | { |
| 47 | 696 | | if (rootEntity.meshesInfo?.currentShape == null) |
| 22 | 697 | | return false; |
| 25 | 698 | | if (rootEntity.meshesInfo.renderers?.Length <= 0) |
| 1 | 699 | | return false; |
| 24 | 700 | | if (rootEntity.meshesInfo.mergedBounds.size != Vector3.one) |
| 17 | 701 | | return false; |
| 7 | 702 | | return true; |
| | 703 | | } |
| | 704 | | } |