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