fix animation array copy

This commit is contained in:
j-hirose 2019-08-29 17:26:58 +09:00
parent eac317c43d
commit f1d40f6ded
5 changed files with 34 additions and 39 deletions

View File

@ -89,7 +89,7 @@ namespace UniGLTF
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))
@ -114,6 +114,21 @@ namespace UniGLTF
}
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

View File

@ -1,7 +1,5 @@
fileFormatVersion: 2
guid: e843bb7270bc9f54f8495e1e35f82e8d
timeCreated: 1514252357
licenseType: Free
guid: a942ac1d32b5c604988565d1d5442237
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -59,10 +59,9 @@ namespace UniGLTF
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;
}
@ -166,35 +165,6 @@ namespace UniGLTF
}
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)]

View File

@ -186,7 +186,11 @@ namespace UniGLTF
{
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,
@ -209,7 +213,11 @@ namespace UniGLTF
{
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,
@ -235,7 +243,11 @@ namespace UniGLTF
{
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,

View File

@ -63,7 +63,7 @@ namespace UniGLTF
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;
}