Skip to content

Commit

Permalink
feat: desktop animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinerius committed Mar 14, 2024
1 parent efc64a5 commit 41585ec
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ private async Task<bool> 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
Expand Down Expand Up @@ -341,7 +343,7 @@ private async Task<bool> 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)
Expand Down Expand Up @@ -417,6 +419,9 @@ private async Task<bool> ProcessAllGltfs()
return false;
}

private AnimationMethod GetAnimationMethod() =>
settings.buildTarget is BuildTarget.StandaloneWindows64 or BuildTarget.StandaloneOSX ? AnimationMethod.Mecanim : AnimationMethod.Legacy;

private void ExtractEmbedMaterialsFromGltf(List<Texture2D> textures, GltfImportSettings gltf, IGltfImport gltfImport, string gltfUrl)
{
Profiler.BeginSample("ExtractEmbedMaterials");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Animator>();

if (animator != null)
{
DestroyImmediate(animator);
var clips = m_Gltf.GetAnimationClips();
var animation = gameObject.AddComponent<Animation>();

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<MeshFilter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async Task GltfAssetIsProcessed()
var gltf = Substitute.For<IGltfImport>();

gltfImporter.GetImporter(Arg.Any<AssetPath>(), Arg.Any<Dictionary<string, string>>(), Arg.Any<ShaderType>(), Arg.Any<BuildTarget>()).Returns(gltf);
gltfImporter.ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>()).Returns(true);
gltfImporter.ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>(), Arg.Any<AnimationMethod>()).Returns(true);
assetDatabase.LoadAssetAtPath<GameObject>(PathUtils.FullPathToAssetPath(assetPath.finalPath)).Returns(dummyGo);

gltf.LoadingDone.Returns(true);
Expand All @@ -177,7 +177,7 @@ public async Task GltfAssetIsProcessed()
await gltf.Received().Load(Arg.Any<string>(), Arg.Any<ImportSettings>());

// Ensure that the imported is properly configured
gltfImporter.Received().ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>());
gltfImporter.Received().ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>(), Arg.Any<AnimationMethod>());

// Ensure that asset was marked for asset bundle build
directory.Received().MarkFolderForAssetBundleBuild(assetPath.finalPath, assetPath.hash);
Expand Down Expand Up @@ -266,7 +266,7 @@ private IGltfImport ConfigureGltf(ContentServerUtils.MappingPair mappingPair)
buildPipeline.BuildAssetBundles(Arg.Any<string>(), Arg.Any<BuildAssetBundleOptions>(), Arg.Any<BuildTarget>()).Returns(Substitute.For<IAssetBundleManifest>());
var gltf = Substitute.For<IGltfImport>();
gltfImporter.GetImporter(Arg.Any<AssetPath>(), Arg.Any<Dictionary<string, string>>(), Arg.Any<ShaderType>(), Arg.Any<BuildTarget>()).Returns(gltf);
gltfImporter.ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>()).Returns(true);
gltfImporter.ConfigureImporter(Arg.Any<string>(), Arg.Any<ContentMap[]>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<ShaderType>(), Arg.Any<AnimationMethod>()).Returns(true);
assetDatabase.LoadAssetAtPath<GameObject>(PathUtils.FullPathToAssetPath(assetPath.finalPath)).Returns(dummyGo);
return gltf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ 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)
{
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AssetBundleConverter.Editor;
using DCL.ABConverter;
using GLTFast;
using System.Collections.Generic;
using UnityEditor;

Expand All @@ -9,6 +10,7 @@ public interface IGltfImporter
{
IGltfImport GetImporter(AssetPath filePath, Dictionary<string, string> 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);
}
}

0 comments on commit 41585ec

Please sign in to comment.