diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs index bdd66ae95..5788b59b3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/ArraySegmentByteBuffer.cs @@ -1,5 +1,4 @@ using System; -using System.IO; namespace UniGLTF @@ -27,9 +26,11 @@ namespace UniGLTF throw new NotImplementedException(); } - public ArraySegment GetBytes() + public void ExtendCapacity(int capacity) { - return m_bytes; + throw new NotImplementedException(); } + + public ArraySegment Bytes => m_bytes; } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs index 6984bb1ec..6ec19069a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs @@ -5,7 +5,8 @@ namespace UniGLTF public interface IBytesBuffer { string Uri { get; } - ArraySegment GetBytes(); - glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct; + ArraySegment Bytes { get; } + glTFBufferView Extend(ArraySegment array, glBufferTarget target = glBufferTarget.NONE) where T : struct; + void ExtendCapacity(int capacity); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs index 6c893b836..540ea36ae 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs @@ -40,13 +40,13 @@ namespace UniGLTF public glTFBufferView Append(ArraySegment segment, glBufferTarget target) where T : struct { var view = Storage.Extend(segment, target); - byteLength = Storage.GetBytes().Count; + byteLength = Storage.Bytes.Count; return view; } public ArraySegment GetBytes() { - return Storage.GetBytes(); + return Storage.Bytes; } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs new file mode 100644 index 000000000..a08c767fb --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs @@ -0,0 +1,98 @@ +using System; +using System.Runtime.InteropServices; + + +namespace UniGLTF +{ + /// + /// for exporter + /// + public class ArrayByteBuffer : IBytesBuffer + { + public string Uri + { + get; + private set; + } + + Byte[] m_bytes; + int m_used; + + public ArrayByteBuffer(Byte[] bytes = null) + { + Uri = ""; + m_bytes = bytes; + } + + public glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct + { + using (var pin = Pin.Create(array)) + { + var elementSize = Marshal.SizeOf(typeof(T)); + var view = Extend(pin.Ptr, array.Count * elementSize, elementSize, target); + return view; + } + } + + 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; + + if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length) + { + // recreate buffer + m_bytes = new Byte[m_used + padding + bytesLength]; + if (m_used > 0) + { + Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used); + } + } + if (m_used + padding + bytesLength > m_bytes.Length) + { + throw new ArgumentOutOfRangeException(); + } + + Marshal.Copy(p, m_bytes, m_used + padding, bytesLength); + var result = new glTFBufferView + { + buffer = 0, + byteLength = bytesLength, + byteOffset = m_used + padding, + byteStride = stride, + target = target, + }; + m_used = m_used + padding + bytesLength; + return result; + } + + public void ExtendCapacity(int capacity) + { + if (m_bytes != null && capacity < m_bytes.Length) + { + return; + } + + var newBuffer = new byte[capacity]; + if (m_bytes != null && m_used > 0) + { + Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used); + } + m_bytes = newBuffer; + } + + public ArraySegment Bytes + { + get + { + if (m_bytes == null) + { + return new ArraySegment(); + } + + return new ArraySegment(m_bytes, 0, m_used); + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs.meta index cdca54f8f..6321c430b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1e9c9201942704b4f9dd050115073032 +guid: d095d4c4eb5e68f4bb10c21bb3604cdb MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs deleted file mode 100644 index bd7963734..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/BytesBuffer.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; - - -namespace UniGLTF -{ - public static class IBytesBufferExtensions - { - public static glTFBufferView Extend(this IBytesBuffer buffer, T[] array, glBufferTarget target) where T : struct - { - return buffer.Extend(new ArraySegment(array), target); - } - } - - /// - /// for buffer with uri read - /// - public class UriByteBuffer : IBytesBuffer - { - public string Uri - { - get; - private set; - } - - Byte[] m_bytes; - public ArraySegment GetBytes() - { - return new ArraySegment(m_bytes); - } - - public UriByteBuffer(string baseDir, string uri) - { - Uri = uri; - m_bytes = ReadFromUri(baseDir, uri); - } - - const string DataPrefix = "data:application/octet-stream;base64,"; - - const string DataPrefix2 = "data:application/gltf-buffer;base64,"; - - const string DataPrefix3 = "data:image/jpeg;base64,"; - - [Obsolete("Use ReadEmbedded(uri)")] - public static Byte[] ReadEmbeded(string uri) - { - return ReadEmbedded(uri); - } - - public static Byte[] ReadEmbedded(string uri) - { - var pos = uri.IndexOf(";base64,"); - if (pos < 0) - { - throw new NotImplementedException(); - } - else - { - return Convert.FromBase64String(uri.Substring(pos + 8)); - } - } - - Byte[] ReadFromUri(string baseDir, string uri) - { - var bytes = ReadEmbedded(uri); - if (bytes != null) - { - return bytes; - } - else - { - // as local file path - return File.ReadAllBytes(Path.Combine(baseDir, uri)); - } - } - - public glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct - { - throw new NotImplementedException(); - } - } - - - /// - /// for exporter - /// - public class ArrayByteBuffer : IBytesBuffer - { - public string Uri - { - get; - private set; - } - - Byte[] m_bytes; - int m_used; - - public ArrayByteBuffer(Byte[] bytes = null) - { - Uri = ""; - m_bytes = bytes; - } - - public glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct - { - using (var pin = Pin.Create(array)) - { - var elementSize = Marshal.SizeOf(typeof(T)); - var view = Extend(pin.Ptr, array.Count * elementSize, elementSize, target); - return view; - } - } - - 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; - - if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length) - { - // recreate buffer - m_bytes = new Byte[m_used + padding + bytesLength]; - if (m_used > 0) - { - Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used); - } - } - if (m_used + padding + bytesLength > m_bytes.Length) - { - throw new ArgumentOutOfRangeException(); - } - - Marshal.Copy(p, m_bytes, m_used + padding, bytesLength); - var result=new glTFBufferView - { - buffer = 0, - byteLength = bytesLength, - byteOffset = m_used + padding, - byteStride = stride, - target = target, - }; - m_used = m_used + padding + bytesLength; - return result; - } - - public ArraySegment GetBytes() - { - if (m_bytes == null) - { - return new ArraySegment(); - } - - return new ArraySegment(m_bytes, 0, m_used); - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs new file mode 100644 index 000000000..2ca07a1ab --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; + +namespace UniGLTF +{ + /// + /// for buffer with uri read + /// + public class UriByteBuffer : IBytesBuffer + { + public string Uri + { + get; + private set; + } + + Byte[] m_bytes; + public ArraySegment Bytes => new ArraySegment(m_bytes); + + public UriByteBuffer(string baseDir, string uri) + { + Uri = uri; + m_bytes = ReadFromUri(baseDir, uri); + } + + const string DataPrefix = "data:application/octet-stream;base64,"; + + const string DataPrefix2 = "data:application/gltf-buffer;base64,"; + + const string DataPrefix3 = "data:image/jpeg;base64,"; + + [Obsolete("Use ReadEmbedded(uri)")] + public static Byte[] ReadEmbeded(string uri) + { + return ReadEmbedded(uri); + } + + public static Byte[] ReadEmbedded(string uri) + { + var pos = uri.IndexOf(";base64,"); + if (pos < 0) + { + throw new NotImplementedException(); + } + else + { + return Convert.FromBase64String(uri.Substring(pos + 8)); + } + } + + Byte[] ReadFromUri(string baseDir, string uri) + { + var bytes = ReadEmbedded(uri); + if (bytes != null) + { + return bytes; + } + else + { + // as local file path + return File.ReadAllBytes(Path.Combine(baseDir, uri)); + } + } + + public glTFBufferView Extend(ArraySegment array, glBufferTarget target) where T : struct + { + throw new NotImplementedException(); + } + + public void ExtendCapacity(int capacity) + { + throw new NotImplementedException(); + } + } +} diff --git a/Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs.meta similarity index 83% rename from Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs.meta index 9650705da..3cd3bff9b 100644 --- a/Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/UriByteBuffer.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: c700d6d6fbb38fa4d9662213c58d5725 +guid: 8b75aca0e4a7415418cd1c3d018483b2 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs b/Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs deleted file mode 100644 index 3cf5486e3..000000000 --- a/Assets/VRM10/Runtime/IO/ArrayBytesBuffer.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using VrmLib; - -namespace UniVRM10 -{ - /// - /// for exporter - /// - public class ArrayByteBuffer10 - { - public ArraySegment Bytes - { - get - { - if (m_bytes == null) - { - return new ArraySegment(); - } - - return new ArraySegment(m_bytes, 0, m_used); - } - } - - Byte[] m_bytes; - int m_used = 0; - - public ArrayByteBuffer10(Byte[] bytes = null) - { - m_bytes = bytes ?? (new byte[] { }); - } - - public ArrayByteBuffer10(Byte[] bytes, int used) - { - m_bytes = bytes ?? (new byte[] { }); - m_used = used; - } - - - public void ExtendCapacity(int byteLength) - { - var backup = m_bytes; - m_bytes = new byte[backup.Length + byteLength]; - backup.CopyTo(m_bytes, backup.Length); - } - - public void Extend(ArraySegment array, out int offset, out int length) - { - var tmp = m_bytes; - // alignment - var padding = 0; // m_used % stride == 0 ? 0 : stride - m_used % stride; - - if (m_bytes == null || m_used + padding + array.Count > m_bytes.Length) - { - // recreate buffer - var newSize = Math.Max(m_used + padding + array.Count, m_bytes.Length * 2); - m_bytes = new Byte[newSize]; - if (m_used > 0) - { - Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used); - } - } - if (m_used + padding + array.Count > m_bytes.Length) - { - throw new ArgumentOutOfRangeException(); - } - - Buffer.BlockCopy(array.Array, array.Offset, m_bytes, m_used + padding, array.Count); - - length = array.Count; - offset = m_used + padding; - - - // var result = new GltfBufferView - // { - // buffer = 0, - // byteLength = array.Length, - // byteOffset = m_used + padding, - // target = target, - // }; - // if (target == GltfBufferTargetType.ARRAY_BUFFER) - // { - // result.byteStride = stride; - // } - - m_used = m_used + padding + array.Count; - // return result; - } - } -} \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs index a3767d596..1f839839b 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs @@ -18,7 +18,7 @@ namespace UniVRM10 private set; } - public readonly List Buffers; + public readonly List Buffers; public UniGLTF.Extensions.VRMC_vrm.VRMC_vrm gltfVrm; @@ -33,9 +33,9 @@ namespace UniVRM10 { extensionsUsed = new List(), }; - Buffers = new List() + Buffers = new List() { - new ArrayByteBuffer10() + new UniGLTF.ArrayByteBuffer() }; } @@ -61,10 +61,9 @@ namespace UniVRM10 gltfVrmSpringBone = springBone; } - var array = bin.ToArray(); - Buffers = new List() + Buffers = new List() { - new ArrayByteBuffer10(array, bin.Count) + new UniGLTF.ArraySegmentByteBuffer(bin) }; } @@ -75,14 +74,9 @@ namespace UniVRM10 public int AppendToBuffer(int bufferIndex, ArraySegment segment) { - Buffers[bufferIndex].Extend(segment, out int offset, out int length); + var gltfBufferView = Buffers[bufferIndex].Extend(segment); var viewIndex = Gltf.bufferViews.Count; - Gltf.bufferViews.Add(new UniGLTF.glTFBufferView - { - buffer = 0, - byteOffset = offset, - byteLength = length, - }); + Gltf.bufferViews.Add(gltfBufferView); return viewIndex; }