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

Fix/animation import #674

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 deletions Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,26 @@ public static T[] GetArrayFromAccessor<T>(this glTF self, int accessorIndex) whe
return result;
}

public static float[] GetFloatArrayFromAccessor(this glTF self, int accessorIndex)
public static float[] FlatternFloatArrayFromAccessor(this glTF self, int accessorIndex)
{
var vertexAccessor = self.accessors[accessorIndex];

if (vertexAccessor.count <= 0) return new float[] { };

var bufferCount = vertexAccessor.count * vertexAccessor.TypeCount;
var result = (vertexAccessor.bufferView != -1)
? self.GetAttrib<float>(bufferCount, vertexAccessor.byteOffset, self.bufferViews[vertexAccessor.bufferView])
: new float[bufferCount]
;

float[] result = null;
if(vertexAccessor.bufferView != -1){
var attrib = new float[vertexAccessor.count * vertexAccessor.TypeCount];
var view = self.bufferViews[vertexAccessor.bufferView];
var segment = self.buffers[view.buffer].GetBytes();
var bytes = new ArraySegment<Byte>(segment.Array, segment.Offset + view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * view.byteStride);
bytes.MarshalCopyTo(attrib);
result = attrib;
}
else{
result = new float[bufferCount];
}

var sparse = vertexAccessor.sparse;
if (sparse != null && sparse.count > 0)
Expand Down
6 changes: 3 additions & 3 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animat
{
var sampler = animation.samplers[channel.sampler];
var input = gltf.GetArrayFromAccessor<float>(sampler.input);
var output = gltf.GetFloatArrayFromAccessor(sampler.output);
var output = gltf.FlatternFloatArrayFromAccessor(sampler.output);

AnimationImporterUtil.SetAnimationCurve(
clip,
Expand All @@ -225,7 +225,7 @@ public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animat
{
var sampler = animation.samplers[channel.sampler];
var input = gltf.GetArrayFromAccessor<float>(sampler.input);
var output = gltf.GetFloatArrayFromAccessor(sampler.output);
var output = gltf.FlatternFloatArrayFromAccessor(sampler.output);

AnimationImporterUtil.SetAnimationCurve(
clip,
Expand All @@ -251,7 +251,7 @@ public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animat
{
var sampler = animation.samplers[channel.sampler];
var input = gltf.GetArrayFromAccessor<float>(sampler.input);
var output = gltf.GetFloatArrayFromAccessor(sampler.output);
var output = gltf.FlatternFloatArrayFromAccessor(sampler.output);

AnimationImporterUtil.SetAnimationCurve(
clip,
Expand Down
16 changes: 15 additions & 1 deletion Assets/VRM.Samples/Scripts/ViewerUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void EnableTPose()
void OnOpenClicked()
{
#if UNITY_STANDALONE_WIN
var path = FileDialogForWindows.FileDialog("open VRM", "vrm", "glb", "bvh");
var path = FileDialogForWindows.FileDialog("open VRM", "vrm", "glb", "bvh", "gltf", "zip");
#elif UNITY_EDITOR
var path = UnityEditor.EditorUtility.OpenFilePanel("Open VRM", "", "vrm");
#else
Expand All @@ -281,6 +281,7 @@ void OnOpenClicked()
case ".gltf":
case ".glb":
case ".vrm":
case ".zip":
LoadModel(path);
break;

Expand Down Expand Up @@ -328,6 +329,19 @@ void LoadModel(string path)
break;
}

case ".gltf":
case ".zip":
{
var context = new UniGLTF.ImporterContext();
context.Parse(path);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
context.ShowMeshes();
SetModel(context.Root);
break;
}

default:
Debug.LogWarningFormat("unknown file type: {0}", path);
break;
Expand Down