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