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