| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | |
|
| | 8 | | namespace Tests |
| | 9 | | { |
| | 10 | | public class BillboardTests : IntegrationTestSuite_Legacy |
| | 11 | | { |
| | 12 | | Billboard billboard; |
| 1 | 13 | | string entityId = "e1"; |
| | 14 | |
|
| | 15 | | [UnityTest] |
| | 16 | | public IEnumerator AddBillboardComponent() |
| | 17 | | { |
| 1 | 18 | | yield return CreateComponent(x: false, y: true, z: false); |
| | 19 | |
|
| 1 | 20 | | Assert.IsFalse(billboard.GetModel().x, "Wrong model data! x should be false."); |
| 1 | 21 | | Assert.IsTrue(billboard.GetModel().y, "Wrong model data! y should be true."); |
| 1 | 22 | | Assert.IsFalse(billboard.GetModel().z, "Wrong model data! z should be false"); |
| | 23 | |
|
| 1 | 24 | | yield return null; |
| | 25 | |
|
| 1 | 26 | | yield return null; |
| 1 | 27 | | } |
| | 28 | |
|
| | 29 | | [UnityTest] |
| | 30 | | public IEnumerator CheckLookAtPlayer() |
| | 31 | | { |
| 1 | 32 | | DCLCharacterController.i.PauseGravity(); |
| | 33 | |
|
| 1 | 34 | | yield return CreateComponent(x: true, y: true, z: true); |
| | 35 | |
|
| 1 | 36 | | yield return billboard.routine; |
| | 37 | |
|
| 1 | 38 | | Transform entityTransform = scene.entities[entityId].gameObject.transform; |
| 1 | 39 | | Vector3 lookAt = GetLookAtVector(billboard.GetModel(), entityTransform); |
| | 40 | |
|
| 1 | 41 | | yield return null; |
| 1 | 42 | | Assert.AreApproximatelyEqual(lookAt.x, entityTransform.forward.x, "billboard entity forward vector should be |
| 1 | 43 | | Assert.AreApproximatelyEqual(lookAt.y, entityTransform.forward.y, "billboard entity forward vector should be |
| 1 | 44 | | Assert.AreApproximatelyEqual(lookAt.z, entityTransform.forward.z, "billboard entity forward vector should be |
| | 45 | |
|
| 1 | 46 | | var billboardModel = new Billboard.Model() |
| | 47 | | { |
| | 48 | | x = true, |
| | 49 | | y = false, |
| | 50 | | z = true |
| | 51 | | }; |
| | 52 | |
|
| 1 | 53 | | yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel); |
| | 54 | |
|
| 1 | 55 | | lookAt = GetLookAtVector(billboardModel, entityTransform); |
| 1 | 56 | | Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the |
| | 57 | |
|
| 1 | 58 | | billboardModel = new Billboard.Model() |
| | 59 | | { |
| | 60 | | x = false, |
| | 61 | | y = false, |
| | 62 | | z = false |
| | 63 | | }; |
| | 64 | |
|
| 1 | 65 | | yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel); |
| | 66 | |
|
| 1 | 67 | | lookAt = GetLookAtVector(billboardModel, entityTransform); |
| 1 | 68 | | Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the |
| | 69 | |
|
| 1 | 70 | | yield return null; |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | [UnityTest] |
| | 74 | | public IEnumerator CheckLookAtPlayerWhileTransformMoves() |
| | 75 | | { |
| 1 | 76 | | DCLCharacterController.i.PauseGravity(); |
| | 77 | |
|
| 1 | 78 | | yield return CreateComponent(x: true, y: true, z: true); |
| | 79 | |
|
| 1 | 80 | | yield return billboard.routine; |
| | 81 | |
|
| 1 | 82 | | var entity = scene.entities[entityId]; |
| 1 | 83 | | Transform entityTransform = entity.gameObject.transform; |
| 1 | 84 | | Vector3 lookAt = GetLookAtVector(billboard.GetModel(), entityTransform); |
| | 85 | |
|
| 1 | 86 | | Assert.AreApproximatelyEqual(lookAt.x, entityTransform.forward.x, "billboard entity forward vector should be |
| 1 | 87 | | Assert.AreApproximatelyEqual(lookAt.y, entityTransform.forward.y, "billboard entity forward vector should be |
| 1 | 88 | | Assert.AreApproximatelyEqual(lookAt.z, entityTransform.forward.z, "billboard entity forward vector should be |
| | 89 | |
|
| 1 | 90 | | var billboardModel = new Billboard.Model(); |
| | 91 | |
|
| 1 | 92 | | yield return TestHelpers.EntityComponentUpdate<Billboard, Billboard.Model>(billboard, billboardModel); |
| | 93 | |
|
| 1 | 94 | | lookAt = GetLookAtVector(billboardModel, entityTransform); |
| 1 | 95 | | Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as the |
| | 96 | |
|
| | 97 | | // We simulate a "system" moving the billboard object |
| 1 | 98 | | Vector3 position = Vector3.one; |
| 12 | 99 | | for (int i = 0; i < 5; i++) |
| | 100 | | { |
| 5 | 101 | | position.y += i; |
| 5 | 102 | | entityTransform.position = position; |
| 5 | 103 | | Assert.IsTrue(entityTransform.forward == lookAt, "billboard entity forward vector should be the same as |
| | 104 | | } |
| | 105 | |
|
| 1 | 106 | | yield return null; |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | IEnumerator CreateComponent(bool x, bool y, bool z) |
| | 110 | | { |
| 3 | 111 | | var entity = TestHelpers.CreateSceneEntity(scene, entityId); |
| 3 | 112 | | TestHelpers.SetEntityTransform(scene, entity, Vector3.one, Quaternion.identity, Vector3.one); |
| 3 | 113 | | yield return null; |
| | 114 | |
|
| 3 | 115 | | var billboardModel = new Billboard.Model() |
| | 116 | | { |
| | 117 | | x = x, |
| | 118 | | y = y, |
| | 119 | | z = z |
| | 120 | | }; |
| | 121 | |
|
| 3 | 122 | | billboard = |
| | 123 | | TestHelpers.EntityComponentCreate<Billboard, Billboard.Model>(scene, scene.entities[entityId], |
| | 124 | | billboardModel); |
| | 125 | |
|
| 3 | 126 | | Assert.IsTrue(billboard != null, "Component creation fail!"); |
| | 127 | |
|
| 3 | 128 | | yield return billboard.routine; |
| 3 | 129 | | } |
| | 130 | |
|
| | 131 | | Vector3 GetLookAtVector(Billboard.Model model, Transform entityTransform) |
| | 132 | | { |
| 5 | 133 | | Vector3 lookAtDir = CommonScriptableObjects.cameraPosition - entityTransform.position; |
| | 134 | |
|
| | 135 | | // Note (Zak): This check is here to avoid normalizing twice if not needed |
| 5 | 136 | | if (!(model.x && model.y && model.z)) |
| | 137 | | { |
| 2 | 138 | | lookAtDir.Normalize(); |
| | 139 | |
|
| | 140 | | // Note (Zak): Model x,y,z are axis that we want to enable/disable |
| | 141 | | // while lookAtDir x,y,z are the components of the look-at vector |
| 2 | 142 | | if (!model.x || model.z) |
| 2 | 143 | | lookAtDir.y = entityTransform.forward.y; |
| 2 | 144 | | if (!model.y) |
| 2 | 145 | | lookAtDir.x = entityTransform.forward.x; |
| | 146 | | } |
| | 147 | |
|
| 5 | 148 | | return lookAtDir.normalized; |
| | 149 | | } |
| | 150 | | } |
| | 151 | | } |