| | 1 | | using DCL.Components; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | | using DCL.Models; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class Billboard : BaseComponent |
| | 10 | | { |
| | 11 | | [System.Serializable] |
| | 12 | | public class Model : BaseModel |
| | 13 | | { |
| 15 | 14 | | public bool x = true; |
| 15 | 15 | | public bool y = true; |
| 15 | 16 | | public bool z = true; |
| | 17 | |
|
| 6 | 18 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 19 | | } |
| | 20 | |
|
| | 21 | | Transform entityTransform; |
| 29 | 22 | | Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| | 23 | | Vector3 lastPosition; |
| | 24 | |
|
| 6 | 25 | | private void Awake() { model = new Model(); } |
| | 26 | |
|
| | 27 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 28 | | { |
| 6 | 29 | | cameraPosition.OnChange -= CameraPositionChanged; |
| 6 | 30 | | cameraPosition.OnChange += CameraPositionChanged; |
| | 31 | |
|
| 6 | 32 | | Model model = (Model)newModel; |
| | 33 | |
|
| 6 | 34 | | ChangeOrientation(); |
| | 35 | |
|
| | 36 | |
|
| 6 | 37 | | if (entityTransform == null) |
| | 38 | | { |
| 6 | 39 | | yield return new WaitUntil(() => entity.gameObject != null); |
| 3 | 40 | | entityTransform = entity.gameObject.transform; |
| | 41 | | } |
| 6 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | new public Model GetModel() { return (Model)model; } |
| | 45 | |
|
| 6 | 46 | | public void OnDestroy() { cameraPosition.OnChange -= CameraPositionChanged; } |
| | 47 | |
|
| | 48 | | // This runs on LateUpdate() instead of Update() to be applied AFTER the transform was moved by the transform co |
| | 49 | | public void LateUpdate() |
| | 50 | | { |
| | 51 | | //NOTE(Brian): This fixes #757 (https://github.com/decentraland/unity-client/issues/757) |
| | 52 | | // We must find a more performant way to handle this, until that time, this is the approach. |
| | 53 | |
|
| 19 | 54 | | if (entityTransform == null) |
| 3 | 55 | | return; |
| 16 | 56 | | if (transform.position == lastPosition) |
| 8 | 57 | | return; |
| | 58 | |
|
| 8 | 59 | | lastPosition = transform.position; |
| | 60 | |
|
| 8 | 61 | | ChangeOrientation(); |
| 8 | 62 | | } |
| | 63 | |
|
| | 64 | | Vector3 GetLookAtVector() |
| | 65 | | { |
| 14 | 66 | | bool hasTextShape = entity.components.ContainsKey(Models.CLASS_ID_COMPONENT.TEXT_SHAPE); |
| 14 | 67 | | Vector3 lookAtDir = hasTextShape ? (entityTransform.position - cameraPosition) : (cameraPosition - entityTra |
| | 68 | |
|
| 14 | 69 | | Model model = (Model) this.model; |
| | 70 | | // Note (Zak): This check is here to avoid normalizing twice if not needed |
| 14 | 71 | | if (!(model.x && model.y && model.z)) |
| | 72 | | { |
| 7 | 73 | | lookAtDir.Normalize(); |
| | 74 | |
|
| | 75 | | // Note (Zak): Model x,y,z are axis that we want to enable/disable |
| | 76 | | // while lookAtDir x,y,z are the components of the look-at vector |
| 7 | 77 | | if (!model.x || model.z) |
| 7 | 78 | | lookAtDir.y = entityTransform.forward.y; |
| 7 | 79 | | if (!model.y) |
| 2 | 80 | | lookAtDir.x = entityTransform.forward.x; |
| | 81 | | } |
| | 82 | |
|
| 14 | 83 | | return lookAtDir.normalized; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | void ChangeOrientation() |
| | 87 | | { |
| 18 | 88 | | if (entityTransform == null) |
| 4 | 89 | | return; |
| 14 | 90 | | Vector3 lookAtVector = GetLookAtVector(); |
| 14 | 91 | | if (lookAtVector != Vector3.zero) |
| 14 | 92 | | entityTransform.forward = lookAtVector; |
| 14 | 93 | | } |
| | 94 | |
|
| 8 | 95 | | private void CameraPositionChanged(Vector3 current, Vector3 previous) { ChangeOrientation(); } |
| | 96 | |
|
| 0 | 97 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.BILLBOARD; } |
| | 98 | | } |
| | 99 | | } |