diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs index 722c1f8c24..11d1e8216f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs @@ -43,22 +43,18 @@ public glTFBufferView Extend(ArraySegment array, glBufferTarget target = d public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target) { - var tmp = m_bytes; // alignment var padding = m_used % stride == 0 ? 0 : stride - m_used % stride; + var requiredLength = m_used + padding + bytesLength; - if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length) + if (m_bytes == null || requiredLength > m_bytes.Length) { // recreate buffer - m_bytes = new Byte[m_used + padding + bytesLength]; + var newLength = Math.Max(requiredLength, m_bytes?.Length * 2 ?? 256); + var newBuffer = new Byte[newLength]; if (m_used > 0) - { - Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used); - } - } - if (m_used + padding + bytesLength > m_bytes.Length) - { - throw new ArgumentOutOfRangeException(); + Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used); + m_bytes = newBuffer; } Marshal.Copy(p, m_bytes, m_used + padding, bytesLength); @@ -70,7 +66,7 @@ public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarg byteStride = stride, target = target, }; - m_used = m_used + padding + bytesLength; + m_used = requiredLength; return result; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs index fc8f4c05d2..b02250bbb9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NativeArrayManager.cs @@ -69,8 +69,8 @@ public NativeArray CreateNativeArray(int size) where T : struct public NativeArray CreateNativeArray(ArraySegment data) where T : struct { var array = CreateNativeArray(data.Count); - // TODO: remove ToArray - array.CopyFrom(data.ToArray()); + for (int i = 0; i < data.Count; i++) + array[i] = data.Array[data.Offset + i]; return array; }