| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using DCL; |
| | 9 | | using Newtonsoft.Json; |
| | 10 | | using UnityEngine; |
| | 11 | | using UnityEngine.TestTools; |
| | 12 | | using UnityGLTF; |
| | 13 | | using UnityGLTF.Cache; |
| | 14 | | using UnityGLTF.Cache; |
| | 15 | |
|
| | 16 | | public class GLTFImporterTests : IntegrationTestSuite_Legacy |
| | 17 | | { |
| | 18 | | public IEnumerator LoadModel(string path, System.Action<IDCLEntity, InstantiatedGLTFObject> OnFinishLoading) |
| | 19 | | { |
| 5 | 20 | | string src = TestAssetsUtils.GetPath() + path; |
| | 21 | |
|
| 5 | 22 | | IDCLEntity entity = null; |
| | 23 | |
|
| 5 | 24 | | GLTFShape gltfShape = TestHelpers.CreateEntityWithGLTFShape(scene, Vector3.zero, src, out entity); |
| | 25 | |
|
| 5 | 26 | | yield return gltfShape.routine; |
| | 27 | |
|
| 5 | 28 | | yield return new WaitForAllMessagesProcessed(); |
| 43 | 29 | | yield return new WaitUntil(() => GLTFComponent.downloadingCount == 0); |
| | 30 | |
|
| 5 | 31 | | if (OnFinishLoading != null) |
| | 32 | | { |
| 5 | 33 | | OnFinishLoading.Invoke(entity, entity.meshRootGameObject.GetComponentInChildren<InstantiatedGLTFObject>()); |
| | 34 | | } |
| 5 | 35 | | } |
| | 36 | |
|
| | 37 | | [Test] |
| | 38 | | public void ImporterCanLoadSkeletonsByDefault() |
| | 39 | | { |
| 1 | 40 | | var importer = new GLTFSceneImporter("", "", null, null); |
| 1 | 41 | | Assert.IsTrue( importer.importSkeleton, "Skeleton importing should be true by default or avatars won't load corr |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | [UnityTest] |
| | 45 | | [Explicit("Test takes too long")] |
| | 46 | | [Category("Explicit")] |
| | 47 | | public IEnumerator TrevorModelHasProperScaling() |
| | 48 | | { |
| 0 | 49 | | InstantiatedGLTFObject trevorModel = null; |
| 0 | 50 | | yield return LoadModel("/GLB/Trevor/Trevor.glb", (entity, m) => trevorModel = m); |
| | 51 | |
|
| 0 | 52 | | Transform child = trevorModel.transform.GetChild(0).GetChild(0); |
| 0 | 53 | | Vector3 scale = child.lossyScale; |
| 0 | 54 | | Assert.AreEqual(new Vector3(0.049f, 0.049f, 0.049f).ToString(), scale.ToString()); |
| 0 | 55 | | yield return null; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | [UnityTest] |
| | 59 | | [Explicit("Test takes too long")] |
| | 60 | | [Category("Explicit")] |
| | 61 | | public IEnumerator TrevorModelHasProperTopology() |
| | 62 | | { |
| 0 | 63 | | InstantiatedGLTFObject trevorModel = null; |
| 0 | 64 | | yield return LoadModel("/GLB/Trevor/Trevor.glb", (entity, m) => trevorModel = m); |
| | 65 | |
|
| 0 | 66 | | Assert.IsTrue(trevorModel.transform.childCount == 1); |
| 0 | 67 | | Assert.IsTrue(trevorModel.transform.GetChild(0).childCount == 2); |
| 0 | 68 | | Assert.IsTrue(trevorModel.transform.GetChild(0).GetChild(0).name.Contains("Character_Avatar")); |
| 0 | 69 | | Assert.IsTrue(trevorModel.transform.GetChild(0).GetChild(1).name.Contains("mixamorig")); |
| 0 | 70 | | yield return null; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | [UnityTest] |
| | 74 | | [Explicit("Test takes too long")] |
| | 75 | | [Category("Explicit")] |
| | 76 | | public IEnumerator GLTFWithoutSkeletonIdIsLoadingCorrectly() |
| | 77 | | { |
| 0 | 78 | | InstantiatedGLTFObject trevorModel = null; |
| 0 | 79 | | yield return LoadModel("/GLB/Avatar/Avatar_Idle.glb", (entity, m) => trevorModel = m); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | [UnityTest] |
| | 83 | | public IEnumerator TwoGLTFsWithSameExternalTexturePathDontCollide() |
| | 84 | | { |
| 1 | 85 | | InstantiatedGLTFObject trunk1 = null; |
| 1 | 86 | | InstantiatedGLTFObject trunk2 = null; |
| | 87 | |
|
| 1 | 88 | | PersistentAssetCache.ImageCacheByUri.Clear(); |
| | 89 | |
|
| 2 | 90 | | yield return LoadModel("/GLTF/Trunk/Trunk.gltf", (entity, m) => trunk1 = m); |
| 2 | 91 | | yield return LoadModel("/GLTF/Trunk2/Trunk.gltf", (entity, m) => trunk2 = m); |
| 1 | 92 | | UnityEngine.Assertions.Assert.AreEqual(2, PersistentAssetCache.ImageCacheByUri.Count, "Image cache is colliding! |
| 1 | 93 | | UnityEngine.Assertions.Assert.AreEqual(2, PersistentAssetCache.StreamCacheByUri.Count, "Buffer cache is collidin |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | [UnityTest] |
| | 97 | | public IEnumerator CurvesAreOptimizedCorrectly() |
| | 98 | | { |
| 1 | 99 | | var curvesSource = Resources.Load<AnimationCurveContainer>("CurveOptimizedCorrectlySource"); |
| | 100 | | //NOTE(Brian): We are going to output the optimization result in this SO, so it can be debugged more easily |
| 1 | 101 | | var curvesResult = Resources.Load<AnimationCurveContainer>("CurveOptimizedCorrectlyResult"); |
| | 102 | |
|
| 1 | 103 | | curvesResult.curves = new AnimationCurve[curvesSource.curves.Length]; |
| | 104 | |
|
| 18 | 105 | | for (int i = 0; i < curvesSource.curves.Length; i++) |
| | 106 | | { |
| 8 | 107 | | var curve = curvesSource.curves[i]; |
| | 108 | |
|
| 8 | 109 | | List<Keyframe> keys = new List<Keyframe>(); |
| | 110 | |
|
| 238 | 111 | | for (int i1 = 0; i1 < curve.length; i1++) |
| | 112 | | { |
| 111 | 113 | | keys.Add(curve[i1]); |
| | 114 | | } |
| | 115 | |
|
| 8 | 116 | | var result = GLTFSceneImporter.OptimizeKeyFrames(keys.ToArray()); |
| 8 | 117 | | var modifiedCurve = new AnimationCurve(result); |
| | 118 | |
|
| 8 | 119 | | curvesResult.curves[i] = modifiedCurve; |
| | 120 | |
|
| 528 | 121 | | for (float time = 0; time < 1.0f; time += 0.032f) |
| | 122 | | { |
| 256 | 123 | | var v1 = curve.Evaluate(time); |
| 256 | 124 | | var v2 = modifiedCurve.Evaluate(time); |
| | 125 | |
|
| 256 | 126 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(v1, v2, 0.01f); |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| 1 | 130 | | yield break; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | [UnityTest] |
| | 134 | | public IEnumerator TexturesCacheWorksProperly() |
| | 135 | | { |
| 1 | 136 | | IDCLEntity entity = null; |
| 1 | 137 | | PersistentAssetCache.ImageCacheByUri.Clear(); |
| 2 | 138 | | yield return LoadModel("/GLTF/Trunk/Trunk.gltf", (e, model) => entity = e); |
| | 139 | |
|
| 1 | 140 | | UnityEngine.Assertions.Assert.AreEqual(1, PersistentAssetCache.ImageCacheByUri.Count); |
| 1 | 141 | | scene.RemoveEntity(entity.entityId); |
| 1 | 142 | | PoolManager.i.Cleanup(); |
| | 143 | |
|
| 1 | 144 | | yield return null; |
| | 145 | |
|
| 1 | 146 | | UnityEngine.Assertions.Assert.AreEqual(0, PersistentAssetCache.ImageCacheByUri.Count); |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | [UnityTest] |
| | 150 | | public IEnumerator TexturesOffsetAndScaleWorkProperly() |
| | 151 | | { |
| 1 | 152 | | IDCLEntity entity = null; |
| 1 | 153 | | PersistentAssetCache.ImageCacheByUri.Clear(); |
| 2 | 154 | | yield return LoadModel("/GLB/PlaneUVsOffset/planeUVsOffset.glb", (e, model) => entity = e); |
| | 155 | |
|
| 1 | 156 | | MeshRenderer meshRenderer = entity.gameObject.GetComponentInChildren<MeshRenderer>(); |
| | 157 | |
|
| 1 | 158 | | var unityOffset = GLTFSceneImporter.GLTFOffsetToUnitySpace(new Vector2(0.35f, 0.35f), 2.5f); |
| 1 | 159 | | Assert.AreEqual(unityOffset, meshRenderer.material.GetTextureOffset("_BaseMap")); |
| 1 | 160 | | Assert.AreEqual(Vector2.one * 2.5f, meshRenderer.material.GetTextureScale("_BaseMap")); |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | [UnityTest] |
| | 164 | | public IEnumerator TexturesProcessTexCoords() |
| | 165 | | { |
| 1 | 166 | | IDCLEntity entity = null; |
| 1 | 167 | | PersistentAssetCache.ImageCacheByUri.Clear(); |
| 2 | 168 | | yield return LoadModel("/GLB/PlaneUVsMultichannel/PlaneUVsMultichannel.glb", (e, model) => entity = e); |
| | 169 | |
|
| 1 | 170 | | MeshRenderer meshRenderer = entity.gameObject.GetComponentInChildren<MeshRenderer>(); |
| | 171 | |
|
| 1 | 172 | | Assert.AreEqual(1, meshRenderer.material.GetInt("_BaseMapUVs")); |
| 1 | 173 | | Assert.AreEqual(1, meshRenderer.material.GetInt("_NormalMapUVs")); |
| 1 | 174 | | Assert.AreEqual(1, meshRenderer.material.GetInt("_MetallicMapUVs")); |
| 1 | 175 | | Assert.AreEqual(1, meshRenderer.material.GetInt("_EmissiveMapUVs")); |
| 1 | 176 | | } |
| | 177 | | } |