diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 511af7b7c..02edf3e12 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -316,7 +316,7 @@ namespace UniGLTF return result; } - public static float[] GetArrayFromAccessorAsFloat(this glTF self, int accessorIndex) + public static float[] GetFloatArrayFromAccessor(this glTF self, int accessorIndex) { var vertexAccessor = self.accessors[accessorIndex]; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs index 221f289ab..9cd5fd680 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs @@ -185,7 +185,7 @@ namespace UniGLTF return string.Join("/", path); } - public static AnimationClip ImportAnimationClip(ImporterContext ctx, glTFAnimation animation, glTFNode root = null) + public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animation, glTFNode root = null) { var clip = new AnimationClip(); clip.ClearCurves(); @@ -195,14 +195,14 @@ namespace UniGLTF foreach (var channel in animation.channels) { - var relativePath = RelativePathFrom(ctx.GLTF.nodes, root, ctx.GLTF.nodes[channel.target.node]); + var relativePath = RelativePathFrom(gltf.nodes, root, gltf.nodes[channel.target.node]); switch (channel.target.path) { case glTFAnimationTarget.PATH_TRANSLATION: { var sampler = animation.samplers[channel.sampler]; - var input = ctx.GLTF.GetArrayFromAccessor(sampler.input); - var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output); + var input = gltf.GetArrayFromAccessor(sampler.input); + var output = gltf.GetFloatArrayFromAccessor(sampler.output); AnimationImporterUtil.SetAnimationCurve( clip, @@ -224,8 +224,8 @@ namespace UniGLTF case glTFAnimationTarget.PATH_ROTATION: { var sampler = animation.samplers[channel.sampler]; - var input = ctx.GLTF.GetArrayFromAccessor(sampler.input); - var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output); + var input = gltf.GetArrayFromAccessor(sampler.input); + var output = gltf.GetFloatArrayFromAccessor(sampler.output); AnimationImporterUtil.SetAnimationCurve( clip, @@ -250,8 +250,8 @@ namespace UniGLTF case glTFAnimationTarget.PATH_SCALE: { var sampler = animation.samplers[channel.sampler]; - var input = ctx.GLTF.GetArrayFromAccessor(sampler.input); - var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output); + var input = gltf.GetArrayFromAccessor(sampler.input); + var output = gltf.GetFloatArrayFromAccessor(sampler.output); AnimationImporterUtil.SetAnimationCurve( clip, @@ -267,8 +267,8 @@ namespace UniGLTF case glTFAnimationTarget.PATH_WEIGHT: { - var node = ctx.GLTF.nodes[channel.target.node]; - var mesh = ctx.GLTF.meshes[node.mesh]; + var node = gltf.nodes[channel.target.node]; + var mesh = gltf.meshes[node.mesh]; var primitive = mesh.primitives.FirstOrDefault(); var targets = primitive.targets; @@ -283,8 +283,8 @@ namespace UniGLTF .ToArray(); var sampler = animation.samplers[channel.sampler]; - var input = ctx.GLTF.GetArrayFromAccessor(sampler.input); - var output = ctx.GLTF.GetArrayFromAccessor(sampler.output); + var input = gltf.GetArrayFromAccessor(sampler.input); + var output = gltf.GetArrayFromAccessor(sampler.output); AnimationImporterUtil.SetAnimationCurve( clip, relativePath, @@ -312,51 +312,5 @@ namespace UniGLTF } return clip; } - - private static List ImportAnimationClips(ImporterContext ctx) - { - var animationClips = new List(); - for (var i = 0; i < ctx.GLTF.animations.Count; ++i) - { - var clip = new AnimationClip(); - clip.ClearCurves(); - clip.legacy = true; - clip.name = ctx.GLTF.animations[i].name; - if (string.IsNullOrEmpty(clip.name)) - { - clip.name = $"legacy_{i}"; - } - clip.wrapMode = WrapMode.Loop; - - var animation = ctx.GLTF.animations[i]; - if (string.IsNullOrEmpty(animation.name)) - { - animation.name = $"animation:{i}"; - } - - animationClips.Add(ImportAnimationClip(ctx, animation)); - } - - return animationClips; - } - - public static void ImportAnimation(ImporterContext ctx) - { - // animation - if (ctx.GLTF.animations != null && ctx.GLTF.animations.Any()) - { - var animation = ctx.Root.AddComponent(); - ctx.AnimationClips = ImportAnimationClips(ctx); - foreach (var clip in ctx.AnimationClips) - { - animation.AddClip(clip, clip.name); - } - if (ctx.AnimationClips.Count > 0) - { - animation.clip = ctx.AnimationClips.First(); - } - } - } - } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 9bb7f3537..abf45ef75 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -86,7 +86,7 @@ namespace UniGLTF #endregion #region Animation - IAnimationImporter m_animationImporter; + protected IAnimationImporter m_animationImporter; public void SetAnimationImporter(IAnimationImporter animationImporter) { m_animationImporter = animationImporter; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs index 453d04122..5c3b4e406 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs @@ -1,10 +1,55 @@ -namespace UniGLTF +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace UniGLTF { public sealed class RootAnimationImporter : IAnimationImporter { public void Import(ImporterContext context) { - AnimationImporterUtil.ImportAnimation(context); + // animation + if (context.GLTF.animations != null && context.GLTF.animations.Any()) + { + var animation = context.Root.AddComponent(); + context.AnimationClips = ImportAnimationClips(context.GLTF); + + foreach (var clip in context.AnimationClips) + { + animation.AddClip(clip, clip.name); + } + if (context.AnimationClips.Count > 0) + { + animation.clip = context.AnimationClips.First(); + } + } + } + + private List ImportAnimationClips(glTF gltf) + { + var animationClips = new List(); + 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}"; + } + + animationClips.Add(AnimationImporterUtil.ConvertAnimationClip(gltf, animation)); + } + + return animationClips; } } } \ No newline at end of file