| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using DCL; |
| | 7 | | using UnityEngine; |
| | 8 | | using Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | namespace Builder |
| | 11 | | { |
| | 12 | | public class DCLBuilderEntity : EditableEntity |
| | 13 | | { |
| | 14 | | public static Action<DCLBuilderEntity> OnEntityShapeUpdated; |
| | 15 | | public static Action<DCLBuilderEntity> OnEntityTransformUpdated; |
| | 16 | | public static Action<DCLBuilderEntity> OnEntityAddedWithTransform; |
| | 17 | |
|
| | 18 | | public bool hasGizmoComponent |
| | 19 | | { |
| | 20 | | get |
| | 21 | | { |
| 2 | 22 | | if (rootEntity != null) |
| | 23 | | { |
| 2 | 24 | | return rootEntity.components.ContainsKey(CLASS_ID_COMPONENT.GIZMOS); |
| | 25 | | } |
| | 26 | | else |
| | 27 | | { |
| 0 | 28 | | return false; |
| | 29 | | } |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public bool hasSmartItemComponent |
| | 34 | | { |
| | 35 | | get |
| | 36 | | { |
| 2 | 37 | | if (rootEntity != null) |
| | 38 | | { |
| 2 | 39 | | return rootEntity.components.ContainsKey(CLASS_ID_COMPONENT.SMART_ITEM); |
| | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| 0 | 43 | | return false; |
| | 44 | | } |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | private DCLBuilderSelectionCollider[] meshColliders; |
| | 49 | | private Animation[] meshAnimations; |
| | 50 | | private Action OnShapeLoaded; |
| | 51 | |
|
| | 52 | | private bool isTransformComponentSet; |
| | 53 | | private bool isShapeComponentSet; |
| | 54 | |
|
| | 55 | | private Vector3 scaleTarget; |
| | 56 | | private bool isScalingAnimation = false; |
| | 57 | |
|
| | 58 | | public void SetEntity(IDCLEntity entity) |
| | 59 | | { |
| 1 | 60 | | rootEntity = entity; |
| | 61 | |
|
| 1 | 62 | | entity.OnShapeUpdated -= OnShapeUpdated; |
| 1 | 63 | | entity.OnShapeUpdated += OnShapeUpdated; |
| | 64 | |
|
| 1 | 65 | | entity.OnTransformChange -= OnTransformUpdated; |
| 1 | 66 | | entity.OnTransformChange += OnTransformUpdated; |
| | 67 | |
|
| 1 | 68 | | entity.OnRemoved -= OnEntityRemoved; |
| 1 | 69 | | entity.OnRemoved += OnEntityRemoved; |
| | 70 | |
|
| 1 | 71 | | AvatarShape.OnAvatarShapeUpdated -= OnAvatarShapeUpdated; |
| 1 | 72 | | AvatarShape.OnAvatarShapeUpdated += OnAvatarShapeUpdated; |
| | 73 | |
|
| 1 | 74 | | DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged; |
| 1 | 75 | | DCLBuilderBridge.OnPreviewModeChanged += OnPreviewModeChanged; |
| | 76 | |
|
| | 77 | | //builder evaluate boundaries by itself |
| 1 | 78 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| 1 | 79 | | entity.OnShapeUpdated -= Environment.i.world.sceneBoundsChecker.EvaluateEntityPosition; |
| | 80 | |
|
| 1 | 81 | | gameObject.transform.localScale = Vector3.zero; |
| | 82 | |
|
| 1 | 83 | | isTransformComponentSet = false; |
| 1 | 84 | | isShapeComponentSet = false; |
| | 85 | |
|
| 1 | 86 | | scaleTarget = Vector3.one; |
| | 87 | |
|
| 1 | 88 | | DestroyColliders(); |
| | 89 | |
|
| 1 | 90 | | if (HasShape()) |
| | 91 | | { |
| 0 | 92 | | OnShapeUpdated(entity); |
| | 93 | | } |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public bool IsInsideSceneBoundaries() |
| | 97 | | { |
| 1 | 98 | | if (rootEntity != null && rootEntity.meshesInfo.renderers != null) |
| | 99 | | { |
| 1 | 100 | | return rootEntity.scene.IsInsideSceneBoundaries(MeshesInfoUtils.BuildMergedBounds(rootEntity.meshesInfo. |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | return true; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public override void SetSelectLayer() |
| | 107 | | { |
| 0 | 108 | | if (rootEntity.meshesInfo == null || rootEntity.meshesInfo.renderers == null) |
| | 109 | | { |
| 0 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | int selectionLayer = LayerMask.NameToLayer(DCLBuilderRaycast.LAYER_SELECTION); |
| | 114 | | Renderer renderer; |
| 0 | 115 | | for (int i = 0; i < rootEntity.meshesInfo.renderers.Length; i++) |
| | 116 | | { |
| 0 | 117 | | renderer = rootEntity.meshesInfo.renderers[i]; |
| 0 | 118 | | if (renderer) |
| 0 | 119 | | renderer.gameObject.layer = selectionLayer; |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public override void SetDefaultLayer() |
| | 124 | | { |
| 0 | 125 | | if (rootEntity.meshesInfo == null || rootEntity.meshesInfo.renderers == null) |
| | 126 | | { |
| 0 | 127 | | return; |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | int selectionLayer = 0; |
| | 131 | | Renderer renderer; |
| 0 | 132 | | for (int i = 0; i < rootEntity.meshesInfo.renderers.Length; i++) |
| | 133 | | { |
| 0 | 134 | | renderer = rootEntity.meshesInfo.renderers[i]; |
| 0 | 135 | | if (renderer) |
| 0 | 136 | | renderer.gameObject.layer = selectionLayer; |
| | 137 | | } |
| 0 | 138 | | } |
| | 139 | |
|
| 0 | 140 | | public bool HasShape() { return isShapeComponentSet; } |
| | 141 | |
|
| 0 | 142 | | public bool HasRenderer() { return rootEntity.meshesInfo != null && rootEntity.meshesInfo.renderers != null; } |
| | 143 | |
|
| | 144 | | public void SetOnShapeLoaded(Action onShapeLoad) |
| | 145 | | { |
| 0 | 146 | | if (HasShape()) |
| | 147 | | { |
| 0 | 148 | | if (onShapeLoad != null) |
| 0 | 149 | | onShapeLoad(); |
| 0 | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 0 | 153 | | OnShapeLoaded = onShapeLoad; |
| | 154 | | } |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | private void OnEntityRemoved(IDCLEntity entity) |
| | 158 | | { |
| 1 | 159 | | rootEntity.OnRemoved -= OnEntityRemoved; |
| 1 | 160 | | rootEntity.OnShapeUpdated -= OnShapeUpdated; |
| 1 | 161 | | rootEntity.OnTransformChange -= OnTransformUpdated; |
| 1 | 162 | | DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged; |
| 1 | 163 | | AvatarShape.OnAvatarShapeUpdated -= OnAvatarShapeUpdated; |
| 1 | 164 | | DestroyColliders(); |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | private void OnShapeUpdated(IDCLEntity entity) |
| | 168 | | { |
| 2 | 169 | | isShapeComponentSet = true; |
| 2 | 170 | | OnEntityShapeUpdated?.Invoke(this); |
| | 171 | |
|
| | 172 | | // We don't want animation to be running on editor |
| 2 | 173 | | meshAnimations = GetComponentsInChildren<Animation>(); |
| 2 | 174 | | if (hasSmartItemComponent) |
| | 175 | | { |
| 0 | 176 | | DefaultAnimationStop(); |
| 0 | 177 | | } |
| | 178 | | else |
| | 179 | | { |
| 2 | 180 | | DefaultAnimationSample(0); |
| | 181 | | } |
| | 182 | |
|
| 2 | 183 | | if (hasGizmoComponent) |
| | 184 | | { |
| 0 | 185 | | gameObject.transform.localScale = Vector3.zero; |
| 0 | 186 | | StartCoroutine(ScaleAnimationRoutine(0.3f)); |
| 0 | 187 | | } |
| 2 | 188 | | else if (isTransformComponentSet) |
| | 189 | | { |
| 2 | 190 | | gameObject.transform.localScale = scaleTarget; |
| 2 | 191 | | ProcessEntityShape(entity); |
| | 192 | | } |
| | 193 | |
|
| 2 | 194 | | if (OnShapeLoaded != null) |
| | 195 | | { |
| 0 | 196 | | OnShapeLoaded(); |
| 0 | 197 | | OnShapeLoaded = null; |
| | 198 | | } |
| 2 | 199 | | } |
| | 200 | |
|
| | 201 | | private void OnTransformUpdated(object model) |
| | 202 | | { |
| 1 | 203 | | DCLTransform.Model transformModel = (DCLTransform.Model)model; |
| | 204 | | //NOTE: there is no parenting entities in editor mode so we can set properties in world space |
| 1 | 205 | | gameObject.transform.position = transformModel.position; |
| 1 | 206 | | gameObject.transform.rotation = transformModel.rotation; |
| | 207 | |
|
| 1 | 208 | | if (isScalingAnimation || !isShapeComponentSet) |
| | 209 | | { |
| 1 | 210 | | scaleTarget = transformModel.scale; |
| 1 | 211 | | } |
| 0 | 212 | | else if (isShapeComponentSet) |
| | 213 | | { |
| 0 | 214 | | scaleTarget = transformModel.scale; |
| 0 | 215 | | gameObject.transform.localScale = transformModel.scale; |
| | 216 | | } |
| | 217 | |
|
| 1 | 218 | | if (!isTransformComponentSet) |
| | 219 | | { |
| 1 | 220 | | isTransformComponentSet = true; |
| 1 | 221 | | OnEntityAddedWithTransform?.Invoke(this); |
| | 222 | | } |
| | 223 | |
|
| 1 | 224 | | if (isShapeComponentSet) |
| | 225 | | { |
| 0 | 226 | | OnEntityTransformUpdated?.Invoke(this); |
| | 227 | | } |
| 1 | 228 | | } |
| | 229 | |
|
| | 230 | | private void OnAvatarShapeUpdated(IDCLEntity entity, AvatarShape avatarShape) |
| | 231 | | { |
| 0 | 232 | | if (rootEntity != entity) |
| | 233 | | { |
| 0 | 234 | | return; |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | OnShapeUpdated(rootEntity); |
| 0 | 238 | | } |
| | 239 | |
|
| | 240 | | private void OnPreviewModeChanged(bool isPreview) |
| | 241 | | { |
| 0 | 242 | | if (!hasSmartItemComponent) |
| | 243 | | { |
| 0 | 244 | | if (isPreview) |
| | 245 | | { |
| 0 | 246 | | DefaultAnimationPlay(); |
| 0 | 247 | | } |
| | 248 | | else |
| | 249 | | { |
| 0 | 250 | | DefaultAnimationSample(0); |
| | 251 | | } |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | SetCollidersActive(!isPreview); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | private void ProcessEntityShape(IDCLEntity entity) |
| | 258 | | { |
| 2 | 259 | | if (entity.meshRootGameObject && entity.meshesInfo.renderers.Length > 0) |
| | 260 | | { |
| 1 | 261 | | CreateColliders(entity.meshesInfo); |
| 1 | 262 | | SetCollidersActive(true); |
| | 263 | | } |
| 2 | 264 | | } |
| | 265 | |
|
| | 266 | | private void CreateColliders(MeshesInfo meshInfo) |
| | 267 | | { |
| 1 | 268 | | meshColliders = new DCLBuilderSelectionCollider[meshInfo.renderers.Length]; |
| 8 | 269 | | for (int i = 0; i < meshInfo.renderers.Length; i++) |
| | 270 | | { |
| 3 | 271 | | if (meshInfo.renderers[i] == null) |
| | 272 | | continue; |
| 3 | 273 | | meshColliders[i] = new GameObject("BuilderSelectionCollider").AddComponent<DCLBuilderSelectionCollider>( |
| 3 | 274 | | meshColliders[i].Initialize(this, meshInfo.renderers[i]); |
| 3 | 275 | | meshColliders[i].gameObject.SetActive(false); |
| | 276 | | } |
| 1 | 277 | | } |
| | 278 | |
|
| | 279 | | private IEnumerator ScaleAnimationRoutine(float seconds) |
| | 280 | | { |
| 0 | 281 | | float startingTime = Time.time; |
| 0 | 282 | | float normalizedTime = 0; |
| 0 | 283 | | Vector3 scale = Vector3.zero; |
| | 284 | |
|
| 0 | 285 | | gameObject.transform.localScale = scale; |
| 0 | 286 | | isScalingAnimation = true; |
| | 287 | |
|
| 0 | 288 | | while (Time.time - startingTime <= seconds) |
| | 289 | | { |
| 0 | 290 | | normalizedTime = (Time.time - startingTime) / seconds; |
| 0 | 291 | | scale = Vector3.Lerp(scale, scaleTarget, normalizedTime); |
| 0 | 292 | | gameObject.transform.localScale = scale; |
| 0 | 293 | | yield return null; |
| | 294 | | } |
| | 295 | |
|
| 0 | 296 | | gameObject.transform.localScale = scaleTarget; |
| 0 | 297 | | isScalingAnimation = false; |
| 0 | 298 | | ProcessEntityShape(rootEntity); |
| 0 | 299 | | OnEntityTransformUpdated?.Invoke(this); |
| 0 | 300 | | } |
| | 301 | |
|
| | 302 | | private void DestroyColliders() |
| | 303 | | { |
| 2 | 304 | | if (meshColliders != null) |
| | 305 | | { |
| 8 | 306 | | for (int i = 0; i < meshColliders.Length; i++) |
| | 307 | | { |
| 3 | 308 | | if (meshColliders[i] != null) |
| | 309 | | { |
| 3 | 310 | | Destroy(meshColliders[i].gameObject); |
| | 311 | | } |
| | 312 | | } |
| | 313 | |
|
| 1 | 314 | | meshColliders = null; |
| | 315 | | } |
| 2 | 316 | | } |
| | 317 | |
|
| | 318 | | private void SetCollidersActive(bool active) |
| | 319 | | { |
| 1 | 320 | | if (meshColliders != null) |
| | 321 | | { |
| 8 | 322 | | for (int i = 0; i < meshColliders.Length; i++) |
| | 323 | | { |
| 3 | 324 | | if (meshColliders[i] != null) |
| | 325 | | { |
| 3 | 326 | | meshColliders[i].gameObject.SetActive(active); |
| | 327 | | } |
| | 328 | | } |
| | 329 | | } |
| 1 | 330 | | } |
| | 331 | |
|
| | 332 | | private void DefaultAnimationStop() |
| | 333 | | { |
| 0 | 334 | | if (meshAnimations != null) |
| | 335 | | { |
| 0 | 336 | | for (int i = 0; i < meshAnimations.Length; i++) |
| | 337 | | { |
| 0 | 338 | | meshAnimations[i].Stop(); |
| | 339 | | } |
| | 340 | | } |
| 0 | 341 | | } |
| | 342 | |
|
| | 343 | | private void DefaultAnimationSample(float time) |
| | 344 | | { |
| 2 | 345 | | if (meshAnimations != null) |
| | 346 | | { |
| 4 | 347 | | for (int i = 0; i < meshAnimations.Length; i++) |
| | 348 | | { |
| 0 | 349 | | meshAnimations[i].Stop(); |
| 0 | 350 | | meshAnimations[i].clip?.SampleAnimation(meshAnimations[i].gameObject, time); |
| | 351 | | } |
| | 352 | | } |
| 2 | 353 | | } |
| | 354 | |
|
| | 355 | | private void DefaultAnimationPlay() |
| | 356 | | { |
| 0 | 357 | | if (meshAnimations != null) |
| | 358 | | { |
| 0 | 359 | | for (int i = 0; i < meshAnimations.Length; i++) |
| | 360 | | { |
| 0 | 361 | | meshAnimations[i].Play(); |
| | 362 | | } |
| | 363 | | } |
| 0 | 364 | | } |
| | 365 | | } |
| | 366 | | } |