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