| | 1 | | using DCL; |
| | 2 | | using DCL.ECSComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace ECSSystems.BillboardSystem |
| | 8 | | { |
| | 9 | | public class ECSBillboardSystem |
| | 10 | | { |
| | 11 | | private readonly ECSComponent<PBBillboard> billboardComponent; |
| | 12 | | private readonly DataStore_Camera camera; |
| | 13 | |
|
| 3 | 14 | | public ECSBillboardSystem(ECSComponent<PBBillboard> billboards, DataStore_Camera camera) |
| | 15 | | { |
| 3 | 16 | | this.billboardComponent = billboards; |
| 3 | 17 | | this.camera = camera; |
| 3 | 18 | | } |
| | 19 | |
|
| | 20 | | public void Update() |
| | 21 | | { |
| | 22 | | const uint BILLBOARD_NONE = (uint)BillboardMode.BmNone; |
| | 23 | | const uint BILLBOARD_X = (uint)BillboardMode.BmX; |
| | 24 | | const uint BILLBOARD_Y = (uint)BillboardMode.BmY; |
| | 25 | | const uint BILLBOARD_Z = (uint)BillboardMode.BmZ; |
| | 26 | | const uint BILLBOARD_XY = BILLBOARD_X | BILLBOARD_Y; |
| | 27 | |
|
| 1 | 28 | | var billboards = billboardComponent.Get(); |
| 1 | 29 | | int billboardsCount = billboards.Count; |
| | 30 | |
|
| 1 | 31 | | if (billboardsCount == 0) |
| 0 | 32 | | return; |
| | 33 | |
|
| 1 | 34 | | Transform cameraT = camera.transform.Get(); |
| 1 | 35 | | Vector3 cameraPos = cameraT.position; |
| 1 | 36 | | Quaternion cameraRotationAxisZ = Quaternion.Euler(0, 0, cameraT.rotation.eulerAngles.z); |
| | 37 | |
|
| 4 | 38 | | for (var i = 0; i < billboardsCount; i++) |
| | 39 | | { |
| 1 | 40 | | uint billboardMode = (uint)billboards[i].value.model.GetBillboardMode(); |
| | 41 | |
|
| 1 | 42 | | if (billboardMode == BILLBOARD_NONE) |
| | 43 | | { |
| | 44 | | continue; |
| | 45 | | } |
| | 46 | |
|
| 1 | 47 | | IDCLEntity entity = billboards[i].value.entity; |
| 1 | 48 | | Transform billboardT = entity.gameObject.transform; |
| | 49 | |
|
| 1 | 50 | | Vector3 billboardForward = billboardT.forward; |
| 1 | 51 | | Vector3 billboardPos = billboardT.position; |
| | 52 | |
|
| 1 | 53 | | Vector3 forward = billboardForward; |
| | 54 | |
|
| | 55 | | // either or both X and Y are set |
| 1 | 56 | | if ((billboardMode & BILLBOARD_XY) != 0) |
| | 57 | | { |
| 1 | 58 | | forward = billboardPos - cameraPos; |
| | 59 | |
|
| 1 | 60 | | if ((billboardMode & BILLBOARD_Y) == 0) forward.x = 0; |
| 2 | 61 | | if ((billboardMode & BILLBOARD_X) == 0) forward.y = 0; |
| | 62 | |
|
| 1 | 63 | | forward.Normalize(); |
| | 64 | | } |
| | 65 | |
|
| 1 | 66 | | Quaternion rotation = forward != Vector3.zero ? Quaternion.LookRotation(forward) : Quaternion.identity; |
| | 67 | |
|
| | 68 | | // apply Z axis rotation |
| 1 | 69 | | if ((billboardMode & BILLBOARD_Z) != 0) |
| | 70 | | { |
| 0 | 71 | | rotation *= cameraRotationAxisZ; |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | billboardT.rotation = rotation; |
| | 75 | | } |
| 1 | 76 | | } |
| | 77 | | } |
| | 78 | | } |