| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Linq; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.TestTools; |
| | 10 | |
|
| | 11 | | namespace Tests |
| | 12 | | { |
| | 13 | | public class AnimatorTests : IntegrationTestSuite_Legacy |
| | 14 | | { |
| | 15 | | [UnityTest] |
| | 16 | | public IEnumerator CreateAnimationComponent() |
| | 17 | | { |
| 1 | 18 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| | 19 | |
|
| 1 | 20 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null, |
| | 21 | | "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist"); |
| | 22 | |
|
| 1 | 23 | | TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, |
| | 24 | | JsonConvert.SerializeObject(new |
| | 25 | | { |
| | 26 | | src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb" |
| | 27 | | })); |
| | 28 | |
|
| 1 | 29 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 30 | | { |
| | 31 | | states = new DCLAnimator.Model.DCLAnimationState[] |
| | 32 | | { |
| | 33 | | new DCLAnimator.Model.DCLAnimationState |
| | 34 | | { |
| | 35 | | name = "clip01", |
| | 36 | | clip = "animation:0", |
| | 37 | | playing = true, |
| | 38 | | weight = 1, |
| | 39 | | speed = 1 |
| | 40 | | } |
| | 41 | | } |
| | 42 | | }; |
| | 43 | |
|
| 1 | 44 | | DCLAnimator animator = |
| | 45 | | TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel); |
| | 46 | |
|
| 1 | 47 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity); |
| 9 | 48 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded == true); |
| | 49 | |
|
| 1 | 50 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<Animation>() != null, |
| | 51 | | "'GLTFScene' child object with 'Animator' component should exist if the GLTF was loaded correctly."); |
| 1 | 52 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<DCLAnimator>() != null, |
| | 53 | | "'GLTFScene' child object with 'DCLAnimator' component should exist if the GLTF was loaded correctly."); |
| | 54 | |
|
| 1 | 55 | | yield return animator.routine; |
| | 56 | |
|
| 1 | 57 | | animator = entity.gameObject.GetComponentInChildren<DCLAnimator>(); |
| | 58 | |
|
| 1 | 59 | | Assert.IsTrue(animator.GetStateByString("clip01") != null, "dclAnimator.GetStateByString fail!"); |
| 1 | 60 | | Assert.IsTrue(animator.GetModel().states[0].clip != null, "dclAnimator clipReference is null!"); |
| 1 | 61 | | } |
| | 62 | |
|
| | 63 | | [UnityTest] |
| | 64 | | public IEnumerator DCLAnimatorResetAnimation() |
| | 65 | | { |
| 1 | 66 | | GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, |
| | 67 | | new LoadableShape.Model |
| | 68 | | { |
| | 69 | | src = TestAssetsUtils.GetPath() + "/GLB/Shark/shark_anim.gltf" |
| | 70 | | }); |
| 1 | 71 | | var entity = gltfShape.attachedEntities.First(); |
| | 72 | |
|
| 1 | 73 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 74 | | { |
| | 75 | | states = new[] |
| | 76 | | { |
| | 77 | | new DCLAnimator.Model.DCLAnimationState |
| | 78 | | { |
| | 79 | | name = "Bite", |
| | 80 | | clip = "shark_skeleton_bite", |
| | 81 | | playing = true, |
| | 82 | | weight = 1, |
| | 83 | | speed = 1 |
| | 84 | | }, |
| | 85 | | new DCLAnimator.Model.DCLAnimationState |
| | 86 | | { |
| | 87 | | name = "Swim", |
| | 88 | | clip = "shark_skeleton_swim", |
| | 89 | | playing = true, |
| | 90 | | weight = 1, |
| | 91 | | speed = 1 |
| | 92 | | } |
| | 93 | | } |
| | 94 | | }; |
| | 95 | |
|
| 1 | 96 | | DCLAnimator animator = |
| | 97 | | TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel); |
| 1 | 98 | | LoadWrapper gltfLoader = GLTFShape.GetLoaderForEntity(entity); |
| 9 | 99 | | yield return new WaitUntil(() => gltfLoader.alreadyLoaded); |
| | 100 | |
|
| 1 | 101 | | yield return animator.routine; |
| | 102 | |
|
| 1 | 103 | | animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 104 | |
|
| 1 | 105 | | yield return null; |
| | 106 | |
|
| 1 | 107 | | Animation animation = entity.gameObject.GetComponentInChildren<Animation>(); |
| 6 | 108 | | foreach (AnimationState animState in animation) |
| | 109 | | { |
| 2 | 110 | | Assert.AreNotEqual(0f, animState.time); |
| | 111 | | } |
| | 112 | |
|
| 1 | 113 | | animatorModel.states[1].shouldReset = true; |
| | 114 | |
|
| 1 | 115 | | yield return TestHelpers.EntityComponentUpdate(animator, animatorModel); |
| | 116 | |
|
| 1 | 117 | | animator.ResetAnimation(animator.GetStateByString("Swim")); |
| 6 | 118 | | foreach (AnimationState animState in animation) |
| | 119 | | { |
| 2 | 120 | | if (animator.GetStateByString("Swim").clipReference.name == animState.clip.name) |
| | 121 | | { |
| 1 | 122 | | Assert.AreEqual(0f, animState.time); |
| 1 | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 1 | 126 | | Assert.AreNotEqual(0f, animState.time); |
| | 127 | | } |
| | 128 | | } |
| 1 | 129 | | } |
| | 130 | |
|
| | 131 | | [UnityTest] |
| | 132 | | public IEnumerator DCLAnimatorResetAllAnimations() |
| | 133 | | { |
| 1 | 134 | | var gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, |
| | 135 | | new LoadableShape.Model |
| | 136 | | { |
| | 137 | | src = TestAssetsUtils.GetPath() + "/GLB/Shark/shark_anim.gltf" |
| | 138 | | }); |
| 1 | 139 | | var entity = gltfShape.attachedEntities.First(); |
| | 140 | |
|
| 1 | 141 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 142 | | { |
| | 143 | | states = new[] |
| | 144 | | { |
| | 145 | | new DCLAnimator.Model.DCLAnimationState |
| | 146 | | { |
| | 147 | | name = "Bite", |
| | 148 | | clip = "shark_skeleton_bite", |
| | 149 | | playing = true, |
| | 150 | | weight = 1, |
| | 151 | | speed = 1 |
| | 152 | | }, |
| | 153 | | new DCLAnimator.Model.DCLAnimationState |
| | 154 | | { |
| | 155 | | name = "Swim", |
| | 156 | | clip = "shark_skeleton_swim", |
| | 157 | | playing = true, |
| | 158 | | weight = 1, |
| | 159 | | speed = 1 |
| | 160 | | } |
| | 161 | | } |
| | 162 | | }; |
| | 163 | |
|
| 1 | 164 | | DCLAnimator animator = |
| | 165 | | TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, animatorModel); |
| 1 | 166 | | LoadWrapper gltfLoader = GLTFShape.GetLoaderForEntity(entity); |
| 9 | 167 | | yield return new WaitUntil(() => gltfLoader.alreadyLoaded); |
| | 168 | |
|
| 1 | 169 | | yield return animator.routine; |
| | 170 | |
|
| 1 | 171 | | animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 172 | |
|
| 1 | 173 | | yield return null; |
| | 174 | |
|
| 1 | 175 | | Animation animation = entity.gameObject.GetComponentInChildren<Animation>(); |
| | 176 | |
|
| 6 | 177 | | foreach (AnimationState animState in animation) |
| | 178 | | { |
| 2 | 179 | | Assert.AreNotEqual(0f, animState.time); |
| | 180 | | } |
| | 181 | |
|
| 1 | 182 | | animatorModel.states[0].shouldReset = true; |
| 1 | 183 | | animatorModel.states[1].shouldReset = true; |
| | 184 | |
|
| 1 | 185 | | yield return TestHelpers.EntityComponentUpdate(animator, animatorModel); |
| | 186 | |
|
| 6 | 187 | | foreach (AnimationState animState in animation) |
| | 188 | | { |
| 2 | 189 | | Assert.AreEqual(0f, animState.time); |
| | 190 | | } |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | [UnityTest] |
| 2 | 194 | | public IEnumerator AnimationComponentMissingValuesGetDefaultedOnUpdate() { yield return TestHelpers.TestEntityCo |
| | 195 | |
|
| | 196 | | [UnityTest] |
| | 197 | | public IEnumerator UpdateAnimationComponent() |
| | 198 | | { |
| 1 | 199 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| | 200 | |
|
| 1 | 201 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null, |
| | 202 | | "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist"); |
| | 203 | |
|
| 1 | 204 | | TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, |
| | 205 | | JsonConvert.SerializeObject(new |
| | 206 | | { |
| | 207 | | src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb" |
| | 208 | | })); |
| | 209 | |
|
| 1 | 210 | | string clipName = "animation:0"; |
| 1 | 211 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 212 | | { |
| | 213 | | states = new DCLAnimator.Model.DCLAnimationState[] |
| | 214 | | { |
| | 215 | | new DCLAnimator.Model.DCLAnimationState |
| | 216 | | { |
| | 217 | | name = "clip01", |
| | 218 | | clip = clipName, |
| | 219 | | playing = true, |
| | 220 | | weight = 1, |
| | 221 | | speed = 1, |
| | 222 | | looping = false |
| | 223 | | } |
| | 224 | | } |
| | 225 | | }; |
| | 226 | |
|
| 1 | 227 | | DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim |
| | 228 | |
|
| 1 | 229 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity); |
| 8 | 230 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded == true); |
| | 231 | |
|
| 1 | 232 | | Assert.IsTrue(animator.animComponent.isPlaying); |
| 1 | 233 | | Assert.AreEqual(animator.animComponent.clip.name, clipName); |
| 1 | 234 | | Assert.IsFalse(animator.animComponent.clip.wrapMode == WrapMode.Loop); |
| | 235 | |
|
| 1 | 236 | | yield return null; |
| | 237 | |
|
| | 238 | | // update component properties |
| 1 | 239 | | animatorModel.states[0].playing = false; |
| 1 | 240 | | animatorModel.states[0].looping = true; |
| 1 | 241 | | yield return TestHelpers.EntityComponentUpdate(animator, animatorModel); |
| | 242 | |
|
| 1 | 243 | | Assert.IsFalse(animator.animComponent.isPlaying); |
| 1 | 244 | | Assert.IsTrue(animator.animComponent.clip.wrapMode == WrapMode.Loop); |
| 1 | 245 | | } |
| | 246 | |
|
| | 247 | | [UnityTest] |
| | 248 | | [Explicit] |
| | 249 | | [Category("Explicit")] |
| | 250 | | public IEnumerator AnimationStartsAutomaticallyWithNoDCLAnimator() |
| | 251 | | { |
| | 252 | | // GLTFShape without DCLAnimator |
| 0 | 253 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| | 254 | |
|
| 0 | 255 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null, |
| | 256 | | "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist"); |
| | 257 | |
|
| 0 | 258 | | TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, |
| | 259 | | JsonConvert.SerializeObject(new |
| | 260 | | { |
| | 261 | | src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb" |
| | 262 | | })); |
| | 263 | |
|
| 0 | 264 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity); |
| 0 | 265 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded == true); |
| | 266 | |
|
| 0 | 267 | | Animation animation = entity.meshRootGameObject.GetComponentInChildren<Animation>(); |
| | 268 | |
|
| 0 | 269 | | Assert.IsTrue(animation != null); |
| 0 | 270 | | Assert.IsTrue(animation.isPlaying); |
| | 271 | |
|
| | 272 | | // GLTFShape with DCLAnimator |
| 0 | 273 | | var entity2 = TestHelpers.CreateSceneEntity(scene); |
| | 274 | |
|
| 0 | 275 | | Assert.IsTrue(entity2.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null, |
| | 276 | | "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist"); |
| | 277 | |
|
| 0 | 278 | | TestHelpers.CreateAndSetShape(scene, entity2.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, |
| | 279 | | JsonConvert.SerializeObject(new |
| | 280 | | { |
| | 281 | | src = TestAssetsUtils.GetPath() + "/GLB/CesiumMan/CesiumMan.glb" |
| | 282 | | })); |
| | 283 | |
|
| 0 | 284 | | string clipName = "animation:0"; |
| 0 | 285 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 286 | | { |
| | 287 | | states = new DCLAnimator.Model.DCLAnimationState[] |
| | 288 | | { |
| | 289 | | new DCLAnimator.Model.DCLAnimationState |
| | 290 | | { |
| | 291 | | name = "clip01", |
| | 292 | | clip = clipName, |
| | 293 | | playing = false, |
| | 294 | | weight = 1, |
| | 295 | | speed = 1, |
| | 296 | | looping = false |
| | 297 | | } |
| | 298 | | } |
| | 299 | | }; |
| | 300 | |
|
| 0 | 301 | | DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim |
| | 302 | |
|
| 0 | 303 | | LoadWrapper gltfShape2 = GLTFShape.GetLoaderForEntity(entity); |
| 0 | 304 | | yield return new WaitUntil(() => gltfShape2.alreadyLoaded == true); |
| | 305 | |
|
| 0 | 306 | | Assert.IsTrue(animator.animComponent != null); |
| 0 | 307 | | Assert.AreEqual(animator.animComponent.clip.name, clipName); |
| 0 | 308 | | Assert.IsFalse(animator.animComponent.isPlaying); |
| 0 | 309 | | } |
| | 310 | |
|
| | 311 | | [UnityTest] |
| | 312 | | public IEnumerator NonSkeletalAnimationsSupport() |
| | 313 | | { |
| 1 | 314 | | var entity = TestHelpers.CreateSceneEntity(scene); |
| | 315 | |
|
| 1 | 316 | | TestHelpers.SetEntityTransform(scene, entity, new Vector3(8, 2, 8), Quaternion.identity, Vector3.one); |
| | 317 | |
|
| 1 | 318 | | Assert.IsTrue(entity.gameObject.GetComponentInChildren<UnityGLTF.InstantiatedGLTFObject>() == null, |
| | 319 | | "Since the shape hasn't been updated yet, the 'GLTFScene' child object shouldn't exist"); |
| | 320 | |
|
| 1 | 321 | | TestHelpers.CreateAndSetShape(scene, entity.entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, |
| | 322 | | JsonConvert.SerializeObject(new |
| | 323 | | { |
| | 324 | | src = TestAssetsUtils.GetPath() + "/GLB/non-skeletal-3-transformations.glb" |
| | 325 | | })); |
| | 326 | |
|
| 1 | 327 | | string clipName = "All"; |
| 1 | 328 | | DCLAnimator.Model animatorModel = new DCLAnimator.Model |
| | 329 | | { |
| | 330 | | states = new DCLAnimator.Model.DCLAnimationState[] |
| | 331 | | { |
| | 332 | | new DCLAnimator.Model.DCLAnimationState |
| | 333 | | { |
| | 334 | | name = "clip01", |
| | 335 | | clip = clipName, |
| | 336 | | playing = false, |
| | 337 | | weight = 1, |
| | 338 | | speed = 1, |
| | 339 | | looping = false |
| | 340 | | } |
| | 341 | | } |
| | 342 | | }; |
| | 343 | |
|
| 1 | 344 | | DCLAnimator animator = TestHelpers.EntityComponentCreate<DCLAnimator, DCLAnimator.Model>(scene, entity, anim |
| | 345 | |
|
| 1 | 346 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(entity); |
| 5 | 347 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded == true); |
| | 348 | |
|
| 1 | 349 | | animator.animComponent.cullingType = AnimationCullingType.AlwaysAnimate; |
| | 350 | |
|
| 1 | 351 | | Assert.IsTrue(!animator.animComponent.isPlaying); |
| 1 | 352 | | Assert.AreEqual(animator.animComponent.clip.name, clipName); |
| 1 | 353 | | Assert.IsFalse(animator.animComponent.clip.wrapMode == WrapMode.Loop); |
| | 354 | |
|
| 1 | 355 | | Transform animatedGameObject = animator.animComponent.transform.GetChild(0); |
| | 356 | |
|
| 1 | 357 | | Vector3 originalScale = animatedGameObject.transform.localScale; |
| 1 | 358 | | Vector3 originalPos = animatedGameObject.transform.localPosition; |
| 1 | 359 | | Quaternion originalRot = animatedGameObject.transform.localRotation; |
| | 360 | |
|
| | 361 | | // start animation |
| 1 | 362 | | animatorModel.states[0].playing = true; |
| 1 | 363 | | yield return TestHelpers.EntityComponentUpdate(animator, animatorModel); |
| | 364 | |
|
| 1 | 365 | | yield return new WaitForSeconds(0.1f); |
| | 366 | |
|
| 1 | 367 | | Assert.IsFalse(animatedGameObject.localScale == originalScale); |
| 1 | 368 | | Assert.IsFalse(animatedGameObject.localPosition == originalPos); |
| 1 | 369 | | Assert.IsFalse(animatedGameObject.localRotation == originalRot); |
| 1 | 370 | | } |
| | 371 | | } |
| | 372 | | } |