| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using DCL.Models; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSComponents |
| | 8 | | { |
| | 9 | | public class BillboardComponentHandler : IECSComponentHandler<PBBillboard> |
| | 10 | | { |
| | 11 | | private readonly IUpdateEventHandler updateEventHandler; |
| | 12 | | private readonly DataStore_Player playerDataStore; |
| | 13 | |
|
| | 14 | | private Transform entityTransform; |
| 1 | 15 | | private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| | 16 | | private UnityEngine.Vector3 lastPosition; |
| | 17 | |
|
| | 18 | | private PBBillboard model; |
| | 19 | |
|
| 2 | 20 | | public BillboardComponentHandler(IUpdateEventHandler updateEventHandler) |
| | 21 | | { |
| 2 | 22 | | this.updateEventHandler = updateEventHandler; |
| 2 | 23 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| 2 | 24 | | } |
| | 25 | |
|
| | 26 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 27 | | { |
| | 28 | | // The billboard will rotate the entity transform toward the camera |
| 3 | 29 | | entityTransform = entity.gameObject.transform; |
| 3 | 30 | | } |
| | 31 | |
|
| | 32 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 33 | | { |
| 2 | 34 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| 2 | 35 | | } |
| | 36 | |
|
| | 37 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBBillboard model) |
| | 38 | | { |
| 1 | 39 | | this.model = model; |
| | 40 | |
|
| 1 | 41 | | ChangeOrientation(); |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co |
| | 45 | | private void LateUpdate() |
| | 46 | | { |
| | 47 | | //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757) |
| | 48 | | // We must find a more performant way to handle this, until that time, this is the approach. |
| | 49 | |
|
| 0 | 50 | | if (entityTransform == null) |
| 0 | 51 | | return; |
| | 52 | |
|
| 0 | 53 | | if (cameraPosition == lastPosition) |
| 0 | 54 | | return; |
| | 55 | |
|
| 0 | 56 | | lastPosition = cameraPosition; |
| | 57 | |
|
| 0 | 58 | | ChangeOrientation(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// We use the model axis as a lock values. This means, that if the model.X comes true, we lock the X axis to lo |
| | 63 | | /// </summary> |
| | 64 | | /// <returns></returns> |
| | 65 | | private UnityEngine.Vector3 GetLookAtVector() |
| | 66 | | { |
| 1 | 67 | | UnityEngine.Vector3 lookAtDir = model.OppositeDirection ? (cameraPosition - entityTransform.position) : (ent |
| | 68 | |
|
| 1 | 69 | | if (model.BillboardMode == BillboardMode.BmYAxe) |
| | 70 | | { |
| 1 | 71 | | lookAtDir.Normalize(); |
| 1 | 72 | | lookAtDir.y = entityTransform.forward.y; |
| | 73 | | } |
| | 74 | |
|
| 1 | 75 | | return lookAtDir.normalized; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private void ChangeOrientation() |
| | 79 | | { |
| 1 | 80 | | if (entityTransform == null || model == null) |
| 0 | 81 | | return; |
| 1 | 82 | | UnityEngine.Vector3 lookAtVector = GetLookAtVector(); |
| 1 | 83 | | if (lookAtVector != UnityEngine.Vector3.zero) |
| 1 | 84 | | entityTransform.forward = lookAtVector; |
| 1 | 85 | | } |
| | 86 | | } |
| | 87 | | } |