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 gltf animation import #306

Merged
merged 2 commits into from
Sep 3, 2019
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
17 changes: 16 additions & 1 deletion Assets/VRM/UniGLTF/Scripts/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void Dispose()

public static class ArrayExtensions
{
public static int MarshalCoyTo<T>(this ArraySegment<byte> src, T[] dst) where T : struct
public static int MarshalCopyTo<T>(this ArraySegment<byte> src, T[] dst) where T : struct
{
var size = dst.Length * Marshal.SizeOf(typeof(T));
using (var pin = Pin.Create(dst))
Expand All @@ -114,6 +114,21 @@ public static T[] SelectInplace<T>(this T[] src, Func<T, T> pred)
}
return src;
}

public static void Copy<TFrom, TTo>(ArraySegment<TFrom> src, ArraySegment<TTo> dst)
where TFrom: struct
where TTo : struct
{
var bytes = new byte[src.Count() * Marshal.SizeOf(typeof(TFrom))];
using (var pin = Pin.Create(src))
{
Marshal.Copy(pin.Ptr, bytes, 0, bytes.Length);
};
using (var pin = Pin.Create(dst))
{
Marshal.Copy(bytes, 0, pin.Ptr, bytes.Length);
};
}
}

public static class ListExtensions
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 1 addition & 31 deletions Assets/VRM/UniGLTF/Scripts/Format/glTF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ T[] GetAttrib<T>(glTFAccessor accessor, glTFBufferView view) where T : struct
T[] GetAttrib<T>(int count, int byteOffset, glTFBufferView view) where T : struct
{
var attrib = new T[count];
//
var segment = buffers[view.buffer].GetBytes();
var bytes = new ArraySegment<Byte>(segment.Array, segment.Offset + view.byteOffset + byteOffset, count * view.byteStride);
bytes.MarshalCoyTo(attrib);
bytes.MarshalCopyTo(attrib);
return attrib;
}

Expand Down Expand Up @@ -166,35 +165,6 @@ public T[] GetArrayFromAccessor<T>(int accessorIndex) where T : struct
}
return result;
}

public float[] GetArrayFromAccessorAsFloat(int accessorIndex)
{
var vertexAccessor = accessors[accessorIndex];

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

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

var sparse = vertexAccessor.sparse;
if (sparse != null && sparse.count > 0)
{
// override sparse values
var indices = _GetIndices(bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType);
var values = GetAttrib<float>(sparse.count * vertexAccessor.TypeCount, sparse.values.byteOffset, bufferViews[sparse.values.bufferView]);

var it = indices.GetEnumerator();
for (int i = 0; i < sparse.count; ++i)
{
it.MoveNext();
result[it.Current] = values[i];
}
}
return result;
}
#endregion

[JsonSchema(MinItems = 1, ExplicitIgnorableItemLength = 0)]
Expand Down
18 changes: 15 additions & 3 deletions Assets/VRM/UniGLTF/Scripts/IO/AnimationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ public static List<AnimationClip> ImportAnimationClip(ImporterContext ctx)
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);
var outputVector = ctx.GLTF.GetArrayFromAccessor<Vector3>(sampler.output);
var output = new float[outputVector.Count() * 3];
ArrayExtensions.Copy<Vector3, float>(
new ArraySegment<Vector3>(outputVector),
new ArraySegment<float>(output));

AnimationImporter.SetAnimationCurve(
clip,
Expand All @@ -209,7 +213,11 @@ public static List<AnimationClip> ImportAnimationClip(ImporterContext ctx)
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);
var outputVector = ctx.GLTF.GetArrayFromAccessor<Vector4>(sampler.output);
var output = new float[outputVector.Count() * 4];
ArrayExtensions.Copy<Vector4, float>(
new ArraySegment<Vector4>(outputVector),
new ArraySegment<float>(output));

AnimationImporter.SetAnimationCurve(
clip,
Expand All @@ -235,7 +243,11 @@ public static List<AnimationClip> ImportAnimationClip(ImporterContext ctx)
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);
var outputVector = ctx.GLTF.GetArrayFromAccessor<Vector3>(sampler.output);
var output = new float[outputVector.Count() * 3];
ArrayExtensions.Copy<Vector3, float>(
new ArraySegment<Vector3>(outputVector),
new ArraySegment<float>(output));

AnimationImporter.SetAnimationCurve(
clip,
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/UniGLTF/Scripts/IO/BytesReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int ReadInt32()

public void ReadToArray<T>(T[] dst) where T : struct
{
var size = new ArraySegment<Byte>(m_bytes, m_pos, m_bytes.Length - m_pos).MarshalCoyTo(dst);
var size = new ArraySegment<Byte>(m_bytes, m_pos, m_bytes.Length - m_pos).MarshalCopyTo(dst);
m_pos += size;
}

Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,9 @@ IEnumerator LoadNodes()
{
using (MeasureTime("LoadNodes"))
{
foreach (var x in GLTF.nodes)
for (int i = 0; i < GLTF.nodes.Count; i++)
{
Nodes.Add(NodeImporter.ImportNode(x).transform);
Nodes.Add(NodeImporter.ImportNode(GLTF.nodes[i], i).transform);
}
}

Expand Down
6 changes: 5 additions & 1 deletion Assets/VRM/UniGLTF/Scripts/IO/NodeImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ namespace UniGLTF
{
public static class NodeImporter
{
public static GameObject ImportNode(glTFNode node)
public static GameObject ImportNode(glTFNode node, int nodeIndex)
{
var nodeName = node.name;
if (!string.IsNullOrEmpty(nodeName) && nodeName.Contains("/"))
{
Debug.LogWarningFormat("node {0} contains /. replace _", node.name);
nodeName = nodeName.Replace("/", "_");
}
if(string.IsNullOrEmpty(nodeName))
{
nodeName = string.Format("nodeIndex_{0}", nodeIndex);
}
var go = new GameObject(nodeName);

//
Expand Down