From 41585ec96f401969d52561d62d99a7cdb63ce83e Mon Sep 17 00:00:00 2001 From: Patricio Guerra Date: Thu, 14 Mar 2024 17:03:16 -0300 Subject: [PATCH] feat: desktop animations --- .../AssetBundleConverter.cs | 9 +++-- .../Editor/CustomGltfImporter.cs | 34 ++++++++++++++++++- .../Tests/AssetBundleConverterShould.cs | 6 ++-- .../Default/DefaultGltfImporter.cs | 5 +-- .../Wrappers/Interfaces/IGltfImporter.cs | 4 ++- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/AssetBundleConverter.cs b/asset-bundle-converter/Assets/AssetBundleConverter/AssetBundleConverter.cs index 8c71fa83..13f5bf60 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/AssetBundleConverter.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/AssetBundleConverter.cs @@ -281,9 +281,11 @@ private async Task ProcessAllGltfs() ContentMap[] contentMap = contentTable.Select(kvp => new ContentMap(kvp.Key, kvp.Value)).ToArray(); + AnimationMethod animationMethod = GetAnimationMethod(); + var importSettings = new ImportSettings { - AnimationMethod = AnimationMethod.Legacy, + AnimationMethod = animationMethod, NodeNameMethod = NameImportMethod.OriginalUnique, AnisotropicFilterLevel = 0, GenerateMipMaps = false @@ -341,7 +343,7 @@ private async Task ProcessAllGltfs() log.Verbose($"Importing {relativePath}"); configureGltftime.Start(); - bool importerSuccess = env.gltfImporter.ConfigureImporter(relativePath, contentMap, gltf.AssetPath.fileRootPath, gltf.AssetPath.hash, settings.shaderType); + bool importerSuccess = env.gltfImporter.ConfigureImporter(relativePath, contentMap, gltf.AssetPath.fileRootPath, gltf.AssetPath.hash, settings.shaderType, animationMethod); configureGltftime.Stop(); if (importerSuccess) @@ -417,6 +419,9 @@ private async Task ProcessAllGltfs() return false; } + private AnimationMethod GetAnimationMethod() => + settings.buildTarget is BuildTarget.StandaloneWindows64 or BuildTarget.StandaloneOSX ? AnimationMethod.Mecanim : AnimationMethod.Legacy; + private void ExtractEmbedMaterialsFromGltf(List textures, GltfImportSettings gltf, IGltfImport gltfImport, string gltfUrl) { Profiler.BeginSample("ExtractEmbedMaterials"); diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/Editor/CustomGltfImporter.cs b/asset-bundle-converter/Assets/AssetBundleConverter/Editor/CustomGltfImporter.cs index cf084d78..de61b1b5 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/Editor/CustomGltfImporter.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/Editor/CustomGltfImporter.cs @@ -75,7 +75,12 @@ public override void OnImportAsset(AssetImportContext ctx) if (!useOriginalMaterials) SetupCustomMaterialGenerator(new AssetBundleConverterMaterialGenerator(AssetBundleConverterMaterialGenerator.UseNewShader(EditorUserBuildSettings.activeBuildTarget))); - try { base.OnImportAsset(ctx); } + try + { + base.OnImportAsset(ctx); + + SwapAnimatorPostProcess(ctx); + } catch (Exception e) { Debug.LogError("UNCAUGHT FATAL: Failed to Import GLTF " + hash); @@ -84,6 +89,33 @@ public override void OnImportAsset(AssetImportContext ctx) } } + // In explorer alpha we are using non legacy animations and since we cannot create an Animator graph, we just add the clips to an Animation component so we can fetch them easily + // we dont even need to check the current platform target since animators only are created by desktop targets + private void SwapAnimatorPostProcess(AssetImportContext ctx) + { + GameObject gameObject = ctx.mainObject as GameObject; + + if (gameObject != null) + { + Animator animator = gameObject.GetComponent(); + + if (animator != null) + { + DestroyImmediate(animator); + var clips = m_Gltf.GetAnimationClips(); + var animation = gameObject.AddComponent(); + + foreach (AnimationClip animationClip in clips) + { + // we trick the Animation component believing that we are truly adding a legacy animation + animationClip.legacy = true; + animation.AddClip(animationClip, animationClip.name); + animationClip.legacy = false; + } + } + } + } + protected override void PreProcessGameObjects(GameObject sceneGo) { var meshFilters = sceneGo.GetComponentsInChildren(); diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/Tests/AssetBundleConverterShould.cs b/asset-bundle-converter/Assets/AssetBundleConverter/Tests/AssetBundleConverterShould.cs index 1c71d2a4..44350b9d 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/Tests/AssetBundleConverterShould.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/Tests/AssetBundleConverterShould.cs @@ -151,7 +151,7 @@ public async Task GltfAssetIsProcessed() var gltf = Substitute.For(); gltfImporter.GetImporter(Arg.Any(), Arg.Any>(), Arg.Any(), Arg.Any()).Returns(gltf); - gltfImporter.ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + gltfImporter.ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); assetDatabase.LoadAssetAtPath(PathUtils.FullPathToAssetPath(assetPath.finalPath)).Returns(dummyGo); gltf.LoadingDone.Returns(true); @@ -177,7 +177,7 @@ public async Task GltfAssetIsProcessed() await gltf.Received().Load(Arg.Any(), Arg.Any()); // Ensure that the imported is properly configured - gltfImporter.Received().ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); + gltfImporter.Received().ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); // Ensure that asset was marked for asset bundle build directory.Received().MarkFolderForAssetBundleBuild(assetPath.finalPath, assetPath.hash); @@ -266,7 +266,7 @@ private IGltfImport ConfigureGltf(ContentServerUtils.MappingPair mappingPair) buildPipeline.BuildAssetBundles(Arg.Any(), Arg.Any(), Arg.Any()).Returns(Substitute.For()); var gltf = Substitute.For(); gltfImporter.GetImporter(Arg.Any(), Arg.Any>(), Arg.Any(), Arg.Any()).Returns(gltf); - gltfImporter.ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + gltfImporter.ConfigureImporter(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); assetDatabase.LoadAssetAtPath(PathUtils.FullPathToAssetPath(assetPath.finalPath)).Returns(dummyGo); return gltf; } diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Implementations/Default/DefaultGltfImporter.cs b/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Implementations/Default/DefaultGltfImporter.cs index cb2606f2..80363d99 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Implementations/Default/DefaultGltfImporter.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Implementations/Default/DefaultGltfImporter.cs @@ -39,7 +39,8 @@ private static IMaterialGenerator GetNewMaterialGenerator(ShaderType shaderType, ? new AssetBundleConverterMaterialGenerator(AssetBundleConverterMaterialGenerator.UseNewShader(buildTarget)) : null; - public bool ConfigureImporter(string relativePath, ContentMap[] contentMap, string fileRootPath, string hash, ShaderType shaderType ) + public bool ConfigureImporter(string relativePath, ContentMap[] contentMap, string fileRootPath, string hash, ShaderType shaderType, + AnimationMethod animationMethod) { var gltfImporter = AssetImporter.GetAtPath(relativePath) as CustomGltfImporter; if (gltfImporter != null) @@ -47,7 +48,7 @@ public bool ConfigureImporter(string relativePath, ContentMap[] contentMap, stri gltfImporter.SetupCustomFileProvider(contentMap, fileRootPath, hash); gltfImporter.useOriginalMaterials = shaderType == ShaderType.GlTFast; - gltfImporter.importSettings.AnimationMethod = AnimationMethod.Legacy; + gltfImporter.importSettings.AnimationMethod = animationMethod; gltfImporter.importSettings.GenerateMipMaps = false; assetDatabase.SaveImporter(gltfImporter); diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Interfaces/IGltfImporter.cs b/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Interfaces/IGltfImporter.cs index 27113238..d6aba4e8 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Interfaces/IGltfImporter.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/Wrappers/Interfaces/IGltfImporter.cs @@ -1,5 +1,6 @@ using AssetBundleConverter.Editor; using DCL.ABConverter; +using GLTFast; using System.Collections.Generic; using UnityEditor; @@ -9,6 +10,7 @@ public interface IGltfImporter { IGltfImport GetImporter(AssetPath filePath, Dictionary contentTable, ShaderType shaderType, BuildTarget buildTarget); - bool ConfigureImporter(string relativePath, ContentMap[] contentMap, string fileRootPath, string hash, ShaderType shaderType); + bool ConfigureImporter(string relativePath, ContentMap[] contentMap, string fileRootPath, string hash, ShaderType shaderType, + AnimationMethod animationMethod); } }