| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using System; |
| | 6 | | using UnityEditor; |
| | 7 | | using UnityEditor.SceneManagement; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | | using Object = UnityEngine.Object; |
| | 11 | |
|
| | 12 | | namespace DCL.ABConverter |
| | 13 | | { |
| | 14 | | public static class VisualTests |
| | 15 | | { |
| 0 | 16 | | static readonly string baselinePath = AssetBundlesVisualTestHelpers.baselineImagesPath; |
| 0 | 17 | | static readonly string testImagesPath = AssetBundlesVisualTestHelpers.testImagesPath; |
| 0 | 18 | | static string abPath = Application.dataPath + "/../AssetBundles/"; |
| 0 | 19 | | static int skippedAssets = 0; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Instantiate all locally-converted GLTFs in both formats (GLTF and Asset Bundle) and |
| | 23 | | /// compare them visually. If a visual test fails, the AB is deleted to avoid uploading it |
| | 24 | | /// </summary> |
| | 25 | | public static IEnumerator TestConvertedAssets(Environment env = null, Action<int> OnFinish = null) |
| | 26 | | { |
| 0 | 27 | | if (Utils.ParseOption(Config.CLI_SET_CUSTOM_OUTPUT_ROOT_PATH, 1, out string[] outputPath)) |
| | 28 | | { |
| 0 | 29 | | abPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), outputPath[0] + "/"); |
| | 30 | |
|
| 0 | 31 | | Debug.Log($"Visual Test Detection: -output PATH param found, setting ABPath as '{abPath}'"); |
| 0 | 32 | | } |
| | 33 | | else |
| | 34 | | { |
| 0 | 35 | | Debug.Log($"Visual Test Detection: -output PATH param NOT found, setting ABPath as '{abPath}'"); |
| | 36 | | } |
| | 37 | |
|
| 0 | 38 | | if (!System.IO.Directory.Exists(abPath)) |
| | 39 | | { |
| 0 | 40 | | Debug.Log($"Visual Test Detection: ABs path '{abPath}' doesn't exist..."); |
| 0 | 41 | | SkipAllAssets(); |
| 0 | 42 | | OnFinish?.Invoke(skippedAssets); |
| 0 | 43 | | yield break; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | Debug.Log("Visual Test Detection: Starting converted assets testing..."); |
| | 47 | |
|
| 0 | 48 | | var scene = EditorSceneManager.OpenScene($"Assets/ABConverter/VisualTestScene.unity", OpenSceneMode.Single); |
| 0 | 49 | | yield return new WaitUntil(() => scene.isLoaded); |
| | 50 | |
|
| 0 | 51 | | AssetBundlesVisualTestHelpers.baselineImagesPath += "ABConverter/"; |
| 0 | 52 | | AssetBundlesVisualTestHelpers.testImagesPath += "ABConverter/"; |
| 0 | 53 | | skippedAssets = 0; |
| | 54 | |
|
| 0 | 55 | | var gltfs = LoadAndInstantiateAllGltfAssets(); |
| | 56 | |
|
| 0 | 57 | | if (gltfs.Length == 0) |
| | 58 | | { |
| 0 | 59 | | Debug.Log("Visual Test Detection: no instantiated GLTFs..."); |
| 0 | 60 | | SkipAllAssets(); |
| 0 | 61 | | OnFinish?.Invoke(skippedAssets); |
| 0 | 62 | | yield break; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Take prewarm snapshot to make sure the scene is correctly loaded |
| 0 | 66 | | yield return TakeObjectSnapshot(new GameObject(), $"ABConverter_Warmup.png"); |
| | 67 | |
|
| 0 | 68 | | AssetBundlesVisualTestHelpers.generateBaseline = true; |
| | 69 | |
|
| 0 | 70 | | foreach (GameObject go in gltfs) |
| | 71 | | { |
| 0 | 72 | | go.SetActive(false); |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | foreach (GameObject go in gltfs) |
| | 76 | | { |
| 0 | 77 | | go.SetActive(true); |
| | 78 | |
|
| 0 | 79 | | yield return TakeObjectSnapshot(go, $"ABConverter_{go.name}.png"); |
| | 80 | |
|
| 0 | 81 | | go.SetActive(false); |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | foreach (GameObject go in gltfs) |
| | 85 | | { |
| 0 | 86 | | go.SetActive(true); |
| | 87 | |
|
| 0 | 88 | | yield return TakeObjectSnapshot(go, $"ABConverter_{go.name}.png"); |
| | 89 | |
|
| 0 | 90 | | go.SetActive(false); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | AssetBundlesVisualTestHelpers.generateBaseline = false; |
| | 94 | |
|
| 0 | 95 | | var abs = LoadAndInstantiateAllAssetBundles(); |
| | 96 | |
|
| 0 | 97 | | if (abs.Length == 0) |
| | 98 | | { |
| 0 | 99 | | Debug.Log("Visual Test Detection: no instantiated ABs..."); |
| 0 | 100 | | SkipAllAssets(); |
| 0 | 101 | | OnFinish?.Invoke(skippedAssets); |
| 0 | 102 | | yield break; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | foreach (GameObject go in abs) |
| | 106 | | { |
| 0 | 107 | | go.SetActive(false); |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | foreach (GameObject go in abs) |
| | 111 | | { |
| 0 | 112 | | string testName = $"ABConverter_{go.name}.png"; |
| | 113 | |
|
| 0 | 114 | | go.SetActive(true); |
| | 115 | |
|
| 0 | 116 | | yield return TakeObjectSnapshot(go, testName); |
| | 117 | |
|
| 0 | 118 | | bool result = AssetBundlesVisualTestHelpers.TestSnapshot( |
| | 119 | | AssetBundlesVisualTestHelpers.baselineImagesPath + testName, |
| | 120 | | AssetBundlesVisualTestHelpers.testImagesPath + testName, |
| | 121 | | 95, |
| | 122 | | false); |
| | 123 | |
|
| | 124 | | // Delete failed AB files to avoid uploading them |
| 0 | 125 | | if (!result && env != null) |
| | 126 | | { |
| 0 | 127 | | string filePath = abPath + go.name; |
| 0 | 128 | | if (env.file.Exists(filePath)) |
| | 129 | | { |
| 0 | 130 | | env.file.Delete(filePath); |
| 0 | 131 | | env.file.Delete(filePath + ".depmap"); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | skippedAssets++; |
| | 135 | |
|
| | 136 | | // TODO: Notify some metrics API or something to let us know that this asset has conversion problems |
| 0 | 137 | | Debug.Log("Visual Test Detection: FAILED converting asset -> " + go.name); |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | go.SetActive(false); |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | AssetBundlesVisualTestHelpers.baselineImagesPath = baselinePath; |
| 0 | 144 | | AssetBundlesVisualTestHelpers.testImagesPath = testImagesPath; |
| | 145 | |
|
| 0 | 146 | | Debug.Log("Visual Test Detection: Finished converted assets testing...skipped assets: " + skippedAssets); |
| 0 | 147 | | OnFinish?.Invoke(skippedAssets); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Set skippedAssets to the amount of target assets |
| | 152 | | /// </summary> |
| 0 | 153 | | private static void SkipAllAssets() { skippedAssets = AssetDatabase.FindAssets($"t:GameObject", new[] { "Assets/ |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Position camera based on renderer bounds and take snapshot |
| | 157 | | /// </summary> |
| | 158 | | private static IEnumerator TakeObjectSnapshot(GameObject targetGO, string testName) |
| | 159 | | { |
| 0 | 160 | | Vector3 originalScale = targetGO.transform.localScale; |
| 0 | 161 | | var renderers = targetGO.GetComponentsInChildren<Renderer>(); |
| | 162 | |
|
| | 163 | | // unify all child renderer bounds and use that to position the snapshot camera |
| 0 | 164 | | var mergedBounds = MeshUtils.BuildMergedBounds(renderers); |
| | 165 | |
|
| | 166 | | // Some objects are imported super small (like 0.00x in scale) and we can barely see them in the snapshots |
| 0 | 167 | | if (mergedBounds.size.magnitude < 1f) |
| | 168 | | { |
| 0 | 169 | | targetGO.transform.localScale *= 100; |
| 0 | 170 | | mergedBounds = MeshUtils.BuildMergedBounds(renderers); |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | Vector3 offset = mergedBounds.extents; |
| 0 | 174 | | offset.x = Mathf.Max(1, offset.x); |
| 0 | 175 | | offset.y = Mathf.Max(1, offset.y); |
| 0 | 176 | | offset.z = Mathf.Max(1, offset.z); |
| | 177 | |
|
| 0 | 178 | | Vector3 cameraPosition = new Vector3(mergedBounds.min.x - offset.x, mergedBounds.max.y + offset.y, mergedBou |
| | 179 | |
|
| 0 | 180 | | yield return AssetBundlesVisualTestHelpers.TakeSnapshot(testName, Camera.main, cameraPosition, mergedBounds. |
| | 181 | |
|
| 0 | 182 | | targetGO.transform.localScale = originalScale; |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary> |
| | 186 | | /// Instantiate all local GLTFs found in the "_Downloaded" directory |
| | 187 | | /// </summary> |
| | 188 | | public static GameObject[] LoadAndInstantiateAllGltfAssets() |
| | 189 | | { |
| 0 | 190 | | var assets = AssetDatabase.FindAssets($"t:GameObject", new[] { "Assets/_Downloaded" }); |
| | 191 | |
|
| 0 | 192 | | List<GameObject> importedGLTFs = new List<GameObject>(); |
| | 193 | |
|
| 0 | 194 | | foreach (var guid in assets) |
| | 195 | | { |
| 0 | 196 | | GameObject gltf = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid)); |
| 0 | 197 | | var importedGLTF = Object.Instantiate(gltf); |
| 0 | 198 | | importedGLTF.name = importedGLTF.name.Replace("(Clone)", ""); |
| | 199 | |
|
| 0 | 200 | | PatchSkeletonlessSkinnedMeshRenderer(importedGLTF.gameObject.GetComponentInChildren<SkinnedMeshRenderer> |
| | 201 | |
|
| 0 | 202 | | importedGLTFs.Add(importedGLTF); |
| | 203 | | } |
| | 204 | |
|
| 0 | 205 | | return importedGLTFs.ToArray(); |
| | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Search for local GLTFs in "_Downloaded" and use those hashes to find their corresponding |
| | 210 | | /// Asset Bundle files, then instantiate those ABs in the Unity scene |
| | 211 | | /// </summary> |
| | 212 | | public static GameObject[] LoadAndInstantiateAllAssetBundles() |
| | 213 | | { |
| 0 | 214 | | Caching.ClearCache(); |
| | 215 | |
|
| 0 | 216 | | string workingFolderName = "_Downloaded"; |
| | 217 | |
|
| 0 | 218 | | var pathList = Directory.GetDirectories(Application.dataPath + "/" + workingFolderName); |
| | 219 | |
|
| 0 | 220 | | List<string> dependencyAbs = new List<string>(); |
| 0 | 221 | | List<string> mainAbs = new List<string>(); |
| | 222 | |
|
| 0 | 223 | | foreach (var paths in pathList) |
| | 224 | | { |
| 0 | 225 | | var hash = new DirectoryInfo(paths).Name; |
| 0 | 226 | | var path = "Assets/" + workingFolderName + "/" + hash; |
| 0 | 227 | | var guids = AssetDatabase.FindAssets("t:GameObject", new[] { path }); |
| | 228 | |
|
| | 229 | | // NOTE(Brian): If no gameObjects are found, we assume they are dependency assets (textures, etc). |
| 0 | 230 | | if (guids.Length == 0) |
| | 231 | | { |
| | 232 | | // We need to avoid adding dependencies that are NOT converted to ABs (like .bin files) |
| 0 | 233 | | if (AssetDatabase.FindAssets("t:Texture", new[] { path }).Length != 0) |
| | 234 | | { |
| 0 | 235 | | dependencyAbs.Add(hash); |
| | 236 | | } |
| 0 | 237 | | } |
| | 238 | | else |
| | 239 | | { |
| | 240 | | // Otherwise we assume they are gltfs. |
| 0 | 241 | | mainAbs.Add(hash); |
| | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| | 245 | | // NOTE(Brian): We need to store the asset bundles so they can be unloaded later. |
| 0 | 246 | | List<AssetBundle> loadedAbs = new List<AssetBundle>(); |
| | 247 | |
|
| 0 | 248 | | foreach (var hash in dependencyAbs) |
| | 249 | | { |
| 0 | 250 | | string path = abPath + hash; |
| 0 | 251 | | var req = UnityWebRequestAssetBundle.GetAssetBundle(path); |
| | 252 | |
|
| 0 | 253 | | if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX || SystemInfo.operatingSystemFamily |
| 0 | 254 | | req.url = req.url.Replace("http://localhost", "file:///"); |
| | 255 | |
|
| 0 | 256 | | req.SendWebRequest(); |
| | 257 | |
|
| 0 | 258 | | while (!req.isDone) { } |
| | 259 | |
|
| 0 | 260 | | if (!req.WebRequestSucceded()) |
| | 261 | | { |
| 0 | 262 | | Debug.Log("Visual Test Detection: Failed to download dependency asset: " + hash); |
| 0 | 263 | | continue; |
| | 264 | | } |
| | 265 | |
|
| 0 | 266 | | var assetBundle = DownloadHandlerAssetBundle.GetContent(req); |
| 0 | 267 | | assetBundle.LoadAllAssets(); |
| 0 | 268 | | loadedAbs.Add(assetBundle); |
| | 269 | | } |
| | 270 | |
|
| 0 | 271 | | List<GameObject> results = new List<GameObject>(); |
| | 272 | |
|
| 0 | 273 | | foreach (var hash in mainAbs) |
| | 274 | | { |
| 0 | 275 | | string path = abPath + hash; |
| 0 | 276 | | var req = UnityWebRequestAssetBundle.GetAssetBundle(path); |
| | 277 | |
|
| 0 | 278 | | if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX || SystemInfo.operatingSystemFamily |
| 0 | 279 | | req.url = req.url.Replace("http://localhost", "file:///"); |
| | 280 | |
|
| 0 | 281 | | req.SendWebRequest(); |
| | 282 | |
|
| 0 | 283 | | while (!req.isDone) { } |
| | 284 | |
|
| 0 | 285 | | if (!req.WebRequestSucceded()) |
| | 286 | | { |
| 0 | 287 | | Debug.Log("Visual Test Detection: Failed to instantiate AB, missing source file for : " + hash); |
| 0 | 288 | | skippedAssets++; |
| 0 | 289 | | continue; |
| | 290 | | } |
| | 291 | |
|
| 0 | 292 | | var assetBundle = DownloadHandlerAssetBundle.GetContent(req); |
| 0 | 293 | | Object[] assets = assetBundle.LoadAllAssets(); |
| | 294 | |
|
| 0 | 295 | | foreach (Object asset in assets) |
| | 296 | | { |
| 0 | 297 | | if (asset is Material material) |
| | 298 | | { |
| 0 | 299 | | material.shader = Shader.Find("DCL/Universal Render Pipeline/Lit"); |
| | 300 | | } |
| | 301 | |
|
| 0 | 302 | | if (asset is GameObject assetAsGameObject) |
| | 303 | | { |
| 0 | 304 | | GameObject instance = Object.Instantiate(assetAsGameObject); |
| | 305 | |
|
| 0 | 306 | | PatchSkeletonlessSkinnedMeshRenderer(instance.GetComponentInChildren<SkinnedMeshRenderer>()); |
| | 307 | |
|
| 0 | 308 | | results.Add(instance); |
| 0 | 309 | | instance.name = instance.name.Replace("(Clone)", ""); |
| | 310 | | } |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | loadedAbs.Add(assetBundle); |
| | 314 | | } |
| | 315 | |
|
| 0 | 316 | | foreach (var ab in loadedAbs) |
| | 317 | | { |
| 0 | 318 | | ab.Unload(false); |
| | 319 | | } |
| | 320 | |
|
| 0 | 321 | | return results.ToArray(); |
| | 322 | | } |
| | 323 | |
|
| | 324 | | /// <summary> |
| | 325 | | /// Wearables that are not body-shapes are optimized getting rid of the skeleton, so if this |
| | 326 | | /// SkinnedMeshRenderer is missing its root bone, we replace the renderer to make it rendereable |
| | 327 | | /// for the visual tests. In runtime, WearableController.SetAnimatorBones() takes care of the |
| | 328 | | /// root bone setup. |
| | 329 | | /// </summary> |
| | 330 | | private static void PatchSkeletonlessSkinnedMeshRenderer(SkinnedMeshRenderer skinnedMeshRenderer) |
| | 331 | | { |
| 0 | 332 | | if (skinnedMeshRenderer == null || skinnedMeshRenderer.rootBone != null) |
| 0 | 333 | | return; |
| | 334 | |
|
| 0 | 335 | | MeshRenderer meshRenderer = skinnedMeshRenderer.gameObject.AddComponent<MeshRenderer>(); |
| 0 | 336 | | meshRenderer.sharedMaterials = skinnedMeshRenderer.sharedMaterials; |
| | 337 | |
|
| 0 | 338 | | Object.DestroyImmediate(skinnedMeshRenderer); |
| 0 | 339 | | } |
| | 340 | | } |
| | 341 | | } |