| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Components; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class Billboard : BaseComponent, IBillboard |
| | 11 | | { |
| | 12 | | [Serializable] |
| | 13 | | public class Model : BaseModel |
| | 14 | | { |
| 17 | 15 | | public bool x = true; |
| 17 | 16 | | public bool y = true; |
| 17 | 17 | | public bool z = true; |
| | 18 | |
|
| 7 | 19 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 20 | | } |
| | 21 | |
|
| | 22 | |
|
| | 23 | | private const string COMPONENT_NAME = "billboard"; |
| | 24 | |
|
| 81 | 25 | | public Transform Tr { get; set; } |
| 94 | 26 | | public Transform EntityTransform { get; set; } |
| 35 | 27 | | public Vector3 LastPosition { get; set; } |
| | 28 | |
|
| | 29 | |
|
| 7 | 30 | | public new Model GetModel() => (Model)model; |
| | 31 | |
|
| 5 | 32 | | public override string componentName => COMPONENT_NAME; |
| | 33 | |
|
| | 34 | |
|
| | 35 | | public override int GetClassId() |
| | 36 | | { |
| 1 | 37 | | return (int)CLASS_ID_COMPONENT.BILLBOARD; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 41 | | { |
| 9 | 42 | | if (EntityTransform == null) |
| | 43 | | { |
| 8 | 44 | | yield return new WaitUntil(() => entity.gameObject != null); |
| 4 | 45 | | EntityTransform = entity.gameObject.transform; |
| | 46 | | } |
| 9 | 47 | | } |
| | 48 | |
|
| | 49 | |
|
| | 50 | | public Vector3 GetLookAtVector(Vector3 cameraPosition) |
| | 51 | | { |
| 15 | 52 | | bool hasTextShape = scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.TEXT_SHAPE); |
| 15 | 53 | | Vector3 lookAtDir = hasTextShape ? (EntityTransform.position - cameraPosition) : (cameraPosition - EntityTra |
| | 54 | |
|
| 15 | 55 | | Model model = (Model) this.model; |
| | 56 | | // Note (Zak): This check is here to avoid normalizing twice if not needed |
| 15 | 57 | | if (!(model.x && model.y && model.z)) |
| | 58 | | { |
| 6 | 59 | | lookAtDir.Normalize(); |
| | 60 | |
|
| | 61 | | // Note (Zak): Model x,y,z are axis that we want to enable/disable |
| | 62 | | // while lookAtDir x,y,z are the components of the look-at vector |
| 6 | 63 | | if (!model.x || model.z) |
| 6 | 64 | | lookAtDir.y = EntityTransform.forward.y; |
| 6 | 65 | | if (!model.y) |
| 3 | 66 | | lookAtDir.x = EntityTransform.forward.x; |
| | 67 | | } |
| | 68 | |
|
| 15 | 69 | | return lookAtDir.normalized; |
| | 70 | | } |
| | 71 | |
|
| | 72 | |
|
| | 73 | | private void Awake() |
| | 74 | | { |
| 4 | 75 | | model = new Model(); |
| 4 | 76 | | Tr = transform; |
| 4 | 77 | | LastPosition = Vector3.up * float.MaxValue; |
| | 78 | |
|
| 4 | 79 | | IBillboardsController controller = Environment.i.serviceLocator.Get<IBillboardsController>(); |
| 4 | 80 | | controller.BillboardAdded(this); |
| 4 | 81 | | } |
| | 82 | | } |
| | 83 | | } |