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