Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support animation #27

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update VRM examples to follow new interfaces for Animations
  • Loading branch information
yutopp committed Jun 5, 2022
commit 35c055caa32888d8005ecaf167596fbb5c583a87
80 changes: 50 additions & 30 deletions Assets/VGltfExamples/VRMExample/Scripts/VRMLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public sealed class VRMLoader : MonoBehaviour

[SerializeField] public RuntimeAnimatorController RuntimeAnimatorController;

// デバッグ用
[SerializeField] AnimationClip[] Clips;

sealed class VRMResource : IDisposable
{
public IImporterContext Context;
Expand All @@ -42,6 +39,21 @@ public void Dispose()

readonly List<VRMResource> _vrmResources = new List<VRMResource>();

[Serializable]
sealed class ClipResource : IDisposable
{
public IImporterContext Context;
[SerializeField] public AnimationClip[] ClipRefs;

public void Dispose()
{
Context?.Dispose();
}
}

// デバッグ用
[SerializeField] List<ClipResource> _clipResources = new List<ClipResource>();

void Start()
{
loadButton.onClick.AddListener(UIOnLoadButtonClick);
Expand All @@ -61,6 +73,11 @@ void OnDestroy()
{
disposable.Dispose();
}

foreach(var disposable in _clipResources)
{
disposable.Dispose();
}
}

async UniTask<VRMResource> LoadVRM()
Expand Down Expand Up @@ -122,9 +139,9 @@ void UIOnLoadButtonClick()

async UniTaskVoid UIOnLoadButtonClickAsync()
{
#if true
var context = await ImportClip("anim.glb"); // TODO: このままだとリソースリークするのでデバッグ終わったら消す
Clips = context.Resources.Animations.Map(clip => clip).Select(iv => iv.Value).ToArray();
#if false
var clipRes = await ImportClip("anim.glb");
_clipResources.Insert(0, clipRes);
#endif

var p0 = Common.MemoryProfile.Now;
Expand Down Expand Up @@ -222,6 +239,33 @@ await Task.Run(() =>
#endif
}

async Task<ClipResource> ImportClip(string path)
{
// Read the glTF container (unity-independent)
var gltfContainer = await Task.Run(() =>
{
using (var fs = new FileStream(path, FileMode.Open))
{
return GltfContainer.FromGlb(fs);
}
});

var clipRefs = new List<AnimationClip>();

var timeSlicer = new Common.TimeSlicer();
using (var gltfImporter = new Importer(gltfContainer, timeSlicer))
{
gltfImporter.Context.Importers.Animations.AddHook(new VGltf.Unity.Ext.Helper.HumanoidAnimationImporter(clipRefs));

var context = await gltfImporter.ImportOnlyAnimations(System.Threading.CancellationToken.None);
return new ClipResource
{
Context = context,
ClipRefs = clipRefs.ToArray(),
};
}
}

async Task ExportClip(AnimationClip clip, string path)
{
GltfContainer gltfContainer = null;
Expand All @@ -244,30 +288,6 @@ await Task.Run(() =>
});
}

async Task<IImporterContext> ImportClip(string path)
{
// Read the glTF container (unity-independent)
var gltfContainer = await Task.Run(() =>
{
using (var fs = new FileStream(path, FileMode.Open))
{
return GltfContainer.FromGlb(fs);
}
});

// Create a glTF Importer for Unity.
// The resources will be cached in the internal Context of this Importer.
// Resources can be released by calling Dispose of the Importer (or the internal Context).
var timeSlicer = new Common.TimeSlicer();
using (var gltfImporter = new Importer(gltfContainer, timeSlicer))
{
gltfImporter.Context.Importers.Animations.AddHook(new VGltf.Unity.Ext.Helper.HumanoidAnimationImporter());

// Load the Scene.
return await gltfImporter.ImportOnlyAnimations(System.Threading.CancellationToken.None);
}
}

void DebugLogProfile(Common.MemoryProfile now, Common.MemoryProfile prev = null)
{
Debug.Log($"----------");
Expand Down