| | 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 IDCLEntity entity; |
| | 19 | | private IParcelScene scene; |
| | 20 | | private PBBillboard model; |
| | 21 | |
|
| 2 | 22 | | public BillboardComponentHandler(DataStore_Player playerDataStore, IUpdateEventHandler updateEventHandler) |
| | 23 | | { |
| 2 | 24 | | this.playerDataStore = playerDataStore; |
| 2 | 25 | | this.updateEventHandler = updateEventHandler; |
| 2 | 26 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 30 | | { |
| 3 | 31 | | this.entity = entity; |
| 3 | 32 | | this.scene = scene; |
| | 33 | |
|
| | 34 | | // The billboard will rotate the entity transform toward the camera |
| 3 | 35 | | entityTransform = entity.gameObject.transform; |
| 3 | 36 | | } |
| | 37 | |
|
| | 38 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 39 | | { |
| 2 | 40 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| 2 | 41 | | } |
| | 42 | |
|
| | 43 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBBillboard model) |
| | 44 | | { |
| 1 | 45 | | this.model = model; |
| | 46 | |
|
| 1 | 47 | | ChangeOrientation(); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co |
| | 51 | | private void LateUpdate() |
| | 52 | | { |
| | 53 | | //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757) |
| | 54 | | // We must find a more performant way to handle this, until that time, this is the approach. |
| | 55 | |
|
| 0 | 56 | | if (entityTransform == null) |
| 0 | 57 | | return; |
| | 58 | |
|
| 0 | 59 | | UnityEngine.Vector3 playerPosition = playerDataStore.playerUnityPosition.Get(); |
| | 60 | |
|
| 0 | 61 | | if (playerPosition == lastPosition) |
| 0 | 62 | | return; |
| | 63 | |
|
| 0 | 64 | | lastPosition = playerPosition; |
| | 65 | |
|
| 0 | 66 | | ChangeOrientation(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// 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 |
| | 71 | | /// </summary> |
| | 72 | | /// <returns></returns> |
| | 73 | | private UnityEngine.Vector3 GetLookAtVector() |
| | 74 | | { |
| 1 | 75 | | UnityEngine.Vector3 lookAtDir =(cameraPosition - entityTransform.position); |
| | 76 | |
|
| | 77 | | // Note (Zak): This check is here to avoid normalizing twice if not needed |
| 1 | 78 | | if (!(model.GetX() && model.GetY() && model.GetZ())) |
| | 79 | | { |
| 1 | 80 | | lookAtDir.Normalize(); |
| | 81 | |
|
| | 82 | | // Note (Zak): Model x,y,z are axis that we want to enable/disable |
| | 83 | | // while lookAtDir x,y,z are the components of the look-at vector |
| 1 | 84 | | if (!model.GetX() || model.GetZ()) |
| 0 | 85 | | lookAtDir.y = entityTransform.forward.y; |
| 1 | 86 | | if (!model.GetY()) |
| 1 | 87 | | lookAtDir.x = entityTransform.forward.x; |
| | 88 | | } |
| | 89 | |
|
| 1 | 90 | | return lookAtDir.normalized; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private void ChangeOrientation() |
| | 94 | | { |
| 1 | 95 | | if (entityTransform == null || model == null) |
| 0 | 96 | | return; |
| 1 | 97 | | UnityEngine.Vector3 lookAtVector = GetLookAtVector(); |
| 1 | 98 | | if (lookAtVector != UnityEngine.Vector3.zero) |
| 1 | 99 | | entityTransform.forward = lookAtVector; |
| 1 | 100 | | } |
| | 101 | | } |
| | 102 | | } |