AnimationClip の extract を実装

This commit is contained in:
ousttrue 2021-06-01 19:36:24 +09:00
parent acfe0d347a
commit 541ece55ca
3 changed files with 33 additions and 49 deletions

View File

@ -1,10 +1,11 @@
using System.Collections.Generic;
using UnityEngine;
using VRMShaders;
namespace UniGLTF
{
public interface IAnimationImporter
{
List<AnimationClip> Import(glTF gltf, GameObject root, List<Transform> nodes, Axes invertAxis);
AnimationClip Import(glTF gltf, int i, Axes invertAxis);
}
}

View File

@ -1,53 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine;
namespace UniGLTF
{
public sealed class RootAnimationImporter : IAnimationImporter
{
public List<AnimationClip> Import(glTF gltf, GameObject root, List<Transform> _nodes, Axes invertAxis)
public AnimationClip Import(glTF gltf, int i, Axes invertAxis)
{
var animationClips = new List<AnimationClip>();
if (gltf.animations != null && gltf.animations.Any())
{
var animation = root.AddComponent<Animation>();
animationClips.AddRange(ImportAnimationClips(gltf, invertAxis));
foreach (var clip in animationClips)
{
animation.AddClip(clip, clip.name);
}
if (animationClips.Count > 0)
{
animation.clip = animationClips.First();
}
}
return animationClips;
}
private IEnumerable<AnimationClip> ImportAnimationClips(glTF gltf, Axes invertAxis)
{
for (var i = 0; i < gltf.animations.Count; ++i)
{
var clip = new AnimationClip();
clip.ClearCurves();
clip.legacy = true;
clip.name = gltf.animations[i].name;
if (string.IsNullOrEmpty(clip.name))
{
clip.name = $"legacy_{i}";
}
clip.wrapMode = WrapMode.Loop;
var animation = gltf.animations[i];
if (string.IsNullOrEmpty(animation.name))
{
animation.name = $"animation:{i}";
}
yield return AnimationImporterUtil.ConvertAnimationClip(gltf, animation, invertAxis.Create());
}
return AnimationImporterUtil.ConvertAnimationClip(gltf, gltf.animations[i], invertAxis.Create());
}
}
}

View File

@ -36,6 +36,7 @@ namespace UniGLTF
public IMaterialDescriptorGenerator MaterialDescriptorGenerator { get; protected set; }
public TextureFactory TextureFactory { get; }
public MaterialFactory MaterialFactory { get; }
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> _externalObjectMap;
public ImporterContext(
GltfParser parser,
@ -46,13 +47,13 @@ namespace UniGLTF
TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Parser);
MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator();
externalObjectMap = externalObjectMap ?? new Dictionary<SubAssetKey, UnityEngine.Object>();
_externalObjectMap = externalObjectMap ?? new Dictionary<SubAssetKey, UnityEngine.Object>();
textureDeserializer = textureDeserializer ?? new UnityTextureDeserializer();
TextureFactory = new TextureFactory(textureDeserializer, externalObjectMap
TextureFactory = new TextureFactory(textureDeserializer, _externalObjectMap
.Where(x => x.Value is Texture)
.ToDictionary(x => x.Key, x => (Texture)x.Value));
MaterialFactory = new MaterialFactory(externalObjectMap
MaterialFactory = new MaterialFactory(_externalObjectMap
.Where(x => x.Value is Material)
.ToDictionary(x => x.Key, x => (Material)x.Value));
}
@ -122,7 +123,30 @@ namespace UniGLTF
using (MeasureTime("AnimationImporter"))
{
AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, null, InvertAxis));
if (GLTF.animations != null && GLTF.animations.Any())
{
var animation = Root.AddComponent<Animation>();
for (int i = 0; i < GLTF.animations.Count; ++i)
{
var gltfAnimation = GLTF.animations[i];
AnimationClip clip = default;
if (_externalObjectMap.TryGetValue(new SubAssetKey(typeof(AnimationClip), gltfAnimation.name), out UnityEngine.Object value))
{
clip = value as AnimationClip;
}
else
{
clip = AnimationImporter.Import(GLTF, i, InvertAxis);
AnimationClips.Add(clip);
}
animation.AddClip(clip, clip.name);
if (i == 0)
{
animation.clip = clip;
}
}
}
}
await OnLoadHierarchy(awaitCaller, MeasureTime);